简体   繁体   中英

TestNG - How to run setup once before all tests in a class for each parallel run

I am trying to see if it is possible to have a setup run once for each parallel class instantiated.

If I have the following test class, because of the DataFactory (CSV has 2 rows of data, one for each test), two tests are started in parallel

Is it possible to get testSetup() to run once for each instance of the TestFu class. BeforeClass seems to run it once before both parallel test instances.

TestXML

<suite name="SomeTest" parallel="none" thread-count="20" verbose="2">
    <test name="Graph+APITests" parallel="instances" thread-count="5">
        <classes>
            <class name="TestFu" />
        </classes>
    </test>
</suite>

Test Class

public class TestFu {

    String var1;

    @Factory(dataProvider = "testStuff")
    public TestFu(String var1, String var2) {
        this.var1 = var1;
        this.var2 = var2;
    }

    @DataProvider(name = "testStuff")
    public static Object[][] stuff() {

        return methodThatLoadsVarsFromCSV;
    }

    @BeforeClass
    public void testSetup() {
        System.out.println("Doing setup");
    }

    @Test
    public void testOne() {
        System.out.println("Test 1 " + this.var1);
    }

    @Test
    public void testTwo() {
        System.out.println("Test 2 " + this.var2);
    }
}

Use static flag like this:

    static boolean initialized = false;

    @BeforeClass
    public void testSetup() {
        if (!initialized) {
            initialized = true;
            System.out.println("Doing setup");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM