简体   繁体   English

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

[英]Run parallel test methods which are dependent with different parameter using Data Provider in TestNg

I want to run test methods which are dependent and use ITestContext, to run in parallel with different parameters using Data Provider in TestNg.我想运行依赖并使用 ITestContext 的测试方法,以使用 TestNg 中的数据提供程序与不同的参数并行运行。 I am trying to call the test class pragmatically.我正在尝试务实地调用测试 class 。

The code looks like this:代码如下所示:

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();
    }

}

My functionality is to call the test class multiple times with different parameters and the classes being called should run parallelly我的功能是使用不同的参数多次调用测试 class 并且被调用的类应该并行运行

The issue with this is that the testclass1 would be invoked three times, but the testclass2 and testclass3 would be invoked only once because, the execution of those methods does not depend on the number of times testclass1 is executed.这样做的问题是testclass1将被调用 3 次,但testclass2testclass3只会被调用一次,因为这些方法的执行不依赖于testclass1的执行次数。 It only depends on whether testclass1 passed or not.它只取决于testclass1是否通过。 To solve the issue, you need to use @Factory要解决此问题,您需要使用@Factory

public class ParallelMethodTest {

    private final String deviceId;

    ParallelMethodTest(String id) {
        this.deviceId = id;
    }

    @DataProvider(parallel = true)
    public static Object[][] dataProvider() {
        return new Object[][] { { "1001" }, { "1002" }, { "1003" } };
    }

    @Factory(dataProvider = "dataProvider")
    public Object[] createInstances(String id) {
        // The factory method uses the dataProvider to initialize
        // multiple instances of the test class.
        return new Object[] { new ParallelMethodTest(id) };
    }

    @Test
    public void testclass1() {

        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);
    }

    @Test(dependsOnMethods = { "testclass1" })
    public void testclass2() {
        long id = Thread.currentThread().getId();
        System.out.println("TestClass2 is being called and context is being retrieved " + deviceId);
        System.out.println("The thread id is " + id + "\n");
    }

    @Test(dependsOnMethods = { "testclass2" })
    public void testclass3() {
        long id = Thread.currentThread().getId();
        System.out.println("TestClass3 is being called and context is being retrieved " + deviceId);
        System.out.println("The thread id is " + id);
    }
}

With this, there is no need for storing the deviceId in test context as the corresponding deviceId would be available in the class itself.这样,就无需在测试上下文中存储deviceId ,因为相应的deviceId将在 class 本身中可用。

UPDATE : To run in parallel, you need to do one more thing inside DynamicXMLSetuo .更新:要并行运行,您需要在DynamicXMLSetuo中再做一件事。

suite.setParallel(XmlSuite.ParallelMode.METHODS);

By adding ParallelMode.METHODS , TestNG will run all your test methods in separate threads.通过添加ParallelMode.METHODS , TestNG 将在单独的线程中运行所有测试方法。 Dependent methods will also run in separate threads but they will respect the order that you specified.依赖方法也将在单独的线程中运行,但它们将遵循您指定的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用testng并行运行数据提供者测试 - Run data provider tests in parallel using testng 无法通过来自数据提供商的 TestNg 运行并行测试 - Unable to run parallel test via TestNg from DATA provider 如何使用TestNG在两个不同的浏览器中并行运行两个测试方法? - How to run two test methods in two different browser in parallel using TestNG? 如何使用testng并行运行我的硒测试方法 - how to run my selenium test methods in parallel using testng 如何使用数据提供程序在 testNG 上并行运行测试? - How to run tests in parallel on testNG using data provider? 在Testng中使用数据提供程序和并行方法,如何在给定测试的同一线程中在方法之前,方法之后和测试中运行? - Using data-provider and parallel in Testng, how to run before method, after method and test in same thread for a given test.? 如何使用TestNG以连续顺序运行@Test的前三个方法和使用@Test并行的后三个方法? - How to run the first three methods with @Test in sequential order and next three methods with @Test in parallel, using TestNG? 使用testNG并行运行测试 - Make test run in parallel using testNG 在IDE中使用RUN选项运行时,使用TestNG XML并行执行运行良好,但是mvn clean测试失败,并出现与参数相关的错误 - Parallel execution using TestNG XML runs fine when run using the RUN option in an IDE but mvn clean test fails with a parameter related error 使用不同的数据提供程序在TestNG中重复整个测试类 - Repeat entire test class in TestNG, with different data provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM