Java 在 testng 中使用 DataProvider 从文件中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24067577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading data from file using DataProvider in testng
提问by Damien-Amen
Let's say I have a text file with the following data
假设我有一个包含以下数据的文本文件
username=testuser
password=testpassword
[email protected]
address=testaddress
zipcode=12345
Or I have an XML with the following data
或者我有一个包含以下数据的 XML
<TestData>
<UserInfo>
<username>testuser</username>
<password>testpassword</password>
<email>[email protected]</email>
<address>testaddress</address>
</UserInfo>
</TestData>
I have a test as below
我有一个测试如下
public class DPTest {
@Test(dataprovider="testdp")
public void userTest_01(String username, String Password) {
//Test goes here
}
}
Another class
另一堂课
public class DPTest2 {
@Test(dataprovider="testdp")
public void userTest_02(String email, String address, String password) {
//Test goes here
}
}
Can my dataprovider read the values from the above mentioned text file or XML and supply it to the test methods?
我的数据提供者能否从上述文本文件或 XML 中读取值并将其提供给测试方法?
As per my understanding the data provider is going to read all the lines in the text file and supply it to the test method and throw an error saying "data provider is trying to provide 6 parameters but Test can only accept 2 parameter" ?
根据我的理解,数据提供者将读取文本文件中的所有行并将其提供给测试方法并抛出错误,提示“数据提供者试图提供 6 个参数,但测试只能接受 2 个参数”?
Please help me.
请帮我。
采纳答案by Tiago Henrique Engel
Yes, that's possible. You can create an annotation to specify the parameters this DataProvider should load from your XML.
是的,这是可能的。您可以创建一个注释来指定此 DataProvider 应从您的 XML 加载的参数。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface XmlParameters {
String[] value();
}
@Test(dataProvider = "XMLFileLoader")
@XmlParameters({"username", "password"})
public void testSomething(String username, String password) {
// implementation omitted for brevity
}
@DataProvider(name = "XMLFileLoader")
public static Object[][] getDataFromXmlFile(final Method testMethod) {
XmlParameters parameters = testMethod.getAnnotation(XmlParameters.class);
String[] fields = parameters.value();
//load just the fields you want
return new Object[][] { { "user1", "pass1" } };
}
This code is not "production ready", you should check if the annotation is not null before reading the values, and is probably better move the interface and the logic to load your xml to another class, so you can reuse on another tests.
这段代码不是“生产就绪”,你应该在读取值之前检查注释是否不为空,并且可能最好将接口和逻辑移动到你的 xml 到另一个类,这样你就可以在另一个测试中重用。
回答by Sighil
Buddy, you are looking at data provider in the wrong way. Please refer to the testNG documentation. http://testng.org/doc/documentation-main.html
伙计,您以错误的方式看待数据提供者。请参考 testNG 文档。http://testng.org/doc/documentation-main.html
You goal can be achieved by something like the code below. Test
class contains test method. it gets data from a dataprovider specified inside another class TestData
. In TestData
class we define method for accessing data from a file/xml and return it as an 'Object[][]' in '@DataProvider' method
你的目标可以通过类似下面的代码来实现。Test
类包含测试方法。它从另一个类中指定的数据提供者获取数据TestData
。在TestData
类中,我们定义了从文件/xml 访问数据的方法,并将其作为“@DataProvider”方法中的“Object[][]”返回
public class Test {
@Test(dataProvider="testData" dataProviderclass = TestData.class)
public void userTest(TestData testData) {
//Test code goes here
}
}
public class TestData {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
@DataProvider(name="testData")
public static Object[][] userTestData (TestData testData) {
//Code to read from file/xml
TestData testData = new TestData();
testData.setUsername("Get from file/xml");
testData.setPassword("Get from file/xml")
return new Object{{testData}}
}
}