0
votes

I've parallel test cases execution setup with testng but I need to execute setup method only once.

BeforeClass, BeforeMethod also gets executed for individual threads. But I need to execute method once before all the threads.

How to achieve this with TestNG setup?

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelSuiteTest 
{
    String testName = "";

    @BeforeTest
    @Parameters({ "test-name" })
    public void beforeTest(String testName) {
        this.testName = testName;
        long id = Thread.currentThread().getId();
        System.out.println("Before test " + testName + ". Thread id is: " + id);
    }

    @BeforeClass
    public void beforeClass() {
        long id = Thread.currentThread().getId();
        System.out.println("Before test-class " + testName + ". Thread id is: "
                + id);
    }

    @Test
    public void testMethodOne() {
        long id = Thread.currentThread().getId();
        System.out.println("Sample test-method " + testName
                + ". Thread id is: " + id);
    }

    @AfterClass
    public void afterClass() {
        long id = Thread.currentThread().getId();
        System.out.println("After test-method  " + testName
                + ". Thread id is: " + id);
    }

    @AfterTest
    public void afterTest() {
        long id = Thread.currentThread().getId();
        System.out.println("After test  " + testName + ". Thread id is: " + id);
    }
}

testng.xml

<suite name="Test-class Suite" parallel="tests" thread-count="2">
    <test name="Test-class test 1">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
    <test name="Test-class test 2">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
</suite>
1
Why not build that logic within your @BeforeClass annotated method which looks at a shared boolean to check if init() has been called already before calling it ? TestNG AFAIK doesnt offer anything out of the box that would satisfy this need.Krishnan Mahadevan
But doesn't parallel execution calls @BeforeClass at same time ? In this case both multiple threads will be calling init method at same instance ?vikramvi
Parallel execution is for @Test methods IMO and is not for @BeforeClass. What happens when you try it ? Do you have a sample test which can show the problem ?Krishnan Mahadevan
Please refer to updated question with example, in this @BeforeClass gets called twice for each of the threads so does other Before & After methodsvikramvi
@BeforeClass gets called once for every test class. You have two test classes each of which is embedded in a <test> tag. But you should still be able to have both of your @BeforeClass methods call into one global init() guarded by a synchronized block and which internally checks its invocation state by inspecting a boolean.Krishnan Mahadevan

1 Answers

2
votes

The following sample should explain what I was suggesting.

package com.rationaleemotions.stackoverflow.qn45371087;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ParallelSuiteTest {
    private static final Object lock = new Object();
    private static boolean initialised = false;

    @BeforeClass
    public void beforeClass() {
        synchronized (lock) {
            if (!initialised) {
                init();
                initialised = true;
            }
        }
    }

    private void init() {
        System.err.println("Initialisation done");
    }

    @Test
    public void testMethodOne() {
        System.err.println("This is a test method running on [" + Thread.currentThread().getId() + "]");
    }

}

Suite xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45371087_Suite" verbose="2" parallel="tests" thread-count="10">
    <test name="45371087_Tests_1">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/>
        </classes>
    </test>
    <test name="45371087_Tests_2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/>
        </classes>
    </test>
</suite>

Here's the output:

...
... TestNG 6.12 by Cédric Beust ([email protected])
...
Initialisation done
This is a test method running on [12]
This is a test method running on [11]

===============================================
45371087_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================