Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support AssertJ soft assertions (#19) #988

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions allure-assertj/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dependencies {
testImplementation(project(":allure-java-commons-test"))
testImplementation(project(":allure-junit-platform"))
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.vintage:junit-vintage-engine")
testImplementation("org.junit.jupiter:junit-jupiter-params")
}

tasks.jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.assertj.core.api.DefaultAssertionErrorCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,6 +38,7 @@

import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
import static io.qameta.allure.util.StepsUtils.getStepStatus;

/**
* @author charlie (Dmitry Baev).
Expand Down Expand Up @@ -65,11 +67,35 @@ public void proxyMethod() {
//pointcut body, should be empty
}

@Pointcut("execution(public * org.assertj.core.api.AbstractAssert+.*(..)) && !proxyMethod()")
@Pointcut("execution(* org.assertj.core.api.*ByteBuddy*.*(..))")
public void generatedByteCode() {
//pointcut body, should be empty
}

@Pointcut("execution(public * org.assertj.core.api.AbstractAssert+.*(..)) " +
"&& !proxyMethod() && !generatedByteCode()")
public void anyAssert() {
//pointcut body, should be empty
}

@Pointcut("execution(public org.assertj.core.api.DefaultAssertionErrorCollector.new())")
public void softAssertCreation() {
//pointcut body, should be empty
}

@After("softAssertCreation()")
public void setStepHookOnCollectedError(final JoinPoint joinPoint) {
final DefaultAssertionErrorCollector errorCollector = (DefaultAssertionErrorCollector) joinPoint.getTarget();
errorCollector.setAfterAssertionErrorCollected(error -> {
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(step -> {
step.setStatus(getStatus(error).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(error).orElse(null));
});
}
});
}

@After("anyAssertCreation()")
public void logAssertCreation(final JoinPoint joinPoint) {
final String actual = joinPoint.getArgs().length > 0
Expand Down Expand Up @@ -107,12 +133,24 @@ public void stepFailed(final Throwable e) {
.setStatus(getStatus(e).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(e).orElse(null)));
getLifecycle().stopStep();
// Outer method can catch exception so outer step status should be also changed (soft assertions case).
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(s -> s.setStatus(getStatus(e).orElse(Status.BROKEN)));
}
}

@AfterReturning(pointcut = "anyAssert()")
public void stepStop() {
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
getLifecycle().stopStep();
final Status currentStepStatus = getStepStatus();
if (currentStepStatus == null) {
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
getLifecycle().stopStep();
} else {
getLifecycle().stopStep();
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(outerStep -> outerStep.setStatus(currentStepStatus));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj;

import io.qameta.allure.test.AllureFeatures;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.StandardSoftAssertionsProvider;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.function.Consumer;

import static io.qameta.allure.assertj.util.SoftAssertionsUtils.eraseCollectedErrors;

/**
* @author Achitheus (Yury Yurchenko).
*/
public class SoftAssertionsBasicApproachesTest {

@AllureFeatures.Steps
@DisplayName("Check hardcoded soft assertions object")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
void tests(String testName, Consumer<StandardSoftAssertionsProvider> test) {
SoftAssertions softly = new SoftAssertions();
test.accept(softly);
}

@AllureFeatures.Steps
@DisplayName("Check autocloseable soft assertions")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
void autocloseableTests(String testName, Consumer<StandardSoftAssertionsProvider> test) {
try (AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()) {
test.accept(softly);
eraseCollectedErrors(softly);
}
}

@AllureFeatures.Steps
@DisplayName("Check SoftAssertions.assertSoftly() method")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
void assertSoftlyMethodTests(String testName, Consumer<StandardSoftAssertionsProvider> test) {
SoftAssertions.assertSoftly(softly -> {
test.accept(softly);
eraseCollectedErrors(softly);
});
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj;

import io.qameta.allure.test.AllureFeatures;
import org.assertj.core.api.JUnitSoftAssertions;
import org.assertj.core.api.StandardSoftAssertionsProvider;
import org.junit.Rule;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.function.Consumer;

import static io.qameta.allure.assertj.util.SoftAssertionsUtils.eraseCollectedErrors;

/**
* @author Achitheus (Yury Yurchenko).
*/
public class SoftAssertionsJUnit4RuleTest {

@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();

@AllureFeatures.Steps
@DisplayName("Check JUnit4 Rule soft assertions")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
public void tests(String testName, Consumer<StandardSoftAssertionsProvider> test) {
test.accept(softly);
eraseCollectedErrors(softly);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj;

import io.qameta.allure.test.AllureFeatures;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.StandardSoftAssertionsProvider;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.function.Consumer;

import static io.qameta.allure.assertj.util.SoftAssertionsUtils.eraseCollectedErrors;

/**
* @author Achitheus (Yury Yurchenko).
*/
@ExtendWith(SoftAssertionsExtension.class)
public class SoftAssertionsJUnit5ExtensionTest {

@InjectSoftAssertions
private SoftAssertions softly;

@AllureFeatures.Steps
@DisplayName("Check soft assertions obj injected as field by JUnit5 extension")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
void fieldInjectionTests(String testName, Consumer<StandardSoftAssertionsProvider> test) {
test.accept(softly);
eraseCollectedErrors(softly);
}

@AllureFeatures.Steps
@DisplayName("Check soft assertions obj injected as parameter by JUnit5 extension")
@ParameterizedTest(name = "[{index}]: {arguments}")
@MethodSource(value = "io.qameta.allure.assertj.util.SoftAssertionsTestsProvider#softAssertionsTests")
void parameterInjectionTests(String testName, Consumer<StandardSoftAssertionsProvider> test, SoftAssertions softly) {
test.accept(softly);
eraseCollectedErrors(softly);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj.util;

import java.lang.reflect.Method;
import java.util.function.Consumer;

public class ReflectionUtils {

public static <T> Consumer<T> staticMethodAsConsumer(Method m) {
return param -> {
try {
m.invoke(null, param);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
};
}
}
Loading
Loading