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

Report skipped config methods as aborted test classes #69

Merged
merged 1 commit into from
Oct 23, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.junit.platform.engine.EngineExecutionListener;
import org.junit.platform.engine.TestExecutionResult;
Expand All @@ -40,8 +42,8 @@ class ExecutionListener extends DefaultListener {
private final TestClassRegistry testClassRegistry = new TestClassRegistry();
private final Map<ITestNGMethod, MethodProgress> inProgressTestMethods = new ConcurrentHashMap<>();

private final Set<Throwable> engineFailures = ConcurrentHashMap.newKeySet();
private final Map<ClassDescriptor, Set<Throwable>> classFailures = new ConcurrentHashMap<>();
private final Set<ITestResult> engineLevelFailureResults = ConcurrentHashMap.newKeySet();
private final Map<ClassDescriptor, Set<ITestResult>> classLevelFailureResults = new ConcurrentHashMap<>();

private final EngineExecutionListener delegate;
private final TestNGEngineDescriptor engineDescriptor;
Expand All @@ -63,13 +65,22 @@ public void onBeforeClass(ITestClass testClass) {

@Override
public void onConfigurationFailure(ITestResult result) {
handleConfigurationResult(result);
}

@Override
public void onConfigurationSkip(ITestResult result) {
handleConfigurationResult(result);
}

private void handleConfigurationResult(ITestResult result) {
Optional<ClassDescriptor> classDescriptor = testClassRegistry.get(result.getTestClass().getRealClass());
if (classDescriptor.isPresent()) {
classFailures.computeIfAbsent(classDescriptor.get(), __ -> ConcurrentHashMap.newKeySet()) //
.add(result.getThrowable());
classLevelFailureResults.computeIfAbsent(classDescriptor.get(), __ -> ConcurrentHashMap.newKeySet()) //
.add(result);
}
else {
engineFailures.add(result.getThrowable());
engineLevelFailureResults.add(result);
}
}

Expand All @@ -78,8 +89,8 @@ public void onAfterClass(ITestClass testClass) {
testClassRegistry.finish(testClass.getRealClass(),
classDescriptor -> classDescriptor.remainingIterations.decrementAndGet() == 0, classDescriptor -> {
finishMethodsNotYetReportedAsFinished(testClass);
Set<Throwable> failures = classFailures.remove(classDescriptor);
delegate.executionFinished(classDescriptor, toTestExecutionResult(failures));
Set<ITestResult> results = classLevelFailureResults.remove(classDescriptor);
delegate.executionFinished(classDescriptor, toTestExecutionResult(results));
});
}

Expand Down Expand Up @@ -207,14 +218,24 @@ private TestDescriptorFactory getTestDescriptorFactory() {
}

public TestExecutionResult toEngineResult() {
return toTestExecutionResult(engineFailures);
return toTestExecutionResult(engineLevelFailureResults);
}

private TestExecutionResult toTestExecutionResult(Set<ITestResult> results) {
return results == null || results.isEmpty() ? successful() : abortedOrFailed(results);
}

private static TestExecutionResult abortedOrFailed(Set<ITestResult> results) {
return results.stream().allMatch(it -> it.getStatus() == ITestResult.SKIP) //
? aborted(chain(throwables(results))) //
: failed(chain(throwables(results)));
}

private TestExecutionResult toTestExecutionResult(Set<Throwable> failures) {
return failures == null || failures.isEmpty() ? successful() : failed(chain(failures));
private static Stream<Throwable> throwables(Set<ITestResult> results) {
return results.stream().map(ITestResult::getThrowable).filter(Objects::nonNull);
}

private Throwable chain(Set<Throwable> failures) {
private static Throwable chain(Stream<Throwable> failures) {
Iterator<Throwable> iterator = failures.iterator();
Throwable throwable = iterator.next();
iterator.forEachRemaining(throwable::addSuppressed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import example.basics.SimpleTestCase;
import example.basics.SuccessPercentageTestCase;
import example.basics.TimeoutTestCase;
import example.configuration.methods.AbortedBeforeClassConfigurationMethodTestCase;
import example.configuration.methods.FailingBeforeClassConfigurationMethodTestCase;
import example.dataproviders.DataProviderMethodTestCase;

Expand Down Expand Up @@ -117,6 +118,17 @@ void reportsFailureFromBeforeClassMethod() {
finishedWithFailure(message("boom"))));
}

@Test
void reportsAbortedBeforeClassMethod() {
var results = testNGEngine().selectors(
selectClass(AbortedBeforeClassConfigurationMethodTestCase.class)).execute();

results.allEvents().debug().assertEventsMatchLooselyInOrder( //
event(testClass(AbortedBeforeClassConfigurationMethodTestCase.class), started()), //
event(testClass(AbortedBeforeClassConfigurationMethodTestCase.class),
abortedWithReason(message("not today"))));
}

@Test
void executesNoTestWhenPostDiscoveryFilterExcludesEverything() {
var testClass = SimpleTestCase.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.configuration.methods;

import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AbortedBeforeClassConfigurationMethodTestCase {

@BeforeClass
public void beforeClass() {
throw new SkipException("not today");
}

@Test
public void test() {
// never called
}
}