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

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

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

&lt;parameter name=&quot;blah blah blah&quot; /&gt;
&lt;parameter name=&quot;when&quot; value=&quot;blah blah blah&quot; /&gt;  
&lt;parameter name=&quot;then&quot; value=&quot;blah blah blah&quot; /&gt; 

And then can use in your test case like below

  private String GIVEN = &quot;&quot;;
   private String WHEN = &quot;&quot;;
   private String THEN = &quot;&quot;;
         
    @Test
         @Parameters({&quot;given&quot;,&quot;when&quot;,&quot;then&quot;})
            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(&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:

确定