Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Support AssertJ soft assertions
  • Loading branch information
Achitheus committed Dec 30, 2023
commit 4ad3f29d8eb3c1625bd4683e097e48e45c72a63c
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
20 changes: 17 additions & 3 deletions allure-java-commons/src/main/java/io/qameta/allure/Allure.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static io.qameta.allure.util.ResultsUtils.createParameter;
import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
import static io.qameta.allure.util.StepsUtils.getStepStatus;
import static java.util.Arrays.asList;
import static java.util.concurrent.CompletableFuture.supplyAsync;

Expand Down Expand Up @@ -179,16 +180,29 @@ public static <T> T step(final ThrowableContextRunnable<T, StepContext> runnable

try {
final T result = runnable.run(new DefaultStepContext(uuid));
getLifecycle().updateStep(uuid, step -> step.setStatus(Status.PASSED));
Status currentStepStatus = getStepStatus();
if (currentStepStatus == null) {
getLifecycle().updateStep(uuid, step -> step.setStatus(Status.PASSED));
getLifecycle().stopStep();
} else {
getLifecycle().stopStep();
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(outerStep -> outerStep.setStatus(currentStepStatus));
}
}
return result;
} catch (Throwable throwable) {
getLifecycle().updateStep(s -> s
.setStatus(getStatus(throwable).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(throwable).orElse(null)));
getLifecycle().stopStep(uuid);
// Is there possibility that anyone puts step(Throwable) calls into try/catch?
// Let the chain of steps be red even in such strange cases as this.
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(outerStep -> outerStep.setStatus(getStatus(throwable).orElse(Status.BROKEN)));
}
ExceptionUtils.sneakyThrow(throwable);
return null;
} finally {
getLifecycle().stopStep(uuid);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ public Optional<String> getCurrentTestCaseOrStep() {
return threadContext.getCurrent();
}

/**
* Returns uuid of current step.
*
* @return the uuid of current running test case or step.
*/
public Optional<String> getCurrentStep() {
return threadContext.getCurrentStep();
}

/**
* Sets specified test case uuid as current. Note that
* test case with such uuid should be created and existed in storage, otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static io.qameta.allure.util.AspectUtils.getParameters;
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 Dmitry Baev charlie@yandex-team.ru
Expand Down Expand Up @@ -85,12 +86,25 @@ public void stepFailed(final Throwable e) {
.setStatus(getStatus(e).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(e).orElse(null)));
getLifecycle().stopStep();
// Is there possibility that anyone puts @Step-methods calls into try/catch?
// Let the chain of steps be red even in such strange cases as this.
if (getLifecycle().getCurrentStep().isPresent()) {
getLifecycle().updateStep(outerStep -> outerStep.setStatus(getStatus(e).orElse(Status.BROKEN)));
}
}

@AfterReturning(pointcut = "anyMethod() && withStepAnnotation()")
public void stepStop() {
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
getLifecycle().stopStep();
Status currentStepStatus = getStepStatus();
if (currentStepStatus == null) {
getLifecycle().updateStep(currentStep -> currentStep.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
Expand Up @@ -28,6 +28,16 @@ public class AllureThreadContext {

private final Context context = new Context();

/**
* Returns last (most recent) uuid but not the root.
*/
public Optional<String> getCurrentStep() {
final LinkedList<String> uuids = context.get();
return uuids.size() < 2
? Optional.empty()
: Optional.of(uuids.getFirst());
}

/**
* Returns last (most recent) uuid.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.util;

import io.qameta.allure.model.Status;

import java.util.concurrent.atomic.AtomicReference;

import static io.qameta.allure.Allure.getLifecycle;

public class StepsUtils {

public static Status getStepStatus() {
AtomicReference<Status> outerStepRef = new AtomicReference<>();
getLifecycle().updateStep(step -> outerStepRef.set(step.getStatus()));
return outerStepRef.get();
}
}