Skip to content

Commit 60e9946

Browse files
committed
Dont honour params specified in suite-file tag
Closes #581 TestNG does not have any logic that would honour parameter tags specified within suite-files tag. But due to a parsing bug, we end up reading parameters that were specified within `<suite-file>` tag. This tag by definition is NOT meant to have any child tags inside of it. Fixed this discrepancy by adding a warning when this anomaly is detected and skipping of reading the `<parameter>` tag inside it as if it were specified within `<suite>` tag.
1 parent d6767b5 commit 60e9946

File tree

7 files changed

+70
-0
lines changed

7 files changed

+70
-0
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Current
22

3+
Fixed: GITHUB-581: Parameters of nested test suites are overridden(Krishnan Mahadevan)
34
Fixed: GITHUB-727 : Fixing data races (Krishnan Mahadevan)
45
Fixed: GITHUB-2913: Maps containing nulls can be incorrectly considered equal (Alex Heneveld)
56

testng-core/src/main/java/org/testng/xml/TestNGContentHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ enum Location {
101101
}
102102

103103
private final Stack<Location> m_locations = new Stack<>();
104+
private boolean isSuiteFileTag = false;
104105

105106
private XmlClass m_currentClass = null;
106107
private ArrayList<XmlInclude> m_currentIncludedMethods = null;
@@ -196,9 +197,11 @@ private void xmlSuiteFile(boolean start, Attributes attributes) {
196197
String path = attributes.getValue("path");
197198
pushLocation(Location.SUITE);
198199
m_suiteFiles.add(path);
200+
isSuiteFileTag = true;
199201
} else {
200202
m_currentSuite.setSuiteFiles(m_suiteFiles);
201203
popLocation();
204+
isSuiteFileTag = false;
202205
}
203206
}
204207

@@ -611,6 +614,13 @@ public void startElement(String uri, String localName, String qName, Attributes
611614
} else if ("exclude".equals(qName)) {
612615
xmlExclude(true, attributes);
613616
} else if ("parameter".equals(qName)) {
617+
if (isSuiteFileTag) {
618+
// do-not process a <parameter> tag when it is specified inside <suite-files>
619+
Logger.getLogger(getClass())
620+
.warn(
621+
"Ignoring the <parameter> tag because it is specified inside a <suite-file> tag.");
622+
return;
623+
}
614624
String value = expandValue(attributes.getValue("value"));
615625
Location location = m_locations.peek();
616626
switch (location) {

testng-core/src/test/java/test/parameters/ParameterTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.util.Collections;
56
import java.util.HashMap;
67
import java.util.List;
78
import java.util.Map;
9+
import org.testng.ISuite;
10+
import org.testng.ISuiteListener;
811
import org.testng.ITestResult;
912
import org.testng.TestListenerAdapter;
1013
import org.testng.TestNG;
@@ -131,4 +134,21 @@ public void testNativeInjectionAndParamsInjection() {
131134
testng.run();
132135
assertThat(listener.getPassedTests().isEmpty()).isFalse();
133136
}
137+
138+
@Test(description = "GITHUB-581")
139+
public void ensureParametersSpecifiedInsideSuiteFilesTagAreIgnored() {
140+
TestNG testng = create();
141+
testng.setTestSuites(
142+
Collections.singletonList("src/test/resources/parametertest/issue_581/parent_suite.xml"));
143+
Map<String, Map<String, String>> actualParameters = new HashMap<>();
144+
testng.addListener(
145+
new ISuiteListener() {
146+
@Override
147+
public void onStart(ISuite suite) {
148+
actualParameters.put(suite.getName(), suite.getXmlSuite().getAllParameters());
149+
}
150+
});
151+
testng.run();
152+
actualParameters.values().forEach(each -> assertThat(each).isEmpty());
153+
}
134154
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.parameters.issue581;
2+
3+
import org.testng.annotations.Optional;
4+
import org.testng.annotations.Parameters;
5+
import org.testng.annotations.Test;
6+
7+
public class TestClassSample {
8+
9+
@Parameters(value = "url")
10+
@Test
11+
public void test(@Optional String ignored) {}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
2+
<suite name="parent_suite">
3+
<suite-files>
4+
<suite-file path="suite_one.xml">
5+
<parameter name="url" value="http://www.google.com"/>
6+
</suite-file>
7+
<suite-file path="suite_two.xml">
8+
<parameter name="url" value="http://www.bing.com"/>
9+
</suite-file>
10+
</suite-files>
11+
</suite>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
2+
<suite name="suite_one">
3+
<test name="test_one">
4+
<classes>
5+
<class name="test.parameters.issue581.TestClassSample"/>
6+
</classes>
7+
</test>
8+
</suite>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
2+
<suite name="suite_two">
3+
<test name="test_two">
4+
<classes>
5+
<class name="test.parameters.issue581.TestClassSample"/>
6+
</classes>
7+
</test>
8+
</suite>

0 commit comments

Comments
 (0)