From 56e53c48e4de75fc4074c48e1a60418756d40927 Mon Sep 17 00:00:00 2001 From: Sergei Baranov Date: Tue, 20 Dec 2022 19:29:05 -0700 Subject: [PATCH] Set XmlTest indexes after loading test suite from Yaml. Closes #2857 --- CHANGES.txt | 1 + .../src/main/java/org/testng/xml/XmlTest.java | 4 ++++ .../src/main/java/org/testng/internal/Yaml.java | 6 ++++-- testng-core/src/test/java/test/yaml/YamlTest.java | 14 ++++++++++++++ .../src/test/resources/yaml/testXmlTestIndex.yaml | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 testng-core/src/test/resources/yaml/testXmlTestIndex.yaml diff --git a/CHANGES.txt b/CHANGES.txt index 4ada37fc53..fe3e01606f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) diff --git a/testng-core-api/src/main/java/org/testng/xml/XmlTest.java b/testng-core-api/src/main/java/org/testng/xml/XmlTest.java index 43fcaa0aeb..ffaf1e3dc1 100644 --- a/testng-core-api/src/main/java/org/testng/xml/XmlTest.java +++ b/testng-core-api/src/main/java/org/testng/xml/XmlTest.java @@ -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; diff --git a/testng-core/src/main/java/org/testng/internal/Yaml.java b/testng-core/src/main/java/org/testng/internal/Yaml.java index ef5006e8b3..dc44cbe5fb 100644 --- a/testng-core/src/main/java/org/testng/internal/Yaml.java +++ b/testng-core/src/main/java/org/testng/internal/Yaml.java @@ -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++); } } diff --git a/testng-core/src/test/java/test/yaml/YamlTest.java b/testng-core/src/test/java/test/yaml/YamlTest.java index 92887ac1bf..e82fd9309c 100644 --- a/testng-core/src/test/java/test/yaml/YamlTest.java +++ b/testng-core/src/test/java/test/yaml/YamlTest.java @@ -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; @@ -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; @@ -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 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; } diff --git a/testng-core/src/test/resources/yaml/testXmlTestIndex.yaml b/testng-core/src/test/resources/yaml/testXmlTestIndex.yaml new file mode 100644 index 0000000000..11bda94ef7 --- /dev/null +++ b/testng-core/src/test/resources/yaml/testXmlTestIndex.yaml @@ -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