将 .properties 文件设置为 testng.xml [英] Set .properties file to testng.xml
问题描述
我有一个 data.properties 文件,其中包含以下几行:
I have a data.properties file with the following lines :
data.login=login
data.password=password
我有一个简单的测试(使用 testng):
and I have a simple test(with testng):
@Test
@Parameters({"data.login","data.password"})
public void testSimpleExample(String login, String password) throws Exception {
Assert.assertTrue(login.equals("login"));
Assert.assertTrue(password.equals("password"));
}
在 testng.xml 文件中我可以写下一个字符串:
In testng.xml file I can write next strings :
<parameter name="data.login" value="login" />
<parameter name="data.password" value="password" />
但我想使用我的 data.properties 文件.我可以以某种方式将此文件设置为 testng.xml 并使用此属性文件中的参数吗?当然没有使用下一个代码:
but I want to use my data.properties file. Can I somehow set this file into testng.xml and use parameters from this properties file ? Of course without using next code :
Properties properties = new Properties();
properties.load(....);
....
properties.getProperty("data.login");
推荐答案
如果您不想使用 @juherr 提出的数据提供程序方法,这里有另一种方法.
Here's another way of doing it, if you don't want to make use of the data provider approach that @juherr has called out.
属性文件代表一个键/值对,所以本质上它是一个映射.因此,您执行以下操作:
Properties file represents a Key/Value pair, so essentially its a map. So you do the following :
- 确保您使用的是 TestNG v6.11(或)更高版本.
- 使用
@Parameters
注释,就像您要从 TestNG 套件 xml 文件中获取值一样. - 构建一个
org.testng.IAlterSuiteListener
实现,您可以在其中读取属性文件,提取地图,然后将此地图作为参数注入您的XmlSuite
对象. - 您可以通过套件 xml 中的
<listeners>
标记(或)通过服务加载器机制连接您在 (3) 中创建的此侦听器.
- Ensure you are using TestNG v6.11 (or) higher.
- Use the
@Parameters
annotation as if you are going to get the values from your TestNG suite xml file. - Build a
org.testng.IAlterSuiteListener
implementation wherein you read your properties file, extract out the map, and then inject this map as parameters into yourXmlSuite
object. - You wire in this listener that you created in (3) either via the
<listeners>
tag in your suite xml (or) via a Service loader mechanism.
现在您可以继续使用 @Parameters
注释,但您的所有属性都是动态注入的.
Now you can continue to work with the @Parameters
annotation, but all of your properties are injected dynamically.
这是所有这些操作的示例.
Here's a sample of all this in action.
TestClass 如下所示
package com.rationaleemotions.stackoverflow.qn46224926;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestClass {
@Test
@Parameters("name")
public void testMethod(String name) {
System.err.println("Hello " + name);
}
}
IAlterSuiteListener
实现如下所示:
package com.rationaleemotions.stackoverflow.qn46224926;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class PropertyInjectorListener implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
XmlSuite suite = suites.get(0);
Properties properties = new Properties();
try {
properties.load(new FileReader("src/test/resources/46224926/qn46224926.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Map<String, String> params = new HashMap<>();
for (Map.Entry<Object, Object> each : properties.entrySet()) {
params.put(each.getKey().toString(), each.getValue().toString());
}
suite.setParameters(params);
}
}
示例属性文件如下所示:
A sample properties file can look like below:
name=Jack
套件 xml 文件如下所示:
The suite xml file would look like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46224926_Suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.stackoverflow.qn46224926.PropertyInjectorListener"/>
</listeners>
<test name="46224926_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn46224926.TestClass"/>
</classes>
</test>
</suite>
当你运行它时,你应该看到如下输出:
When you run this, you should see an output as below:
...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Hello Jack
PASSED: testMethod("Jack")
===============================================
46224926_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
46224926_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
如您所见,套件 xml 文件根本不包含任何
标记.但是 @Test
方法仍然假设它将通过 @Parameters
注释获取参数值.由于我们的侦听器负责读取所有属性并将它们作为参数映射注入,因此 TestNG 不会抱怨.
As you can see, the suite xml file does not contain any <parameters>
tag at all. But the @Test
method still assumes that it will get parameter values via the @Parameters
annotation. Since our listener takes care of reading all the properties and injecting them as a parameter map, TestNG does not complain.
这篇关于将 .properties 文件设置为 testng.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!