我正在使用TestNG进行硒测试。 有一个名为“创建公司”的测试,需要在我的笔记本电脑上多次运行。
所以我为此编写了一个名为“CreateFirm”的类,各个公司的数据都存在于excel电子表格中。
在不同的时间,我需要创建各种公司集合,我使用Excel电子表格中的列来控制它,它保存我的计算机名称。
我使用@Factory创建我的“CreateFirm”类,它有一个@Test方法来创建公司。
在excel电子表格中如果我按照与笔记本电脑相同的顺序分配了Firm1,Firm2,Firm3,Firm4,@ Factory会以随机顺序创建它们,如Firm4,Firm3,Firm1,Firm2
我的问题是如何让@Factory按照我想要的顺序创建测试实例?
我的@Factory方法是
@Factory
public Object[] runCreateFirm()
{
//This is where I get the list of test cases assigned to my laptop
this.test_id_list=get_test_ids_for_test_run("Create Firm (class approach).xls", "Global");
Object[] result = new Object[this.test_id_list.size()];
int index=0 ;
for (String firm_id: this.test_id_list)
{
//This is where I get all the test data from the Excel spreadsheet
HashMap<String,String> test_data_row=this.get_row_from_excel("Create Firm (class approach).xls", "Global", "test_case_id", firm_id);
System.out.println("Inside Firm Factory ,index="+index +", test case id="+ test_data_row.get("test_case_id"));
//CreateFirm is the class which will use the data and do all the UI actions to create a Firm
result[index]=new CreateFirm(test_data_row);
index++;
}
return result;
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="CreateFirm Suite">
<test name="Create Firm Test" order-by-instances="false">
<classes>
<class name="regressionTests.factory.CreateFirmFactory"/>
</classes>
</test>
</suite>
答案 0 :(得分:2)
管理为我的要求编写拦截器方法。 首先,我在我的班级中引入了一个名为“priority”的新字段。我不能使用@Priority,因为我的测试类只有一个测试方法,在这种情况下,我的所有实例都具有相同的优先级。 所以我介绍了一个名为priority的新字段,它将具有应该实例化类的顺序。我在拦截器方法中使用该字段来设置实例化的顺序
public List<IMethodInstance> intercept(List<IMethodInstance> methods,ITestContext context)
{
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
int array_index=0;
for (IMethodInstance m : methods)
{
result.add(m);
}
//Now iterate through each of these test methods
for (IMethodInstance m : methods)
{
try {
//Get the FIELD object from - Methodobj->method->class->field
Field f = m.getMethod().getRealClass().getField("priority");
//Get the object instance of the method object
array_index=f.getInt(m.getInstance());
}
catch (Exception e) {
e.printStackTrace();
}
result.set(array_index-1, m);
}
return result;
}
答案 1 :(得分:1)
工厂实例化测试类,它不运行它们。如果您需要特定的测试订单,那么在依赖关系(组和方法),优先级和方法拦截器之间有很多选择。
答案 2 :(得分:0)
请参考https://github.com/cbeust/testng/issues/1410作为解决方案,并从krmahadevan给出的示例中得到了提示。多谢KR。我正在Eclipse中使用TestNG插件版本6.14.0.201802161500。
只需在Test Class中重写toString方法即可。在此toString方法中,返回键参数,例如Firm1,Firm2等。请参考以下代码以清楚了解。
TestClass.java
import org.testng.annotations.Test;
public class TestClass {
String testcaseName;
public TestClass(String testcaseName) {
this.testcaseName = testcaseName;
}
@Test
public void sendCmd() {
System.out.println("Testcase received "+this.testcaseName);
}
@Override
public String toString() {
return this.testcaseName;
}
}
FactoryClass.java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
public class FactoryClass {
@DataProvider
public static String[][] createTestCmds() {
String[][] testData = new String[6][1];
for (int i = 0, j= 6; i < 6 ; i++,j--) {
testData[i][0]="Firm "+i;
}
return testData;
}
@Factory(dataProvider = "createTestCmds")
public Object[] Factory(String testcaseName) {
return new Object[] {new TestClass(testcaseName)};
}
}
Test.java
import java.util.Collections;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class Test {
public static void main(String[] args) {
TestNG testng = new TestNG();
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setGroupByInstances(true);
xmlSuite.setName("Sample_Test_Suite");
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setName("Sample_Test");
xmlTest.setClasses(Collections.singletonList(new XmlClass(FactoryClass.class)));
testng.setXmlSuites(Collections.singletonList(xmlSuite));
testng.setVerbose(2);
System.err.println("Printing the suite xml file that would be used.");
System.err.println(xmlSuite.toXml());
testng.run();
}
}
Testcase received Firm 0
Testcase received Firm 1
Testcase received Firm 2
Testcase received Firm 3
Testcase received Firm 4
Testcase received Firm 5
PASSED: sendCmd on Firm 0
PASSED: sendCmd on Firm 1
PASSED: sendCmd on Firm 2
PASSED: sendCmd on Firm 3
PASSED: sendCmd on Firm 4
PASSED: sendCmd on Firm 5
===============================================
Sample_Test
Tests run: 6, Failures: 0, Skips: 0
===============================================
===============================================
Sample_Test_Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
我遇到的这个问题。如上所述,我有一个解决方案。