Skip to content
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 @@ -21,6 +21,8 @@
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.lang.management.ManagementFactory;
import java.math.BigInteger;
Expand All @@ -33,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -130,6 +133,9 @@ public void onFinish(ITestContext context) {
public void onTestStart(ITestResult testResult) {
LOGGER.info("onTestStart of " + testResult.getName());
Current current = currentTestResult.get();
if (current.isStarted()) {
current = refreshContext();
}
current.test();
String parentUuid = getUniqueUuid(testResult.getTestContext());
ITestNGMethod method = testResult.getMethod();
Expand All @@ -138,8 +144,8 @@ public void onTestStart(ITestResult testResult) {
label("package", testClass.getName()),
label("testClass", testClass.getName()),
label("testMethod", method.getMethodName()),
label("parentSuite", testClass.getXmlTest().getSuite().getName()),
label("suite", testClass.getXmlTest().getName()),
label("parentSuite", safeExtractSuiteName(testClass)),
label("suite", safeExtractTestTag(testClass)),
label("host", HOST),
label("thread", getThreadName())
);
Expand Down Expand Up @@ -171,15 +177,14 @@ public void onTestFailure(ITestResult result) {
Current current = currentTestResult.get();

if (current.isAfter()) {
currentTestResult.remove();
current = currentTestResult.get();
current = refreshContext();
}

//if test has failed without any setup
if (!current.isStarted()) {
onTestStart(result);
currentTestResult.remove();
createTestResutForUnexecutedTest(result);
}

current.after();
Throwable throwable = result.getThrowable();
Status status = getStatus(throwable).orElse(Status.BROKEN);
Expand All @@ -196,14 +201,12 @@ public void onTestSkipped(ITestResult result) {

//test is being skipped as dependent on failed test, closing context for previous test here
if (current.isAfter()) {
currentTestResult.remove();
current = currentTestResult.get();
current = refreshContext();
}

//if test was skipped without any setup
if (!current.isStarted()) {
onTestStart(result);
currentTestResult.remove();
createTestResutForUnexecutedTest(result);
}
current.after();
StatusDetails details = getStatusDetails(result.getThrowable()).orElse(null);
Expand All @@ -212,6 +215,11 @@ public void onTestSkipped(ITestResult result) {
getLifecycle().writeTestCase(current.getUuid());
}

private void createTestResutForUnexecutedTest(ITestResult result) {
onTestStart(result);
currentTestResult.remove();
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
//do nothing
Expand Down Expand Up @@ -368,6 +376,16 @@ private MessageDigest getMessageDigest() {
}
}

private static String safeExtractSuiteName(ITestClass testClass) {
Optional<XmlTest> xmlTest = Optional.ofNullable(testClass.getXmlTest());
return xmlTest.map(XmlTest::getSuite).map(XmlSuite::getName).orElse("Undefined suite");
}

private static String safeExtractTestTag(ITestClass testClass) {
Optional<XmlTest> xmlTest = Optional.ofNullable(testClass.getXmlTest());
return xmlTest.map(XmlTest::getName).orElse("Undefined test tag");
}

private Label label(String name, String value) {
return new Label().withName(name).withValue(value);
}
Expand Down Expand Up @@ -411,6 +429,11 @@ private Consumer<TestResult> setStatus(Status status, StatusDetails details) {
.withStatusDetails(details);
}

private Current refreshContext() {
currentTestResult.remove();
return currentTestResult.get();
}

private static class Current {
private final String uuid;
private CurrentStage currentStage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.stream.IntStream;

/**
* @author ehborisov
Expand All @@ -31,7 +32,8 @@ public void beforeTest() {
}

@BeforeTest
public void beforeTest2() {}
public void beforeTest2() {
}

@BeforeSuite
public void beforeSuite2() throws IOException {
Expand All @@ -53,16 +55,15 @@ public void test1() throws IOException {
}

@DataProvider(name = "dataProvider")
public Object[][] getTestData() {
return new Object[][]{
{"param11", "param12"},
{"param21", "param22"}
};
public Object[][] provide() {
return IntStream.range(0, 2000)
.mapToObj(i -> new String[]{String.valueOf(i)})
.toArray(Object[][]::new);
}

@Test(dataProvider = "dataProvider")
@DisplayName("Test 2")
public void test2(@Parameter("param1") String param1, @Parameter("param2") String param2) throws IOException {
public void test2(@Parameter("param") String param) throws IOException {
stepSix();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import io.qameta.allure.model.TestResultContainer;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* @author Egor Borisov ehborisov@gmail.com
*/
public class AllureResultsWriterStub implements AllureResultsWriter {

private List<TestResult> testResults = new ArrayList<>();
private List<TestResultContainer> testContainers = new ArrayList<>();
private CopyOnWriteArrayList<TestResult> testResults = new CopyOnWriteArrayList<>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to use List type for field

private CopyOnWriteArrayList<TestResultContainer> testContainers = new CopyOnWriteArrayList<>();

public void write(TestResult testResult) {
testResults.add(testResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

/**
* @author Egor Borisov ehborisov@gmail.com
Expand Down Expand Up @@ -54,7 +56,7 @@ private void runTestNGSuites(String... suites) {
testNG.run();
}

@Test(enabled = false)
@Test
public void parallelDataProvider() {
runTestNGSuites("suites/parallel-data-provider.xml");
List<TestResult> testResult = results.getTestResults();
Expand Down Expand Up @@ -260,9 +262,9 @@ public void parallelMethods() {
List<String> uids = testResults.stream().map(TestResult::getUuid).collect(Collectors.toList());
List<TestResultContainer> testContainers = results.getTestContainers();
assertThat(testResults).as("Unexpected quantity of test case results has been written")
.hasSize(3);
.hasSize(2001);
assertThat(testContainers).as("Unexpected quantity of test containers has been written")
.hasSize(11);
.hasSize(6005);

assertContainersPerMethod(before1, testContainers, uids);
assertContainersPerMethod(before2, testContainers, uids);
Expand All @@ -281,7 +283,7 @@ private static void assertContainersPerMethod(String name, List<TestResultContai
.is(singlyMapped)
.flatExtracting(TestResultContainer::getChildren)
.as("Unexpected children for per-method fixtures " + name)
.containsAll(uids);
.containsOnlyElementsOf(uids);
}

@Test
Expand Down