Skip to content

Commit

Permalink
methods that are excluded by a "groups" filter must not be included i…
Browse files Browse the repository at this point in the history
…n the report (fixes allure-framework#880)
  • Loading branch information
martoe authored and wuhuizuo committed Apr 1, 2017
1 parent e302468 commit 180d923
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.yandex.qatools.allure.testng;

import static java.util.Arrays.asList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ISuite;
Expand Down Expand Up @@ -258,7 +260,7 @@ private String getCurrentSuitePrefix(ITestResult iTestResult) {

private void addPendingMethods(ITestContext iTestContext) {
for (ITestNGMethod method : iTestContext.getExcludedMethods()) {
if (method.isTest() && !method.getEnabled()) {
if (method.isTest() && !method.getEnabled() && isInActiveGroup(method, iTestContext)) {
Description description = new Description().withValue(method.getDescription());
String suiteUid = getSuiteUid(iTestContext);
TestCaseStartedEvent event = new TestCaseStartedEvent(suiteUid, method.getMethodName());
Expand All @@ -276,6 +278,55 @@ private void addPendingMethods(ITestContext iTestContext) {
}
}

/**
* Checks if the given test method is part of the current test run in matters of the include/exclude group filtering
*/
private boolean isInActiveGroup(ITestNGMethod method, ITestContext context) {
String[] includedGroupsArray = context.getIncludedGroups();
List<String> includeGroups = includedGroupsArray != null ? asList(includedGroupsArray) : Collections.<String>emptyList();
String[] excludedGroupsArray = context.getExcludedGroups();
List<String> excludeGroups = excludedGroupsArray != null ? asList(excludedGroupsArray) : Collections.<String>emptyList();
if (includeGroups.isEmpty()) {
if (excludeGroups.isEmpty()) {
return true; // no group restriction
} else {
return isInActiveGroupWithoutIncludes(method, excludeGroups);
}
} else {
return isInActiveGroupWithIncludes(method, includeGroups, excludeGroups);
}
}

private boolean isInActiveGroupWithoutIncludes(ITestNGMethod method, List<String> excludeGroups) {
if (method.getGroups() != null) {
for (String group : method.getGroups()) {
if (excludeGroups.contains(group)) {
return false; // a group of the method is excluded
}
}
}
return true; // no groups of the method are excluded
}

private boolean isInActiveGroupWithIncludes(ITestNGMethod method, List<String> includeGroups, List<String> excludeGroups) {
if (method.getGroups() != null) {
boolean included = false;
boolean excluded = false;
for (String group : method.getGroups()) {
if (includeGroups.contains(group)) {
included = true;
}
if (excludeGroups.contains(group)) {
excluded = true;
}
}
if (included && !excluded) {
return true; // a group of the method is included and not excluded
}
}
return false; // no groups of the method are included
}

private String getMethodInvocationsAndSuccessPercentage(ITestResult iTestResult) {
int percentage = iTestResult.getMethod().getSuccessPercentage();
int curCount = iTestResult.getMethod().getCurrentInvocationCount() + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.yandex.qatools.allure.testng;

import static java.util.Collections.*;
import static javax.xml.bind.JAXB.unmarshal;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static ru.yandex.qatools.allure.commons.AllureFileUtils.*;

import java.io.*;
import java.util.*;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.testng.TestNG;
import ru.yandex.qatools.allure.model.*;
import ru.yandex.qatools.allure.utils.AllureResultsUtils;

public class AllureTestListenerGroupsTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
private File resultsDir;

@Before
public void setUp() throws IOException {
resultsDir = folder.newFolder();
AllureResultsUtils.setResultsDirectory(resultsDir);
}

@Test // see https://github.com/allure-framework/allure-core/issues/880
public void reportContainsTestForGroups() {
// GIVEN: an TestNG suite with groups
TestNG testNG = new TestNG(false);
testNG.setTestSuites(singletonList(getClass().getClassLoader().getResource("suite-groups.xml").getFile()));

// WHEN: executing
testNG.run();

// THEN: report only contains results for included groups
List<File> files = listTestSuiteFiles(resultsDir);
assertThat(files, hasSize(1));
File file = files.get(0);
TestSuiteResult result = unmarshal(file, TestSuiteResult.class);
assertThat(result.getTestCases(), hasSize(2));
List<String> status = new ArrayList<>();
for (TestCaseResult test : result.getTestCases()) {
status.add(test.getName() + ":" + test.getStatus());
}
assertThat(status, containsInAnyOrder("inactiveIncludedTest:PENDING", "activeIncludedTest:PASSED"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.yandex.qatools.allure.testng.testdata;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.testng.Assert.fail;

import org.testng.annotations.*;

public class GroupTest {

@Test
public void activeTest() { }

@Test(enabled = false)
public void inactiveTest() { }

@Test(groups = "include")
public void activeIncludedTest() { }

@Test(groups = "include", enabled = false)
public void inactiveIncludedTest() { }

@Test(groups = "exclude")
public void activeExcludedTest() { }

@Test(groups = "exclude", enabled = false)
public void inactiveExcludedTest() { }

@Test(groups = {"include", "exclude"})
public void activeIncludedExcludedTest() { }

@Test(groups = {"include", "exclude"}, enabled = false)
public void inactiveIncludedExcludedTest() { }
}
15 changes: 15 additions & 0 deletions allure-testng-adaptor/src/test/resources/suite-groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test suite with groups">
<test name="Test with groups">
<groups>
<run>
<include name="include"/>
<exclude name="exclude"/>
</run>
</groups>
<classes>
<class name="ru.yandex.qatools.allure.testng.testdata.GroupTest"/>
</classes>
</test>
</suite>

0 comments on commit 180d923

Please sign in to comment.