簡體   English   中英

使用 TestNg 中的數據提供程序運行依賴於不同參數的並行測試方法

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

我想運行依賴並使用 ITestContext 的測試方法,以使用 TestNg 中的數據提供程序與不同的參數並行運行。 我正在嘗試務實地調用測試 class 。

代碼如下所示:

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

}

我的功能是使用不同的參數多次調用測試 class 並且被調用的類應該並行運行

這樣做的問題是testclass1將被調用 3 次,但testclass2testclass3只會被調用一次,因為這些方法的執行不依賴於testclass1的執行次數。 它只取決於testclass1是否通過。 要解決此問題,您需要使用@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);
    }
}

這樣,就無需在測試上下文中存儲deviceId ,因為相應的deviceId將在 class 本身中可用。

更新:要並行運行,您需要在DynamicXMLSetuo中再做一件事。

suite.setParallel(XmlSuite.ParallelMode.METHODS);

通過添加ParallelMode.METHODS , TestNG 將在單獨的線程中運行所有測試方法。 依賴方法也將在單獨的線程中運行,但它們將遵循您指定的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM