TestNG参数化在方法中传递

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

TestNG parametrization pass in method

问题

我想知道在使用testng.xml时是否可以在方法中传入参数。我知道可以在测试类中设置参数。我使用页面对象模型方法。以下是我的代码示例:

<suite name="dev-parametrization" verbose="1">
  <test name="Paragraphs-Tests">
    <classes>
      <class name="com.java.tests.ParagraphsApiControllerTests">
        <methods>
          <include name="createParagraph">
            <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
          </include>
        </methods>
      </class>
    </classes>
  </test>
</suite>

以下是测试类:

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void createParagraph() {
    paragraphs.createParagraph();
}

以及我的方法 - 在这里,我想使用来自XML文件的参数。这可能吗?我该如何做到这一点?

public class Paragraphs {

    String paragraphsURL = "http://192.168.0.139:8880/paragraphs";
    String apiParagraphsURL = "http://192.168.0.139/api/paragraphs";

    public void createParagraph() {
        RestAssured.baseURI = paragraphsURL;
英文:

I'm wondering is it possible to put parameters in method when I use testng.xml. I know about put parameteres in test class. I use page object model approach. Here is my code

&lt;suite name=&quot;dev-parametrization&quot; verbose=&quot;1&quot; &gt;
  &lt;test name=&quot;Paragraphs-Tests&quot;&gt;
    &lt;classes&gt;
      &lt;class name=&quot;com.java.tests.ParagraphsApiControllerTests&quot;&gt;
        &lt;methods&gt;
          &lt;include name=&quot;createParagraph&quot;&gt;
            &lt;parameter name=&quot;paragraphsURL&quot; value=&quot;http://192.168.0.139:8880/paragraphs&quot;/&gt;
          &lt;/include&gt;
        &lt;/methods&gt;
      &lt;/class&gt;
    &lt;/classes&gt;
  &lt;/test&gt;

Below test class

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

@Parameters({&quot;paragraphsURL&quot;})
@Test(priority = 1)
public void createParagraph() {
    paragraphs.createParagraph();
}

And my method - here I want to use parameter from xml. file. Is it possible? How can I do this?

public class Paragraphs {


    String paragraphsURL = &quot;http://192.168.0.139:8880/paragraphs&quot;;
    String apiParagraphsURL = &quot;http://192.168.0.139/api/paragraphs&quot;;

    public void createParagraph() {
        RestAssured.baseURI = paragraphsURL;

答案1

得分: 1

不要在测试类和Paragraph类中使用相同的方法名。我将测试类方法名从createParagraph更改为testCreateParagraph

Testng.xml

<suite name="dev-parametrization" verbose="1">
  <test name="Paragraphs-Tests">
    <classes>
      <class name="com.java.tests.ParagraphsApiControllerTests">
        <methods>
          <include name="testCreateParagraph">
            <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
          </include>
        </methods>
      </class>
    </classes>
  </test>
</suite>

测试类

public class ParagraphsApiControllerTests {

    Paragraphs paragraphs = new Paragraphs();
    
    @Parameters({"paragraphsURL"})
    @Test(priority = 1)
    public void testCreateParagraph(String paragraphsURL) {
        paragraphs.createParagraph(paragraphsURL);
    }
}

段落类

public class Paragraphs {
    
    public void createParagraph(String paragraphsURL) {
        RestAssured.baseURI = paragraphsURL;
    }
}

更多信息,请参阅此教程

英文:

Don't use the same method name in test class and in Paragraph class. I changed the test class method name from createParagraph to testCreateParagraph.

Testng.xml

&lt;suite name=&quot;dev-parametrization&quot; verbose=&quot;1&quot; &gt;
  &lt;test name=&quot;Paragraphs-Tests&quot;&gt;
    &lt;classes&gt;
      &lt;class name=&quot;com.java.tests.ParagraphsApiControllerTests&quot;&gt;
        &lt;methods&gt;
          &lt;include name=&quot;testCreateParagraph&quot;&gt;
            &lt;parameter name=&quot;paragraphsURL&quot; value=&quot;http://192.168.0.139:8880/paragraphs&quot;/&gt;
          &lt;/include&gt;
        &lt;/methods&gt;
      &lt;/class&gt;
    &lt;/classes&gt;
  &lt;/test&gt;

Test class

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

@Parameters({&quot;paragraphsURL&quot;})
@Test(priority = 1)
public void testCreateParagraph(String paragraphsURL) {
    paragraphs.createParagraph(paragraphsURL);
}

Paragraph class

public class Paragraphs {

    public void createParagraph(String paragraphsURL) {
        RestAssured.baseURI = paragraphsURL;
    }
}

Refer this tutorial for more information

huangapple
  • 本文由 发表于 2020年10月8日 19:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64261691.html
匿名

发表评论

匿名网友

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

确定