Reputation: 229
What I want to do
I'm trying to generate a custom test report, which displays a custom test name. I do this by implementing "the ITest strategy" where you override the getTestName()
method to provide your own.
In my report, I have the method name (from .getMethodName()
, for example testIfStatusCodeIs200
) and the test name (from .getTestName()
, which I customise, and returns for example Tested endpoint is: http://cakeAPI/api/cakes/cakeId
).
How the code looks like
public class BaseTest implements ITest {
private String uri = null;
public BaseTest(String uri) {
this.uri = uri;
}
@Test (groups = { "myGroup" })
public void test1() {
// do something
}
@Test (dependsOnGroups = { "myGroup"})
public void test2() {
// do something
}
public String getTestName() {
return uri;
}
}
This is the report generator logic. This is a pretty big function and most of it consists in appending and writing to an HTML file, so I voluntarily cut out pieces that I believe are non important for the problem:
public class CustomTestNGReporter implements IReporter {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
try {
// Create test methods summary data.
String customTestMethodSummary = this.getTestMehodSummary(suites);
//... Append results to html
} catch (Exception ex) {
ex.printStackTrace();
}
}
/* Get test method summary info. */
private String getTestMehodSummary(List<ISuite> suites) {
try {
for (ISuite tempSuite : suites) {
Map<String, ISuiteResult> testResults = tempSuite.getResults();
for (ISuiteResult result : testResults.values()) {
ITestContext testObj = result.getTestContext();
/* Get failed test method related data. */
IResultMap testFailedResult = testObj.getFailedTests();
String failedTestMethodInfo = this.getTestMethodReport(testFailedResult);
/* Get skipped test method related data. */
IResultMap testSkippedResult = testObj.getSkippedTests();
String skippedTestMethodInfo = this.getTestMethodReport(testSkippedResult);
/* Get passed test method related data. */
IResultMap testPassedResult = testObj.getPassedTests();
String passedTestMethodInfo = this.getTestMethodReport(testPassedResult);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/* Get failed, passed or skipped test methods report. */
private String getTestMethodReport(IResultMap testResultMap) {
Set<ITestResult> testResultSet = testResultMap.getAllResults();
for (ITestResult testResult : testResultSet) {
String testURI = testResult.getTestName();
System.out.println(testURI); // null for skipped tests
}
return null;
}
What is the problem
The problem is that if test1()
fails, then test2()
is skipped, and it seems that in this case getTestName()
for test2
returns null
.
For example, if the test of testIfStatusCodeIs200
fails, then I will skip testIfResponseHas10Elements
. The test name associated with testIfStatusCodeIs200
will return the custom test name, while the test name of testIfResponseHas10Elements
will return null
.
Is there a way to 'force' getTestName()
to be evaluated even though the test has been skipped ?
Upvotes: 1
Views: 860
Reputation: 14746
If you make use of the latest released version of TestNG (6.14.3) you will not see any null values. You will instead at-least get the actual @Test
annotated method name.
I have filed a bug for ensuring that if there's an ITest
implementation available in the test class, then TestNG should always retrieve that even for skipped methods as well.
You can track the issue here: https://github.com/cbeust/testng/issues/1756
The fix should be available in the upcoming version of TestNG viz., 7.0.0
You can add yourself as a watcher to the above mentioned issue to track it.
Upvotes: 2