Java 如何让TestNG-@Factory以特定顺序实例化测试类?
我正在使用TestNG进行硒测试。 有一个名为“创建公司”的测试需要在我的笔记本电脑上运行多次 因此,我为此编写了一个名为“CreateFirm”的类,各种公司的数据保存在excel电子表格中 在不同的时候,我需要创建不同的公司集合,我使用Excel电子表格中的一列来控制这些公司,该列保存着我的计算机名 我使用@Factory来创建我的“CreateFirm”类,它有一个@Test方法来创建公司 在excel电子表格中,如果我已将Firm1、Firm2、Firm3、Firm4以相同的顺序分配给我的笔记本电脑,@Factory会以随机顺序创建它们,如Firm4、Firm3、Firm1、Firm2 我的问题是如何让@Factory按照我想要的顺序创建测试实例 我的@Factory方法是Java 如何让TestNG-@Factory以特定顺序实例化测试类?,java,selenium,testng,Java,Selenium,Testng,我正在使用TestNG进行硒测试。 有一个名为“创建公司”的测试需要在我的笔记本电脑上运行多次 因此,我为此编写了一个名为“CreateFirm”的类,各种公司的数据保存在excel电子表格中 在不同的时候,我需要创建不同的公司集合,我使用Excel电子表格中的一列来控制这些公司,该列保存着我的计算机名 我使用@Factory来创建我的“CreateFirm”类,它有一个@Test方法来创建公司 在excel电子表格中,如果我已将Firm1、Firm2、Firm3、Firm4以相同的顺序分配给我
@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;
}
@工厂
公共对象[]runCreateFirm()
{
//这是我获得分配给笔记本电脑的测试用例列表的地方
test\u id\u list=get\u test\u id\u for\u test\u run(“创建公司(类方法).xls”,“全局”);
Object[]result=新对象[this.test_id_list.size()];
int指数=0;
用于(字符串公司id:此.test\u id\u列表)
{
//这是我从Excel电子表格中获取所有测试数据的地方
HashMap test\u data\u row=这个。从excel中获取行(“创建公司(类方法).xls”,“全局”,“测试案例id”,“公司id”);
System.out.println(“公司工厂内部,索引=“+index+”,测试用例id=“+test_data_row.get”(“test_case_id”));
//CreateFirm是一个类,它将使用数据并执行所有UI操作来创建公司
结果[索引]=新创建的公司(测试数据行);
索引++;
}
返回结果;
}
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>
工厂实例化测试类,但不运行它们。如果您需要测试的特定顺序,那么在依赖项(组和方法)、优先级和方法拦截器之间有很多选择。工厂实例化测试类,但不运行它们。如果您的测试需要特定的顺序,那么在依赖项(组和方法)、优先级和方法拦截器之间有很多选择。根据我的要求编写了拦截器方法。
首先,我在课堂上引入了一个新的领域,叫做“优先级”。我不能使用@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;
}
公共列表截取(列表方法、ITestContext上下文)
{
列表结果=新建ArrayList();
int数组_索引=0;
对于(i方法m:方法)
{
结果:添加(m);
}
//现在迭代这些测试方法
对于(i方法m:方法)
{
试试{
//从-Methodobj->method->class->FIELD获取字段对象
字段f=m.getMethod().getRealClass().getField(“优先级”);
//获取方法对象的对象实例
数组_index=f.getInt(m.getInstance());
}
捕获(例外e){
e、 printStackTrace();
}
结果集(数组_索引-1,m);
}
返回结果;
}
成功地为我的需求编写了拦截器方法。
首先,我在课堂上引入了一个新的领域,叫做“优先级”。我不能使用@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;
}
公共列表截取(列表方法、ITestContext上下文)
{
列表结果=新建ArrayList();
int数组_索引=0;
对于(i方法m:方法)
{
结果:添加(m);
}
//现在迭代这些测试方法
对于(i方法m:方法)
{
试试{
//从-Methodobj->method->class->FIELD获取字段对象
字段f=m.getMethod().getRealClass().getField(“优先级”);
//获取方法对象的对象实例
数组_index=f.getInt(m.getInstance());
}
捕获(例外e){
e、 printStackTrace();
}
结果集(数组_索引-1,m);
}
返回结果;
}
参考了解决方案,并从krmahadevan给出的示例中得到了提示。非常感谢KR。我正在Eclipse中使用TestNG插件版本6.14.0.201802161500
只需在测试类中重写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;
}
}
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)};
}
}
FactoryClass.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;
}
}
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)};
}
}
结果
我面临的这个问题。我得到了如上所述的解决方案。引用了该解决方案,并从krmahadevan给出的示例中得到了提示。非常感谢KR。我正在Eclipse中使用TestNG插件版本6.14.0.201802161500
只需在测试类中重写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;
}
}
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)};
}
}
FactoryClass.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;
}
}
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方法,所以我不能使用依赖项、优先级或组来指定测试类的运行顺序。我将调查MethodIntercep