TestNG参数化传入方法

TestNG parametrization pass in method

我想知道在使用 testng.xml 时是否可以将参数放入方法中。我知道将参数放入测试 class。我使用页面对象模型方法。这是我的代码

<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>

低于测试class

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;

不要在测试 class 和 Paragraph class 中使用相同的方法名称。我将测试 class 方法名称从 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>

测试class

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

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

段落class

public class Paragraphs {

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

参考this tutorial了解更多信息