I have some tests that need to run sequentially, so I added @Test(singleThreaded = true) to their classes.
Works fine, the problem is that with @Test at class level, all methods are executed by TestNG, even if they don't have a @Test annotation, so eventually this causes some time waste when someone from my team wants to disable a test, and as he's not aware of this particularity he only comments the @Test instead of whole method, so later this test that should be disabled end up making the build process fail.
Is there a way to avoid this?
Thanks
Can we use @Test annotation on a class? Yes, We can use @Test annotation on class.
@AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG. xml file. @BeforeSuite: It will run only once, before all tests in the suite are executed.
Hey @Iqbal, some of the most common attributes for @Test annotations are: description: gives information about the test it is attached to. timeOut: maximum number of milliseconds for a test run. priority: specifies when to run a test.
One way is to educate the team :)
You can just put a new annotation on the single test you want to disable i.e. In the below, test 2 won't run.
@Test(singlet..)
public class Testss {
public void test1(){
System.out.println("test1");
}
@Test(enabled=false)
public void test2(){
System.out.println("test2");
}
public void test3(){
System.out.println("test3");
}
}
If executing test sequentially is important I would suggest using dependencies or priority instead of relying on execution order and changing the thread count.
@Test
public void method1() {
System.out.println("This is method 1");
}
@Test(dependsOnMethods = { "method1" })
public void method2() {
System.out.println("This is method 2");
}
or
@Test(priority = 1)
public void method1() {
System.out.println("This is method 1");
}
@Test(priority = 2)
public void method2() {
System.out.println("This is method 2");
}
in conjunction with
@Test(enabled = false) //do not run this method as a test
and/or
<suite name="Suite" parallel="classes" thread-count="3">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With