如何使用 IReporter 在 selenium 中获取失败的测试详细信息? [英] How to get failed tests details in selenium using IReporter?
问题描述
我有这个代码:
int totalTestsCount = 0;
int totalPassedTests = 0;
int totalSkippedTests = 0;
int totalFailedTests = 0;
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
int passedTests = tc.getPassedTests().getAllResults().size();
int skippedTests = tc.getSkippedTests().getAllResults().size();
int failedTests = tc.getFailedTests().getAllResults().size();
totalTestsCount += passedTests + skippedTests + failedTests;
totalPassedTests += passedTests;
totalSkippedTests += skippedTests;
totalFailedTests += failedTests;
}
}
但现在我想获取有关每个失败测试的详细信息,例如:带有测试方法名称和异常的测试参数.我该怎么做?
But now I want to get details about each failed test, for example: the test parameters with test method name and the exception. How I can do that?
推荐答案
根据 testng 文档 https://javadoc.io/static/org.testng/testng/6.13/org/testng/IResultMap.html
According to testng documentation https://javadoc.io/static/org.testng/testng/6.13/org/testng/IResultMap.html
您可以获得失败测试的详细信息,例如:
You can get details for failed tests like:
for (ISuite suite : suites) {
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
int passedTests = tc.getPassedTests().getAllResults().size();
int skippedTests = tc.getSkippedTests().getAllResults().size();
// First you can get failed tests results in Sete
Set<ITestResult> failedTestResults = tc.getFailedTests().getAllResults();
// Then you can use for loop inside that set
for (ITestResult failedTestResult: failedTestResults) {
// According to here: // https://javadoc.io/static/org.testng/testng/6.13/org/testng/ITestResult.html
//To get failed test parameters
Object[] failedTestParameters = failedTestResult.getParameters();
// To get method name (Not sure on this, getMethod() also may work)
String methodName = failedTestResult.getName(); //(There is also //.getTestName())
// To get exception (Not sure about this)
Throwable exception = failedTestResult.getThrowable();
// You can do whatever you want with these
}
int failedTests = tc.getFailedTests().getAllResults().size();
totalTestsCount += passedTests + skippedTests + failedTests;
totalPassedTests += passedTests;
totalSkippedTests += skippedTests;
totalFailedTests += failedTests;
}
}
正如我所说的,这里有更多方法:https://javadoc.io/static/org.testng/testng/6.13/org/testng/ITestResult.html
Looks like more methods are here as i said: https://javadoc.io/static/org.testng/testng/6.13/org/testng/ITestResult.html
这样你就可以查看文档,然后尝试看看哪些对你有用.
So you can check the doc, and try them to see what works for you.
这篇关于如何使用 IReporter 在 selenium 中获取失败的测试详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!