gpt4 book ai didi

java - 如何将对象/参数传递给 Testng

转载 作者:行者123 更新时间:2023-12-02 12:17:52 24 4
gpt4 key购买 nike

我想将参数传递给 testng 类。我有一个带有变量 tc1.featureFileName、tc1.fieldValueMap 的 tc 对象

我已经将名称解析为c2,但我不知道如何传递tc1.fieldValueMap,其中包含 testng 的输入变量的 HashMap 。

        Class c2 = Class.forName("com.Scenarios."+tc1.featureFileName);

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

最佳答案

以下示例向您展示了在使用 TestNG API 时如何将参数作为键/值对注入(inject)。

import org.testng.Assert;
import org.testng.Reporter;
import org.testng.TestNG;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class TestRunner {
public static void main(String[] args) {

XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("Sample_Suite");
Map<String, String> fieldValues = new ParamContainer().getValues();
xmlSuite.setParameters(fieldValues);
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("Sample_test");
xmlTest.setXmlClasses(Collections.singletonList(new XmlClass(HelloWorld.class)));
TestNG tng = new TestNG();
tng.setXmlSuites(Collections.singletonList(xmlSuite));
tng.run();
}

/**
* This is a test class.
*/
public static class HelloWorld {
@Test
@Parameters("name")
public void hi(String name) {
Assert.assertNotNull(name);
Reporter.log("Name is:" + name, true);
}
}

/**
* Simulates a class that will contain all the key/value pairs that are to be used as
* <code><parameters></code> for the suite.
*/
public static class ParamContainer {
private Map<String, String> values = new HashMap<>();

ParamContainer() {
values.put("name", "Jack-Daniel");
}

Map<String, String> getValues() {
return values;
}
}

}

关于java - 如何将对象/参数传递给 Testng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46022648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com