英文:
Is there a way of dynamically attributing the description annotation in testNG?
问题
private String GIVEN = "";
private String WHEN = "";
private String THEN = "";
@Test(description = GIVEN + WHEN + THEN)
public void test() {
GIVEN += "blah blah blah";
WHEN += "blah blah blah";
THEN += "blah blah blah";
}
我想以这种方式进行,因为我希望在测试中使用的方法中添加描述。这样我就可以避免注释,并且可以随着测试的变化更新细节。
例如,我将在测试中调用此方法并同时更新"Given":
public void method(){
// 代码;
GIVEN += "这段代码正在做这个 blah blah";
}
这些值只能是常量,所以我在这一点上感到困惑。
英文:
private String GIVEN = "";
private String WHEN = "";
private String THEN = "";
@Test(description = GIVEN + WHEN + THEN)
public void test() {
GIVEN += "blah blah blah";
WHEN += "blah blah blah";
THEN += "blah blah blah";
}
I want to do it this way b/c I want to add the descriptions in the methods used within tests. This way I avoid comments and can keep details up to date as tests change.
For instance, I will call this method within a test and update the given at the same time:
public void method(){
code;
GIVEN += "this code is doing this blah blah";
}
Values can only be a constants so I'm stumped at this point.
答案1
得分: 0
**在 TestNG.xml 上定义参数**
<parameter name="blah blah blah" />
<parameter name="when" value="blah blah blah" />
<parameter name="then" value="blah blah blah" />
**然后可以在你的测试用例中使用,如下所示**
private String GIVEN = "";
private String WHEN = "";
private String THEN = "";
@Test
@Parameters({"given","when","then"})
public void test() {
GIVEN += given;
WHEN += when;
THEN += then;
}
英文:
Define the parameters on TestNG.xml
<parameter name="blah blah blah" />
<parameter name="when" value="blah blah blah" />
<parameter name="then" value="blah blah blah" />
And then can use in your test case like below
private String GIVEN = "";
private String WHEN = "";
private String THEN = "";
@Test
@Parameters({"given","when","then"})
public void test() {
GIVEN += given;
WHEN += when;
THEN += then;
}
答案2
得分: 0
ITestResult report = Reporter.getCurrentTestResult();
report.getMethod().setDescription("您想要表达的内容");
英文:
ITestResult report = Reporter.getCurrentTestResult();
report.getMethod().setDescription("Whatever you would like to say");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论