-
Notifications
You must be signed in to change notification settings - Fork 1k
This closes #863 Same xmlsuite is added multiple times when tests are run using TestNG.SetXmlSuites method #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
d627682
18f523b
3dd5f5c
5e010aa
cc5b523
d3712b1
105f3bf
725e888
33e5c55
183b26e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,7 @@ | |
| /** | ||
| * This class is the main entry point for running tests in the TestNG framework. | ||
| * Users can create their own TestNG object and invoke it in many different | ||
| * ways: | ||
| * ways | ||
| * <ul> | ||
| * <li>On an existing testng.xml | ||
| * <li>On a synthetic testng.xml, created entirely from Java | ||
|
|
@@ -269,28 +269,33 @@ public void setXmlPathInJar(String xmlPathInJar) { | |
| public void initializeSuitesAndJarFile() { | ||
| // The Eclipse plug-in (RemoteTestNG) might have invoked this method already | ||
| // so don't initialize suites twice. | ||
| System.out.println("ENTER"); | ||
| if (m_isInitialized) { | ||
| return; | ||
| } | ||
|
|
||
| m_isInitialized = true; | ||
| if (m_suites.size() > 0) { | ||
| //to parse the suite files (<suite-file>), if any | ||
| for (XmlSuite s: m_suites) { | ||
| for (String suiteFile : s.getSuiteFiles()) { | ||
| Path rootPath = Paths.get(s.getFileName()).getParent(); | ||
| try { | ||
| Collection<XmlSuite> childSuites = getParser(rootPath.resolve(suiteFile).normalize().toString()).parse(); | ||
| for (XmlSuite cSuite : childSuites){ | ||
| cSuite.setParentSuite(s); | ||
| s.getChildSuites().add(cSuite); | ||
| } | ||
| //to parse the suite files (<suite-file>), if any | ||
| for (XmlSuite s: m_suites) { | ||
| for (String suiteFile : s.getSuiteFiles()) { | ||
| Path suitePath = Paths.get(s.getFileName()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s.getFileName() can return null in surefire plugin suites. By putting it here we will avoid the surefire plugin case and avoid a null check.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Are we agree it is a surefire issue? Do you have an example ? |
||
| Path rootPath = suitePath.getParent(); | ||
| try { | ||
| Collection<XmlSuite> childSuites = getParser(rootPath.resolve(suiteFile).normalize().toString()).parse(); | ||
| for (XmlSuite cSuite : childSuites){ | ||
| Path childSuite = Paths.get(cSuite.getFileName()).normalize(); | ||
| Path parentSuite = Paths.get(suiteFile).normalize(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paths.get(suiteFile).normalize() will get it from the project dir and rootPath.resolve(suiteFile).normalize() will first append the suiteFIle path and the normalize will remove the extra path identifiers. For ex, some users may write .\testng.xml and some may write \tesng.xml.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Could you provide an example where |
||
| if(!childSuite.getFileName().equals(parentSuite.getFileName())) | ||
| { | ||
| cSuite.setParentSuite(s); | ||
| s.getChildSuites().add(cSuite); | ||
| } | ||
| } | ||
| } catch (ParserConfigurationException | IOException | SAXException e) { | ||
| e.printStackTrace(System.out); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1012,13 +1017,13 @@ private void checkSuiteNames(List<XmlSuite> suites) { | |
| private void checkSuiteNamesInternal(List<XmlSuite> suites, Set<String> names) { | ||
| for (XmlSuite suite : suites) { | ||
| final String name = suite.getName(); | ||
|
|
||
| int count = 0; | ||
|
|
||
| String tmpName = name; | ||
| while (names.contains(tmpName)) { | ||
| tmpName = name + " (" + count++ + ")"; | ||
| } | ||
|
|
||
| if (count > 0) { | ||
| suite.setName(tmpName); | ||
| names.add(tmpName); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,16 @@ | |
| import org.testng.xml.XmlSuite; | ||
| import org.testng.xml.XmlTest; | ||
| import org.xml.sax.SAXException; | ||
|
|
||
| import test.SimpleBaseTest; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import javax.xml.parsers.ParserConfigurationException; | ||
|
|
||
| public class CheckSuiteNamesTest extends SimpleBaseTest { | ||
|
|
@@ -81,4 +88,26 @@ public void checkXmlSuiteAddition() throws ParserConfigurationException, SAXExce | |
| tng.setXmlSuites(parser.parseToList()); | ||
| tng.initializeSuitesAndJarFile(); | ||
| } | ||
|
|
||
| @Test(description = "Verify that same suite is not added multiple times") | ||
| public void validateDuplicateSuiteAddition() throws ParserConfigurationException, SAXException, IOException | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for #829 too? We should check that it is still possible to add the same child suite many times.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same child suite will point to same tests, same tests will fail the check in checkTestNames this method. That is the confusion I have. I am seeing the exception about same test name for two tests in one suite. Lets deal with this in a seperate issue. I will open one for this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me pick this up as a part of next issue. I will log one after understanding the problem in details. |
||
| { | ||
| SuiteListner suiteListner = new SuiteListner(); | ||
| TestNG tng = create(); | ||
| String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml"); | ||
| Parser parser = new Parser(testngXmlPath); | ||
| tng.setXmlSuites(parser.parseToList()); | ||
| tng.addListener(suiteListner); | ||
| tng.run(); | ||
|
|
||
| Set<String> allSuite = new HashSet<String>(); | ||
| List<XmlSuite> ranSuites = suiteListner.getAllTestSuite(); | ||
|
|
||
| Assert.assertEquals(ranSuites.size(), 3, "Correct number of suites ran"); | ||
| for(XmlSuite suite : ranSuites) | ||
| { | ||
| Assert.assertTrue(allSuite.add(suite.getFileName()), | ||
| String.format("No duplicate of suite %s added", suite.getFileName())); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package test.sanitycheck; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.testng.ISuite; | ||
| import org.testng.ISuiteListener; | ||
| import org.testng.xml.XmlSuite; | ||
|
|
||
| public class SuiteListner implements ISuiteListener { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: SuiteListener |
||
|
|
||
| List<XmlSuite> allSuite = new ArrayList<XmlSuite>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Private should be the right modifier. In Java its public by default. I think final is not necessary because we wont be changing the reference of the array list to any new array list.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree, but |
||
|
|
||
| @Override | ||
| public void onStart(ISuite suite) { | ||
| allSuite.add(suite.getXmlSuite()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFinish(ISuite suite) { | ||
|
|
||
| } | ||
|
|
||
| public List<XmlSuite> getAllTestSuite(){ | ||
| return allSuite; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I think this line is not expected ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New to Java and git :(, just fixed it up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) You can try to squash all your commits into only one with git interactive rebase. It's for advanced git users but it's really useful.