测试 NG - 使用 setTestClasses() 传递参数

发布于 2024-10-25 07:40:36 字数 567 浏览 1 评论 0原文

我正在使用以编程方式运行 Courier 类中包含的测试。

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Courier.class });
testng.addListener(tla);
testng.run();

如何将参数传递给此类中包含的测试? 例如

testng.setTestClasses(new Class[] { Courier("parameter").class });

快递:

public class Courier {
@Parameter(passed parameter)
@Test
public void Courier_Test(String parameter){
    System.out.println(parameter);
}   

}

感谢您的帮助!

I'm using the programmatically approach to run tests included in the Courier class.

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Courier.class });
testng.addListener(tla);
testng.run();

How is it possible to pass parameter to tests included in this class?
e.g.

testng.setTestClasses(new Class[] { Courier("parameter").class });

Courier:

public class Courier {
@Parameter(passed parameter)
@Test
public void Courier_Test(String parameter){
    System.out.println(parameter);
}   

}

Thanky for any help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尸血腥色 2024-11-01 07:40:36

一些想法:

即使您以编程方式运行测试,您也应该能够在 testng.xml 文件上调用 TestNG。像这样向文件添加参数(来自 文档):

<套件名称=“我的套件”>
   <参数名称=“参数”值=“Foo”/>
   <测试名称=“快递测试”/>
   < ...>>

如果由于某种原因您没有使用 testng.xml 文件,则可以使用 DataProvider,作为测试类中的方法或作为静态类,具体取决于您需要执行的操作。下面的示例(也来自文档)。

类内的DataProvider:

//This method will provide data to any test method that declares
//that its Data Provider is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
   return new Object[][] {
     new Object[] { "Parameter" }
   }
}

//This test method declares that its data should be supplied 
//by the Data Providernamed "test1"
@Test(dataProvider = "test1")
public void Courier_Test(String parameter) {
 System.out.println(parameter);
} 

外部类中的DataProvider:

public static class StaticProvider {
  @DataProvider(name = "create")
  public static Object[][] createData() {
    return new Object[][] {
      new Object[] { "Parameter" }
    }
  }
}

public class Courier {
  @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
  public void Courier_Test(String parameter) {
    // ...
  }
}

A couple of ideas:

Even if you are running the tests programmatically, you should be able to invoke TestNG on a testng.xml file. Add parameters to the file like so (from the documentation):

<suite name="My suite">
   <parameter name="parameter"  value="Foo"/>
   <test name="Courier Test" />
   < ... >

If for some reason you aren't using a testng.xml file, you can use a DataProvider, either as a method within the test class or as a static class, depending on what you need to do. Examples below (also from the documentation).

DataProvider inside the class:

//This method will provide data to any test method that declares
//that its Data Provider is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
   return new Object[][] {
     new Object[] { "Parameter" }
   }
}

//This test method declares that its data should be supplied 
//by the Data Providernamed "test1"
@Test(dataProvider = "test1")
public void Courier_Test(String parameter) {
 System.out.println(parameter);
} 

DataProvider in external class:

public static class StaticProvider {
  @DataProvider(name = "create")
  public static Object[][] createData() {
    return new Object[][] {
      new Object[] { "Parameter" }
    }
  }
}

public class Courier {
  @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
  public void Courier_Test(String parameter) {
    // ...
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文