- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在开发一个需要以编程方式调用 TestNG(使用数据提供程序)的项目。一切都很好,只是在报告中,我们得到了 @Test 方法的名称,它是处理许多情况的通用方法。我们希望在报告中获得一个有意义的名称。
我对此进行了研究,发现了 3 种方法,但不幸的是,我都失败了。
1) 实现 ITest
我在输入@Test 方法后立即设置我想要的名称(对于我尝试的所有 3 种方法,这就是我设置名称的方式)。这个名称是从 getTestName() 返回的。我观察到的是 getTestName() 在我的@Test 之前和之后被调用。最初,它返回 null(为了处理 NullPointerException,我返回 ""而不是 null),后来它返回正确的值。但我没有看到这反射(reflect)在报告中
编辑:还尝试按照 artdanil 的建议从@BeforeMethod 设置名称
2 和 3
两者都基于 second link above 中给出的解决方案
通过覆盖 XmlSuite 中的 setName,我得到了
Exception in thread "main" java.lang.AssertionError: l should not be null
at org.testng.ClassMethodMap.removeAndCheckIfLast(ClassMethodMap.java:58)
at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:208)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:114)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
...
通过覆盖 toString(),我在日志中看到了这些(以及我的评论),但在报告中没有更新
[2013-03-05 14:53:22,174] (Main.java:30) - calling execute
[2013-03-05 14:53:22,346] GenericFunctionTest.<init>(GenericFunctionTest.java:52) - inside constructor
[2013-03-05 14:53:22,372] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning **//this followed by 3 invocations before arriving at @Test method**
[2013-03-05 14:53:22,410] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,416] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,455] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning
[2013-03-05 14:53:22,892] GenericFunctionTest.<init>(GenericFunctionTest.java:52) - inside constructor
[2013-03-05 14:53:23,178] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning **//again blank as i havent set it yet**
[2013-03-05 14:53:23,182] GenericFunctionTest.getResult(GenericFunctionTest.java:69) - inside with test case:TestCase{signature=Signature{...}}**//I am setting it immedietely after this**
[2013-03-05 14:53:23,293] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning MyMethodName **//What i want**
[2013-03-05 14:53:23,299] GenericFunctionTest.toString(GenericFunctionTest.java:276) - returning MyMethodName **// again**
编辑:通过硬编码一个值而不是在我的测试方法入口处设置它,再次尝试了所有 3 个。但结果相同
最佳答案
我也遇到了同样的问题,发现在@BeforeMethod
注解的方法中设置存储测试用例名称的字段,使用native injection of TestNG会有所帮助提供方法名称和测试参数。测试名称取自 DataProvider
提供的测试参数。如果您的测试方法没有参数,只需报告方法名称即可。
//oversimplified for demontration purposes
public class TestParameters {
private String testName = null;
private String testDescription = null;
public TestParameters(String name,
String description) {
this.testName = name;
this.testDescription = description;
}
public String getTestName() {
return testName;
}
public String getTestDescription() {
return testDescription;
}
}
public class SampleTest implements ITest {
// Has to be set to prevent NullPointerException from reporters
protected String mTestCaseName = "";
@DataProvider(name="BasicDataProvider")
public Object[][] getTestData() {
Object[][] data = new Object[][] {
{ new TestParameters("TestCase1", "Sample test 1")},
{ new TestParameters("TestCase2", "Sample test 2")},
{ new TestParameters("TestCase3", "Sample test 3")},
{ new TestParameters("TestCase4", "Sample test 4")},
{ new TestParameters("TestCase5", "Sample test 5") }
};
return data;
}
@BeforeMethod(alwaysRun = true)
public void testData(Method method, Object[] testData) {
String testCase = "";
if (testData != null && testData.length > 0) {
TestParameters testParams = null;
//Check if test method has actually received required parameters
for (Object testParameter : testData) {
if (testParameter instanceof TestParameters) {
testParams = (TestParameters)testParameter;
break;
}
}
if (testParams != null) {
testCase = testParams.getTestName();
}
}
this.mTestCaseName = String.format("%s(%s)", method.getName(), testCase);
}
@Override
public String getTestName() {
return this.mTestCaseName;
}
@Test(dataProvider="BasicDataProvider")
public void testSample1(TestParameters testParams){
//test code here
}
@Test(dataProvider="BasicDataProvider")
public void testSample2(TestParameters testParams){
//test code here
}
@Test
public void testSample3(){
//test code here
}
}
编辑:根据下面的评论,我意识到报告中的示例会很有用。
从上面的运行代码中提取报告:
<testng-results skipped="0" failed="0" total="5" passed="5">
<suite name="SampleTests" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test name="Test1" duration-ms="2818" started-at="<some-time>" finished-at="<some-time>">
<test-method
status="PASS"
signature="testSample1(org.example.test.TestParameters)[pri:0, instance:org.example.test.TimeTest@c9d92c]"
test-instance-name="testSample1(TestCase5)"
name="testSample1"
duration-ms="1014"
started-at="<some-time-before>"
data-provider="BasicDataProvider"
finished-at="<some-time-later>" >
<!-- excluded for demonstration purposes -->
</test-method>
<!-- the rest of test results excluded for brevity -->
</test>
</suite>
</testng-result>
注意,从 getTestName()
方法返回的值是在 test-instance-name
属性中,而不是在 name
属性。
关于java - TestNG 报告中的自定义测试方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220262/
我想多次运行一个测试用例。在testng.xml中可以配置吗?如果在测试方法中添加循环,则每次运行的结果将不会受到testng报告的影响。 最佳答案 您不能通过xml来执行此操作,但是可以在@Test
我在 TestNG 中尝试了一些使用硬断言和软断言的示例。虽然软断言按预期工作,但我遇到了硬断言的问题。 根据我从互联网上读到的信息-如果硬断言失败,它不会执行其他测试。 由于 ffPageEleme
我能够在 Eclipse 中完美地运行 testng。但是,我想使用命令行(Win 8)运行它,如不同论坛上所述。以下是我尝试处理错误的不同方法 - 1. java -cp "D:\testng\te
假设我有一个可以并行或串行执行的套件。但是,这样做的决定要留给运行时。启动此套件的常见方法如下: TestNG runner = new TestNG(); if (runInParallel())
如何获得执行TESTNG的方法所花费的时间? sample 测试: @Test(threadPoolSize = 100, invocationCount = 100) public void tes
我发现有一个选项可以通过surefire插件将参数设置为testng xml,然后可以从命令行发送参数。 [...] org.apache.maven.plug
我正在尝试使用这样的命令行运行 testng 测试: C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>ja
我正在尝试从我的套件文件中删除 testng 参数,因为它们在多个套件中都需要。例如,我有 2 个测试套件,upgrades.xml 和 features.xml。我创建了第三个文件,paramete
目标:并行独立运行 2 个类,其中每个测试将方法名称存储到一个变量中,该变量稍后可以在测试中访问。 问题:当测试并行运行时,它们开始在它们之间共享数据,从而破坏测试。 如果您看到控制台输出这是错误的:
好的,请引用 TestNG doc我可以运行测试 依次 (默认在测试套件中)或在 平行使用 . 现在这是我的 TestNG 配置
我正在尝试从命令行运行TestNG,但这给了我错误。 有人可以告诉我我要去哪里错吗? java -cp C:/TestNG目录/testng-6.2.jar;C:\project目录\src\com\
情况和问题 我有几个测试类,每个类都有几个测试方法。所有测试在后台使用相同的测试数据库。每个测试类初始化其数据库内容,然后在几个测试方法中测试内容。 当我单独运行每个测试时,它们都通过了。但是当我同时
如何让我的 TestNG 报告显示报告中控制台输出的链接?我以前见过 TestNG 这样做,但我现在正在做的一个项目没有显示任何输出,而且我在任何地方都找不到任何说明如何打开它的信息。我尝试在 tes
如果断言失败,如何在 TestNG 中继续执行测试?如何在 TestNG 的 HTML 报告中报告失败? 当我运行以下代码时,执行了断言之后的行,但在报告中未列出断言失败: @Test public
如果断言失败,如何在 TestNG 中继续执行测试?如何在 TestNG 的 HTML 报告中报告失败? 当我运行以下代码时,执行了断言之后的行,但在报告中未列出断言失败: @Test public
我在每个 wiki 使用 TestNG + ReportNG instructions在 gradle 中(我固定在食谱上,因为默认示例对我不起作用)。 我想以某种方式在 TestNG 中捕获控制台输
如何使用 TestNG 自动化功能测试,跨越多个 Java 进程? 我对尝试使用 TestNG 而不是 JUnit 产生了兴趣,因为它声称不仅仅是为单元测试而设计的,但我还没有找到具体的例子来说明如何
如何在测试用例的运行时获取当前正在运行的套件名称?我正在使用下面显示的代码来获取当前套件名称。 监听类: public class SuiteListener implements ISuiteLis
我正在使用 Selenium Webdriver 和 Java 设计一个用于测试的定制自动化框架。 现在出于报告的目的,我正在尝试将 TestNG 框架与我的项目集成。 现在我有一个基于 GUI 的界
出于某种原因,我必须使用旧的 TestNG 库,它没有 "getCurrentXmlTest()).getAllParameters()" API 我应该如何使用testng-5.4-jdk15.ja
我是一名优秀的程序员,十分优秀!