java - need to execute setup method only once in parallel execution mode -
i've parallel test cases execution setup testng need execute setup method once.
beforeclass, beforemethod gets executed individual threads. need execute method once before threads.
how achieve 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>
the following sample should explain 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 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 output:
... ... testng 6.12 cédric beust (cedric@beust.com) ... initialisation done test method running on [12] test method running on [11] =============================================== 45371087_suite total tests run: 2, failures: 0, skips: 0 ===============================================
Comments
Post a Comment