Skip to content

Commit 692d013

Browse files
authored
#2974: Add overrideGroupsFromCliInParentChildXml test (#2975)
1 parent 91a003a commit 692d013

File tree

8 files changed

+131
-9
lines changed

8 files changed

+131
-9
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Current
2+
Fixed: GITHUB-2974: Command line arguments -groups and -excludegroups override defined groups in a suite xml file (dr29bart)
23
Fixed: GITHUB-2961: "Unexpected value: 16" error when multiple beforeMethod config methods with firstTimeOnly property run before a test (Krishnan Mahadevan)
34
Fixed: GITHUB-2904: Add location of docs Github to readme and contributions page (Mohsin Sackeer)
45
Fixed: GITHUB-2934: Parallel Dataproviders & retries causes test result count to be skewed (Krishnan Mahadevan)

testng-core-api/src/main/java/org/testng/xml/XmlSuite.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testng.xml;
22

33
import java.util.Collection;
4+
import java.util.Collections;
45
import java.util.List;
56
import java.util.Locale;
67
import java.util.Map;
@@ -189,8 +190,6 @@ public String toString() {
189190
public static final Boolean DEFAULT_PRESERVE_ORDER = Boolean.TRUE;
190191
private Boolean m_preserveOrder = DEFAULT_PRESERVE_ORDER;
191192

192-
private List<String> m_includedGroups = Lists.newArrayList();
193-
private List<String> m_excludedGroups = Lists.newArrayList();
194193
private XmlMethodSelectors m_xmlMethodSelectors;
195194
private boolean parsed = false;
196195

@@ -848,23 +847,39 @@ public List<String> getIncludedGroups() {
848847
} else if (m_xmlGroups != null && (m_xmlGroups.getRun() != null)) {
849848
return m_xmlGroups.getRun().getIncludes();
850849
} else {
851-
// deprecated
852-
return m_includedGroups;
850+
// deprecated. Use mutable list because there are unit tests which modifies it
851+
return Lists.newArrayList();
852+
}
853+
}
854+
855+
private void initGroupsRun() {
856+
if (m_xmlGroups == null) {
857+
m_xmlGroups = new XmlGroups();
858+
}
859+
if (m_xmlGroups.getRun() == null) {
860+
m_xmlGroups.setRun(new XmlRun());
853861
}
854862
}
855863

856864
public void addIncludedGroup(String g) {
857-
m_includedGroups.add(g);
865+
initGroupsRun();
866+
m_xmlGroups.getRun().onInclude(g);
858867
}
859868

860869
/** @param g - The list of groups to include. */
861870
public void setIncludedGroups(List<String> g) {
862-
m_includedGroups = g;
871+
initGroupsRun();
872+
List<String> includes = m_xmlGroups.getRun().getIncludes();
873+
includes.clear();
874+
includes.addAll(g);
863875
}
864876

865877
/** @param g The excludedGrousps to set. */
866878
public void setExcludedGroups(List<String> g) {
867-
m_excludedGroups = g;
879+
initGroupsRun();
880+
List<String> excludes = m_xmlGroups.getRun().getExcludes();
881+
excludes.clear();
882+
excludes.addAll(g);
868883
}
869884

870885
/**
@@ -877,12 +892,13 @@ public List<String> getExcludedGroups() {
877892
} else if (m_xmlGroups != null && (m_xmlGroups.getRun() != null)) {
878893
return m_xmlGroups.getRun().getExcludes();
879894
} else {
880-
return m_excludedGroups;
895+
return Collections.emptyList();
881896
}
882897
}
883898

884899
public void addExcludedGroup(String g) {
885-
m_excludedGroups.add(g);
900+
initGroupsRun();
901+
m_xmlGroups.getRun().onExclude(g);
886902
}
887903

888904
public Boolean getGroupByInstances() {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package test.cli.github2974;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import org.testng.Assert;
6+
import org.testng.CommandLineArgs;
7+
import org.testng.ITestListener;
8+
import org.testng.ITestResult;
9+
import org.testng.TestNG;
10+
import org.testng.annotations.Test;
11+
import test.SimpleBaseTest;
12+
13+
public class OverrideGroupsCliTest extends SimpleBaseTest {
14+
15+
private static class NameCollector implements ITestListener {
16+
List<String> names = new ArrayList<>();
17+
18+
@Override
19+
public void onTestSuccess(ITestResult result) {
20+
names.add(result.getName());
21+
}
22+
}
23+
24+
@Test(description = "GITHUB-2974")
25+
public void overrideIncludeGroupsFromCliInParentChildXml() {
26+
TestNG testNG =
27+
new TestNG(false) {
28+
{
29+
CommandLineArgs args = new CommandLineArgs();
30+
args.groups = "override_group";
31+
args.suiteFiles = List.of(getPathToResource("2974/parent_include.xml"));
32+
configure(args);
33+
}
34+
};
35+
NameCollector collector = new NameCollector();
36+
testNG.addListener(collector);
37+
testNG.run();
38+
Assert.assertTrue(collector.names.contains("overrideTest"));
39+
Assert.assertFalse(collector.names.contains("defaultTest"));
40+
}
41+
42+
@Test(description = "GITHUB-2974")
43+
public void overrideExcludeGroupsFromCliInParentChildXml() {
44+
TestNG testNG =
45+
new TestNG(false) {
46+
{
47+
CommandLineArgs args = new CommandLineArgs();
48+
args.excludedGroups = "override_group";
49+
args.suiteFiles = List.of(getPathToResource("2974/parent_exclude.xml"));
50+
configure(args);
51+
}
52+
};
53+
NameCollector collector = new NameCollector();
54+
testNG.addListener(collector);
55+
testNG.run();
56+
Assert.assertTrue(collector.names.contains("defaultTest"));
57+
Assert.assertFalse(collector.names.contains("overrideTest"));
58+
}
59+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.cli.github2974;
2+
3+
import org.testng.annotations.Test;
4+
5+
public class TwoGroupsTest {
6+
7+
@Test(groups = "override_group")
8+
public void overrideTest() {}
9+
10+
@Test(groups = "default_group")
11+
public void defaultTest() {}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
2+
<suite name="child">
3+
4+
<test name="Tests inherit groups from parent xml">
5+
<classes>
6+
<class name="test.cli.github2974.TwoGroupsTest"/>
7+
</classes>
8+
</test>
9+
</suite>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
2+
<suite name="parent suite with default group" parallel="methods" thread-count="1">
3+
4+
<groups>
5+
<run>
6+
<exclude name="default_group"/>
7+
</run>
8+
</groups>
9+
<suite-files>
10+
<suite-file path="child.xml"/>
11+
</suite-files>
12+
</suite>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
2+
<suite name="parent suite with default group" parallel="methods" thread-count="1">
3+
4+
<groups>
5+
<run>
6+
<include name="default_group"/>
7+
</run>
8+
</groups>
9+
<suite-files>
10+
<suite-file path="child.xml"/>
11+
</suite-files>
12+
</suite>

testng-core/src/test/resources/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<class name="test.github765.ExcludeSyntheticMethodsFromTemplateCallsTest"/>
110110
<class name="test.github1405.TestExclusionOfMainMethod"/>
111111
<class name="test.cli.CliTest"/>
112+
<class name="test.cli.github2974.OverrideGroupsCliTest"/>
112113
<class name="test.thread.ParallelSuiteTest"/>
113114
<class name="test.simple.IncludedExcludedTest" />
114115
<class name="test.reports.ReportTest" />

0 commit comments

Comments
 (0)