Java 检索@Test 描述表单 testNG 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23871486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving @Test description form testNG tests
提问by gandalf_the_cool
I have the following format on my testNG tests:
我的 testNG 测试有以下格式:
@Test(alwaysRun = true, dependsOnMethods = "testStep_1", description = "Enter the names, and verify that they are appearing correctly ")
public void testStep_2() throws Exception{
}
Is there a way to implement something that could read all test descriptions, and by that generating a test documentation.
I tried to somehow include ITestNGMethod getDescription()
to a afterInvocation(IInvokedMethod method, ITestResult testResult)
so after each method is run, the description is returned, but with no success.
Has anyone tried something similar?
有没有办法实现可以读取所有测试描述的东西,并通过它生成测试文档。我试图在每个方法运行后以某种方式包含ITestNGMethod getDescription()
到一个afterInvocation(IInvokedMethod method, ITestResult testResult)
so 中,返回描述,但没有成功。有没有人尝试过类似的东西?
回答by user1058106
IMethodInterceptor implementation allows you to access all your tests annotations and their parameters.
IMethodInterceptor 实现允许您访问所有测试注释及其参数。
import java.util.List;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Test;
public class Interceptor implements IMethodInterceptor
{
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context)
{
int methCount = methods.size();
for (int i = 0; i < methCount; i++)
{
IMethodInstance instns = methods.get(i);
System.out.println(instns.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class)
.description());
}
return methods;
}
}
Add implemented class to your listeners list. So that TestNG know about it.
将实现的类添加到您的侦听器列表中。让 TestNG 知道它。
回答by djangofan
There is an easier way to do this, without defining a listener interceptor, as I show in this GitHub project I made.
有一种更简单的方法可以做到这一点,而无需定义侦听器拦截器,正如我在这个 GitHub 项目中展示的那样。
Basically, define a TestBase class like this:
基本上,像这样定义一个 TestBase 类:
public abstract class NGTestBase extends AbstractTestNGSpringContextTests
{
....
private String testDescription;
@BeforeMethod
public void setUp(Method ngMethod)
{
...
setTestDescription(ngMethod);
...
}
....
public String getTestDescription()
{
return this.testDescription;
}
private void setTestDescription(Method methodInstance)
{
this.testDescription = methodInstance.getAnnotation(Test.class).description();
}
}
Then, in your test, just annotate them like this:
然后,在您的测试中,只需像这样注释它们:
@Test(description = "Test printing out all the Spring beans.")
public void printAllBeansTest(Method ngMethod)
{
...
String[] beanNames = applicationContext.getBeanDefinitionNames();
...
for (String beanName : beanNames)
{
test.log(LogStatus.INFO, "BEAN[" + beanName + "] : " + applicationContext.getBean(beanName).getClass().toString());
}
...
}
回答by ArrchanaMohan
The simplest way is by using ITestResult.
最简单的方法是使用 ITestResult。
@Override
public void afterInvocation(IInvokedMethod arg, ITestResult arg1) {
System.out.println(arg.getTestMethod().getMethodName());
System.out.println(arg1.getMethod().getDescription());
}
The second sysout will return a (String) description of the invoked test method.
第二个 sysout 将返回调用的测试方法的(字符串)描述。
回答by alicehanhy
public class ReportSet_MethodListener implements IInvokedMethodListener {
@Override
public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
if (iInvokedMethod.getTestMethod().isTest()){
System.out.println("TestCaseName:" + iInvokedMethod.getTestMethod().getDescription());
}
}
public class ReportSet_MethodListener implements IInvokedMethodListener {
@Override
public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
if (iInvokedMethod.getTestMethod().isTest()){
System.out.println("TestCaseName:" + iInvokedMethod.getTestMethod().getDescription());
}
}
回答by Manu
We tried something similar. Below is the method that print the testng test name and test description for each method.
我们尝试了类似的东西。下面是打印每个方法的 testng 测试名称和测试描述的方法。
@BeforeMethod
public void beforeMethod(Method method) {
Test test = method.getAnnotation(Test.class);
System.out.println("Test name is " + test.testName());
System.out.println("Test description is " + test.description());
}
回答by Sidhant
The easiest way to do it is:
最简单的方法是:
public void onTestStart(ITestResult result) {
System.out.prinln(result.getMethod().getDescription());
}
This should give you the description parameter of the @Test annotation
这应该为您提供@Test 注释的描述参数