Skip to content

Commit

Permalink
Set XmlTest indexes after loading test suite from Yaml.
Browse files Browse the repository at this point in the history
Closes #2857
  • Loading branch information
Sergei Baranov authored and krmahadevan committed Jan 3, 2023
1 parent f27a7ea commit f6975f2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Current
Fixed: GITHUB-2857: XmlTest index is not set for test suites invoked with YAML

7.7.1
Fixed: GITHUB-2854: overloaded assertEquals methods do not work from Groovy (Krishnan Mahadevan)
Expand Down
4 changes: 4 additions & 0 deletions testng-core-api/src/main/java/org/testng/xml/XmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ public int getIndex() {
return m_index;
}

public void setIndex(int index) {
m_index = index;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
6 changes: 4 additions & 2 deletions testng-core/src/main/java/org/testng/internal/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ public static XmlSuite parse(String filePath, InputStream is, boolean loadClasse
result.setFileName(filePath);

// Adjust XmlTest parents and indices
int testIndex = 0;
for (XmlTest t : result.getTests()) {
t.setIndex(testIndex++);
t.setSuite(result);
int index = 0;
int classIndex = 0;
for (XmlClass c : t.getClasses()) {
c.setIndex(index++);
c.setIndex(classIndex++);
}
}

Expand Down
14 changes: 14 additions & 0 deletions testng-core/src/test/java/test/yaml/YamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.testng.Assert;
Expand All @@ -18,6 +19,7 @@
import org.testng.internal.YamlParser;
import org.testng.xml.SuiteXmlParser;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import org.testng.xml.internal.Parser;
import test.SimpleBaseTest;

Expand Down Expand Up @@ -100,6 +102,18 @@ public void testLoadClassesFlag() throws IOException {
}
}

@Test(description = "GITHUB-2857")
public void testXmlTestIndex() throws IOException {
YamlParser yamlParser = new YamlParser();
String yamlSuiteFile = "src/test/resources/yaml/testXmlTestIndex.yaml";
XmlSuite suite = yamlParser.parse(yamlSuiteFile, new FileInputStream(yamlSuiteFile), false);
List<XmlTest> tests = suite.getTests();
assertThat(tests.size()).isEqualTo(3);
for (int i = 0; i < tests.size(); i++) {
assertThat(tests.get(i).getIndex()).isEqualTo(i);
}
}

private Throwable getRootCause(Throwable throwable) {
return throwable.getCause() != null ? getRootCause(throwable.getCause()) : throwable;
}
Expand Down
14 changes: 14 additions & 0 deletions testng-core/src/test/resources/yaml/testXmlTestIndex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: SuiteYaml
tests:
- name: TestYaml-1
parameters: { input: yaml_1 }
classes:
- test.TestNGTest
- name: TestYaml-2
parameters: { input: yaml_2 }
classes:
- test.TestNGTest
- name: TestYaml-3
parameters: { input: yaml_3 }
classes:
- test.TestNGTest

0 comments on commit f6975f2

Please sign in to comment.