Skip to content

Commit

Permalink
Fix for issue testng-team#969. Add parameters test for failed reporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ningzhangnj committed Feb 4, 2016
1 parent a94277a commit fcfc6fa
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Fixed: GITHUB-419: parallel mode was ignored with command line (@khospodarysko &
New: GITHUB-932: Deprecate true/false parallel values. none is the new default value. (Julien Herr)
Fixed: GITHUB-960: testng-failed.xml gets generated even when there are no failures. (Krishnan Mahadevan)
Fixed: GITHUB-895: Changing status of test by setStatus of ITestResult (Raj Srivastava & Julien Herr)
Fixed: GITHUB-969: testng-failed.xml does not carry over the parameters of methods from origin suite xml(Ning Zhang)

6.9.10:
2015/12/15
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.1</version>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/org/testng/reporters/FailedReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.testng.xml.XmlTest;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -217,8 +218,10 @@ private List<XmlClass> createXmlClasses(List<ITestNGMethod> methods, XmlTest src
List<XmlInclude> methodNames= Lists.newArrayList(methodList.size());
int ind = 0;
for(ITestNGMethod m: methodList) {
methodNames.add(new XmlInclude(m.getMethod().getName(), m.getFailedInvocationNumbers(),
ind++));
XmlInclude methodName = new XmlInclude(m.getMethod().getName(), m.getFailedInvocationNumbers(),
ind++);
methodName.setParameters(findMethodLocalParameters(srcXmlTest, m));
methodNames.add(methodName);
}
xmlClass.setIncludedMethods(methodNames);
xmlClass.setParameters(parameters);
Expand All @@ -229,6 +232,28 @@ private List<XmlClass> createXmlClasses(List<ITestNGMethod> methods, XmlTest src
return result;
}

/**
* Get local parameters of one include method from origin test xml.
* @param srcXmlTest
* @param method the method we want to find its parameters
* @return local parameters belong to one test method.
*/
private static Map<String, String> findMethodLocalParameters(XmlTest srcXmlTest, ITestNGMethod method) {
Class clazz = method.getRealClass();

for (XmlClass c : srcXmlTest.getClasses()) {
if (clazz == c.getSupportClass()) {
for (XmlInclude xmlInclude : c.getIncludedMethods()) {
if (xmlInclude.getName().equals(method.getMethodName())) {
return xmlInclude.getLocalParameters();
}
}
}
}

return Collections.emptyMap();
}

/**
* TODO: we might want to make that more flexible in the future, but for
* now, hardcode the file name
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/testng/xml/XmlInclude.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public void setDescription(String description) {
m_description = description;
}

public void setParameters(Map<String, String> parameters) {
m_parameters.clear();
m_parameters.putAll(parameters);
}

public String getDescription() {
return m_description;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package test.failedreporter;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import test.BaseTest;

public class FailedReporterParametersTest extends BaseTest {
private File mTempDirectory;

@BeforeMethod
public void setUp() {
File slashTmpDir = new File(System.getProperty("java.io.tmpdir"));
mTempDirectory = new File(slashTmpDir, "testng-tmp-" + System.currentTimeMillis() % 1000);
mTempDirectory.mkdirs();
mTempDirectory.deleteOnExit();
}

@AfterMethod
public void tearDown() {
deleteDir(mTempDirectory);
}

@Test
public void failedSuiteShouldHaveParameters() throws IOException {
Map<String, String> suiteParams = Maps.newHashMap();
Map<String, String> testParams = Maps.newHashMap();
Map<String, String> classParams = Maps.newHashMap();
Map<String, String> methodParams = Maps.newHashMap();
suiteParams.put("suiteParam", "suiteParamValue");
//In testng-failed.xml, suite will have both origin suite parameters and children tests parameters.
testParams.put("testParam", "testParamValue");
classParams.put("classParam", "classParamValue");
methodParams.put("methodParam", "methodParamValue");

TestNG tng = new TestNG();
XmlSuite suite = new XmlSuite();
suite.setParameters(suiteParams);
tng.setXmlSuites(Lists.newArrayList(suite));
XmlTest test = new XmlTest(suite);
XmlClass clazz = new XmlClass();
XmlInclude includeMethod = new XmlInclude("f2");
test.setParameters(testParams);
test.setXmlClasses(Lists.newArrayList(clazz));
clazz.setParameters(classParams);
clazz.setXmlTest(test);
clazz.setIncludedMethods(Lists.newArrayList(includeMethod));
clazz.setClass(FailedReporterSampleTest.class);
includeMethod.setParameters(methodParams);
includeMethod.setXmlClass(clazz);
tng.setVerbose(0);
tng.setOutputDirectory(mTempDirectory.getAbsolutePath());
tng.run();

runAssertions(mTempDirectory, "<parameter name=\"%s\" value=\"%s\"/>",
new String[] {"suiteParam", "testParam", "classParam", "methodParam"});

}

public static void runAssertions(File outputDir, String expectedFormat, String[] expectedKeys) {
File failed = new File(outputDir, "testng-failed.xml");
for (String expectedKey : expectedKeys) {
List<String> resultLines = Lists.newArrayList();
grep(failed, String.format(expectedFormat, expectedKey, expectedKey + "Value"), resultLines);
int expectedSize = 1;
if ("testParam".equals(expectedKey)) {
expectedSize = 2;
}
Assert.assertEquals(resultLines.size(), expectedSize, "Mismatch param:" + expectedKey);
}
}
}
1 change: 1 addition & 0 deletions src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<class name="test.parameters.ParameterOverrideTest" />
<class name="test.reports.FailedReporterTest" />
<class name="test.failedreporter.FailedReporterScenariosTest"/>
<class name="test.failedreporter.FailedReporterParametersTest"/>
<class name="test.reports.ReporterLogTest" />
<class name="test.testng387.TestNG387"/>
</classes>
Expand Down

0 comments on commit fcfc6fa

Please sign in to comment.