在何处初始化TestNG TestContext

在何处初始化TestNG TestContext,testng,Testng,我有一个TestRunner类,它以如下方式启动我的TestNG: TestRunConfigs configs = TestRunConfigs.parseCommandLine(args); TestNG testRunner=new TestNG(); testRunner.setXmlSuites(getXmlSuites(configs.TestSuites)); testRunner.run(); 我需要为我所有套件中的所有测试提供一些值。我认为ITestContext是实现这一

我有一个TestRunner类,它以如下方式启动我的TestNG:

TestRunConfigs configs = TestRunConfigs.parseCommandLine(args);

TestNG testRunner=new TestNG();
testRunner.setXmlSuites(getXmlSuites(configs.TestSuites));
testRunner.run();

我需要为我所有套件中的所有测试提供一些值。我认为ITestContext是实现这一点的写作方式。我只是不知道该在哪里做。有任何说明吗?

注意:请确保您使用的是TestNG 7.0.0-beta1,这是截至今天的最新发布版本

完成此操作的最简单方法是通过侦听器注入这些参数

基本上,您可以使用类实现
org.testng.ITestListener
接口。 您可以通过传入测试所需的自定义对象的所需映射来实例化此侦听器。 在侦听器
onStart(ITestContext-ctx)
方法中,将这些属性传递给
ITestContext
对象

下面是一个完整的例子,在实践中证明了这一点

import java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
导入org.testng.ITestContext;
导入org.testng.ITestListener;
导入org.testng.Reporter;
导入org.testng.testng;
导入org.testng.annotations.Test;
公开课范例{
公共静态void main(字符串[]args){
TestNG TestNG=新的TestNG();
setTestClass(新类[]{MyTestClass.Class});
Map attributes=newhashmap();
attributes.put(“data1”,新数据(“TestNG”);
attributes.put(“data2”,新数据(“Selenium”);
attributes.put(“data3”,新数据(“Maven”);
LocalListener=新的LocalListener(属性);
testng.addListener(listener);
testng.setVerbose(2);
testng.run();
}
公共静态类LocalListener实现ITestListener{
私有地图属性;
公共LocalListener(映射属性){
this.attributes=属性;
}
@凌驾
公共void onStart(ITestContext上下文){
forEach(context::setAttribute);
}
}
公共静态类MyTestClass{
@试验
公共void testMethod(){
ITestContext ctx=Reporter.getCurrentTestResult().getTestContext();
Set attributeNames=ctx.getAttributeNames();
attributeNames.forEach(
属性名称->{
System.err.println(“==>”+ctx.getAttribute(attributeName.toString());
});
}
}
公共静态类数据{
私有字符串名称;
公共数据(字符串名称){
this.name=名称;
}
@凌驾
公共字符串toString(){
返回“数据[”+名称+“]”;
}
}
}
输出如下所示

===>Data[Maven]
===>Data[Selenium]
===>Data[TestNG]
PASSED: testMethod

===============================================
    Command line test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

注意:请确保您使用的是TestNG 7.0.0-beta1,这是截至今天的最新发布版本

完成此操作的最简单方法是通过侦听器注入这些参数

基本上,您可以使用类实现
org.testng.ITestListener
接口。 您可以通过传入测试所需的自定义对象的所需映射来实例化此侦听器。 在侦听器
onStart(ITestContext-ctx)
方法中,将这些属性传递给
ITestContext
对象

下面是一个完整的例子,在实践中证明了这一点

import java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
导入org.testng.ITestContext;
导入org.testng.ITestListener;
导入org.testng.Reporter;
导入org.testng.testng;
导入org.testng.annotations.Test;
公开课范例{
公共静态void main(字符串[]args){
TestNG TestNG=新的TestNG();
setTestClass(新类[]{MyTestClass.Class});
Map attributes=newhashmap();
attributes.put(“data1”,新数据(“TestNG”);
attributes.put(“data2”,新数据(“Selenium”);
attributes.put(“data3”,新数据(“Maven”);
LocalListener=新的LocalListener(属性);
testng.addListener(listener);
testng.setVerbose(2);
testng.run();
}
公共静态类LocalListener实现ITestListener{
私有地图属性;
公共LocalListener(映射属性){
this.attributes=属性;
}
@凌驾
公共void onStart(ITestContext上下文){
forEach(context::setAttribute);
}
}
公共静态类MyTestClass{
@试验
公共void testMethod(){
ITestContext ctx=Reporter.getCurrentTestResult().getTestContext();
Set attributeNames=ctx.getAttributeNames();
attributeNames.forEach(
属性名称->{
System.err.println(“==>”+ctx.getAttribute(attributeName.toString());
});
}
}
公共静态类数据{
私有字符串名称;
公共数据(字符串名称){
this.name=名称;
}
@凌驾
公共字符串toString(){
返回“数据[”+名称+“]”;
}
}
}
输出如下所示

===>Data[Maven]
===>Data[Selenium]
===>Data[TestNG]
PASSED: testMethod

===============================================
    Command line test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Command line suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

你能给你的问题增加一些背景吗?您希望传递哪些值?您的测试如何使用这些值?请编辑您的问题,并添加一些示例代码片段,以帮助我们更好地理解。@KrishnanMahadevan我想添加字符串和对象类型属性。这些属性将在稍后的测试中使用。您能为您的问题添加更多上下文吗?您希望传递哪些值?您的测试如何使用这些值?请编辑您的问题,并添加一些示例代码片段,以帮助我们更好地理解。@KrishnanMahadevan我想添加字符串和对象类型属性。这些属性将在稍后的测试中使用。