有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用TestNg中的数据提供者运行依赖于不同参数的并行测试方法

我想运行依赖于ITestContext的测试方法,使用TestNg中的数据提供程序与不同的参数并行运行。我试着用实用主义的方式称呼这个测试班

代码如下所示:

package com.ExploringTestNg;

import java.util.Random;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ParallelMethodTest {
    
    @Test(dataProvider = "dataprovider")
    public void testclass1(ITestContext context,String deviceId) {
        
        long id = Thread.currentThread().getId();
        System.out.println("TestClass1 is being called and context is being setup");
        System.out.print("The thread id is "+id);
        System.out.println("device id is "+deviceId);
        context.setAttribute("deviceId",deviceId);
    }
    @Test(dependsOnMethods = {"testclass1"})
    public void testclass2(ITestContext context) {
        long id = Thread.currentThread().getId();
        System.out.println("TestClass2 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
        System.out.println("The thread id is "+id+"\n");
    }
    @Test(dependsOnMethods = {"testclass2"})
    public void testclass3(ITestContext context) {
        long id = Thread.currentThread().getId();
        System.out.println("TestClass3 is being called and context is being retrieved "+(String)context.getAttribute("deviceId"));
        System.out.println("The thread id is "+id);
    }
    @DataProvider(name = "dataprovider",parallel = true)
    public Object[][] getDataFromDataprovider(){
        return new Object[][]
                {
                    {"1001"},
                    {"1002"},
                    {"1003"}
                };
    }
}
package com.ExploringTestNg;

import java.util.ArrayList;
import java.util.List;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class DynamicXMLSetuo  {

    public static void main(String[] args) {
        XmlSuite suite = new XmlSuite();
        suite.setName("TempSuite1");
        XmlTest test = new XmlTest(suite);
        test.setName("Functional usecase 1");
        List<XmlClass> classes = new ArrayList<XmlClass>();
        classes.add(new XmlClass("com.ExploringTestNg.ParallelMethodTest"));
        test.setXmlClasses(classes);
        
        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        TestNG tng = new TestNG();
        tng.setXmlSuites(suites);
        tng.setThreadCount(5);
        tng.setParallel(XmlSuite.ParallelMode.METHODS);
        tng.run();
    }

}

我的功能是使用不同的参数多次调用测试类,被调用的类应该并行运行


共 (0) 个答案