Java TestNG跨多个测试进行数据驱动测试 [英] Java TestNG with Data Driven Testing Across Multiple Tests
问题描述
我有一系列商店,我正在电子商务平台上测试,每个商店都有一系列属性,我正在考虑自动化测试。是否有可能让数据提供者在测试套件中提供数据,而不仅仅是在TestNG中进行测试?我试图不使用testNG.xml文件作为机制,因为这些属性直接来自数据库调用。
I have a series of stores that I am testing in an ecommerce platform, and each store has a series of properties that I am looking at automating a test for. Is it possible to have a data provider that gives data across a test suite instead of just a test in TestNG? I'm trying not to use the testNG.xml file as mechanism because these properties are coming directly from a database call.
["StoreName", "username", "password", "credit-enabled", "items-store", "shipping-location", ]
我需要自动化做的事情如下:
What I need the automation to do is the following:
- @Test使用当前数据集行中的用户名和密码登录。
- @Test验证StoreName和项目-store
- @Test导航到管理,并根据items-store值验证商店的信用启用设置和商店的送货地点是否正确。
但这里的每一步都必须是它的单独测试。
But each step here would have to be its separate test.
推荐答案
您可以将dataprovider保存在单独的类中,然后使用dataprovider注释您的测试。您可以使用 dataProviderClass 指定它
You can keep dataprovider in a separate class and then annotate your tests with the dataprovider. You can specify it using dataProviderClass
从testng doc引用这里:
Quoting from testng doc here:
默认情况下,将查看数据提供者for in current test
class或其中一个基类。如果你想把你的数据
提供者放在另一个类中,它需要是一个静态方法,你需要
指定可以在dataProviderClass
属性中找到它的类:
By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method and you specify the class where it can be found in the dataProviderClass attribute:
public class StaticProvider {
@DataProvider(name = "create")
public static Object[][] createData() {
return new Object[][] {
new Object[] { new Integer(42) }
}
}
}
public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
public void test(Integer n) {
// ...
}
}
这篇关于Java TestNG跨多个测试进行数据驱动测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!