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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:

- name: Run tests
if: always()
run: ./gradlew test
run: ./gradlew --no-build-cache cleanTest test
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,12 @@
@Feature("Filtration")
@interface Filtration {
}

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Feature("Ordering")
@interface Ordering {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public class AllureTestNg implements
private final AllureLifecycle lifecycle;
private final AllureTestNgTestFilter testFilter;

public AllureTestNg(final AllureLifecycle lifecycle, final AllureTestNgTestFilter testFilter) {
public AllureTestNg(final AllureLifecycle lifecycle,
final AllureTestNgTestFilter testFilter) {
this.lifecycle = lifecycle;
this.testFilter = testFilter;
}
Expand All @@ -154,15 +155,16 @@ public AllureTestNg(final AllureLifecycle lifecycle) {
}

public AllureTestNg() {
this(Allure.getLifecycle(), new AllureTestNgTestFilter());
this(Allure.getLifecycle());
}

public AllureLifecycle getLifecycle() {
return lifecycle;
}

@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
public List<IMethodInstance> intercept(final List<IMethodInstance> methods,
final ITestContext context) {
return testFilter.intercept(methods, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
import io.qameta.allure.testfilter.TestPlan;
import io.qameta.allure.testfilter.TestPlanUnknown;
import io.qameta.allure.testfilter.TestPlanV1_0;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.internal.ConstructorOrMethod;

import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class AllureTestNgTestFilter implements IMethodInterceptor {

private final TestPlan testPlan;
Expand All @@ -46,18 +46,23 @@ public AllureTestNgTestFilter(final TestPlan testPlan) {

@Override
public List<IMethodInstance> intercept(final List<IMethodInstance> methods, final ITestContext context) {
if (testPlan instanceof TestPlanV1_0) {
return methods.stream()
.filter(instance -> isSelected(instance.getMethod()))
.collect(Collectors.toList());
} else {
return methods;
}
return methods.stream()
.filter(this::isSelected)
.sorted(Comparator.comparing(
IMethodInstance::getMethod,
Comparator.nullsFirst(Comparator.comparingInt(
ITestNGMethod::getPriority
))))
.collect(Collectors.toList());
}

public boolean isSelected(final IMethodInstance instance) {
return isSelected(instance.getMethod());
}

public boolean isSelected(final ITestNGMethod instance) {
public boolean isSelected(final ITestNGMethod method) {
if (testPlan instanceof TestPlanV1_0) {
return isSelected(instance, (TestPlanV1_0) testPlan);
return isSelected(method, (TestPlanV1_0) testPlan);
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.qameta.allure.test.AllureResultsWriterStub;
import io.qameta.allure.testfilter.TestPlan;
import io.qameta.allure.testfilter.TestPlanV1_0;
import io.qameta.allure.testng.samples.PriorityTests;
import io.qameta.allure.testng.samples.TestsWithIdForFilter;
import org.assertj.core.api.Condition;
import org.assertj.core.groups.Tuple;
Expand All @@ -50,6 +51,7 @@
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -434,11 +436,20 @@ public void skippedSuiteTest() {
List<TestResultContainer> testContainers = results.getTestResultContainers();
assertThat(testResults)
.extracting(TestResult::getName, TestResult::getStatus)
.contains(
.containsExactlyInAnyOrder(
tuple("skippedTest", Status.SKIPPED),
tuple("skipSuite", Status.BROKEN),
tuple("testWithOneStep", Status.SKIPPED)
);
assertThat(testContainers).as("Unexpected quantity of testng containers has been written").hasSize(4);
assertThat(testContainers)
.as("Unexpected quantity of testng containers has been written")
.extracting(TestResultContainer::getName)
.containsExactlyInAnyOrder(
"Test tag 8",
"Test suite 8",
"io.qameta.allure.testng.samples.SkippedSuite",
"io.qameta.allure.testng.samples.TestsWithSteps"
);

assertThat(findTestContainerByName(results, "Test suite 8").getBefores())
.as("Before suite container should have a before method with one step")
Expand Down Expand Up @@ -677,7 +688,7 @@ public void ownerTest() {
.filter(label -> "owner".equals(label.getName()))
.map(Label::getValue)
.sorted()
.collect(Collectors.joining(",","[","]"))
.collect(Collectors.joining(",", "[", "]"))
)
.containsExactlyInAnyOrder(
tuple("io.qameta.allure.testng.samples.OwnerMethodTest.testWithOwner", "[charlie]"),
Expand Down Expand Up @@ -1141,8 +1152,6 @@ public Object[][] failedFixtures() {
@Test(dataProvider = "failedFixtures")
@AllureFeatures.Fixtures
public void shouldAddBeforeFixtureToFakeTestResult(final String suite, final String fixture) {
System.out.println(suite);
System.out.println(fixture);
final AllureResults results = runTestNgSuites(suite);
final Optional<TestResult> result = results.getTestResults().stream()
.filter(r -> r.getName().contains(fixture))
Expand All @@ -1156,6 +1165,19 @@ public void shouldAddBeforeFixtureToFakeTestResult(final String suite, final Str
.contains(result.get().getUuid());
}

@Test
@AllureFeatures.Ordering
public void shouldOrderTests() {
final AllureResults results = runTestPlan(null, PriorityTests.class);
final List<String> ordered = results.getTestResults().stream()
.sorted(Comparator.comparing(this::getOrderParameter))
.map(TestResult::getName)
.collect(Collectors.toList());
System.out.println(Arrays.toString(ordered.toArray()));
assertThat(ordered)
.containsExactly("zTest", "yTest", "xTest", "wTest", "vTest", "vTest");
}

@Step("Run testng suites {suites}")
private AllureResults runTestNgSuites(final Consumer<TestNG> configurer,
final String... suites) {
Expand Down Expand Up @@ -1338,7 +1360,7 @@ private static void checkTestJavadocDescriptions(List<TestResult> results, Strin

@Test
@AllureFeatures.Filtration
public void simpleFiltration() {
public void simpleFiltration() {
TestPlanV1_0 plan = new TestPlanV1_0().setTests(Arrays.asList(test1, test2, test3));
List<TestResult> testResults = runTestPlan(plan, TestsWithIdForFilter.class).getTestResults();

Expand All @@ -1354,7 +1376,7 @@ public void simpleFiltration() {

@Test
@AllureFeatures.Filtration
public void onlyId() {
public void onlyId() {
TestPlanV1_0 plan = new TestPlanV1_0().setTests(Arrays.asList(onlyId2, onlyId4));
List<TestResult> testResults = runTestPlan(plan, TestsWithIdForFilter.class).getTestResults();

Expand Down Expand Up @@ -1397,7 +1419,7 @@ public void skippedTest() {

@Test
@AllureFeatures.Filtration
public void correctIdIncorrectSelector() {
public void correctIdIncorrectSelector() {
TestPlanV1_0 plan = new TestPlanV1_0().setTests(
Arrays.asList(test1, test2, correctIdIncorrectSelector, correctIdIncorrectSelectorFailed)
);
Expand All @@ -1413,7 +1435,7 @@ public void correctIdIncorrectSelector() {
);
}

public AllureResultsWriterStub runTestPlan(TestPlan plan, final Class<?> ... testClasses) {
public AllureResultsWriterStub runTestPlan(TestPlan plan, final Class<?>... testClasses) {
final AllureResultsWriterStub results = new AllureResultsWriterStub();
final AllureLifecycle lifecycle = new AllureLifecycle(results);
final AllureTestNg adapter = new AllureTestNg(lifecycle, new AllureTestNgTestFilter(plan));
Expand All @@ -1437,4 +1459,13 @@ public AllureResultsWriterStub runTestPlan(TestPlan plan, final Class<?> ... tes
}
}

private Integer getOrderParameter(final TestResult result) {
return result.getParameters().stream()
.filter(p -> p.getName().equals("order"))
.map(Parameter::getValue)
.map(Integer::parseInt)
.findAny()
.orElse(0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 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.testng.samples;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.atomic.AtomicInteger;

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

public class PriorityTests {

private final static String ORDER_PARAMETER = "order";

private final AtomicInteger cnt = new AtomicInteger();

@DataProvider(name = "someProvider")
public static Object[][] someProvider() {
return new Object[][]{
new Object[]{1},
new Object[]{2}
};
}

@Test(dataProvider = "someProvider", priority = 4)
public void vTest(int parameter) {
parameter(ORDER_PARAMETER, cnt.incrementAndGet());
}

@Test(priority = 3)
public void wTest() {
parameter(ORDER_PARAMETER, cnt.incrementAndGet());
}

@Test(priority = 2)
public void xTest() {
parameter(ORDER_PARAMETER, cnt.incrementAndGet());
}

@Test(priority = 1)
public void yTest() {
parameter(ORDER_PARAMETER, cnt.incrementAndGet());
}

@Test
public void zTest() {
parameter(ORDER_PARAMETER, cnt.incrementAndGet());
}

}