有没有一种方法可以在TestNG中动态地分配描述注解?

huangapple go评论109阅读模式
英文:

Is there a way of dynamically attributing the description annotation in testNG?

问题

  1. private String GIVEN = "";
  2. private String WHEN = "";
  3. private String THEN = "";
  4. @Test(description = GIVEN + WHEN + THEN)
  5. public void test() {
  6. GIVEN += "blah blah blah";
  7. WHEN += "blah blah blah";
  8. THEN += "blah blah blah";
  9. }

我想以这种方式进行,因为我希望在测试中使用的方法中添加描述。这样我就可以避免注释,并且可以随着测试的变化更新细节。

例如,我将在测试中调用此方法并同时更新"Given":

  1. public void method(){
  2. // 代码;
  3. GIVEN += "这段代码正在做这个 blah blah";
  4. }

这些值只能是常量,所以我在这一点上感到困惑。

英文:
  1. private String GIVEN = "";
  2. private String WHEN = "";
  3. private String THEN = "";
  4. @Test(description = GIVEN + WHEN + THEN)
  5. public void test() {
  6. GIVEN += "blah blah blah";
  7. WHEN += "blah blah blah";
  8. THEN += "blah blah blah";
  9. }

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:

  1. public void method(){
  2. code;
  3. GIVEN += "this code is doing this blah blah";
  4. }

Values can only be a constants so I'm stumped at this point.

答案1

得分: 0

  1. **在 TestNG.xml 上定义参数**
  2. <parameter name="blah blah blah" />
  3. <parameter name="when" value="blah blah blah" />
  4. <parameter name="then" value="blah blah blah" />
  5. **然后可以在你的测试用例中使用,如下所示**
  6. private String GIVEN = "";
  7. private String WHEN = "";
  8. private String THEN = "";
  9. @Test
  10. @Parameters({"given","when","then"})
  11. public void test() {
  12. GIVEN += given;
  13. WHEN += when;
  14. THEN += then;
  15. }
英文:

Define the parameters on TestNG.xml

  1. &lt;parameter name=&quot;blah blah blah&quot; /&gt;
  2. &lt;parameter name=&quot;when&quot; value=&quot;blah blah blah&quot; /&gt;
  3. &lt;parameter name=&quot;then&quot; value=&quot;blah blah blah&quot; /&gt;

And then can use in your test case like below

  1. private String GIVEN = &quot;&quot;;
  2. private String WHEN = &quot;&quot;;
  3. private String THEN = &quot;&quot;;
  4. @Test
  5. @Parameters({&quot;given&quot;,&quot;when&quot;,&quot;then&quot;})
  6. public void test() {
  7. GIVEN += given;
  8. WHEN += when;
  9. THEN += then;
  10. }

答案2

得分: 0

  1. ITestResult report = Reporter.getCurrentTestResult();
  2. report.getMethod().setDescription("您想要表达的内容");
英文:
  1. ITestResult report = Reporter.getCurrentTestResult();
  2. report.getMethod().setDescription(&quot;Whatever you would like to say&quot;);

huangapple
  • 本文由 发表于 2020年9月28日 09:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64094764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定