Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG: @Test at class level executing all methods

Tags:

testng

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

like image 256
FkJ Avatar asked Jun 26 '14 12:06

FkJ


People also ask

Can the @test annotation be applied on the class level instead of method level?

Can we use @Test annotation on a class? Yes, We can use @Test annotation on class.

What is @test annotation in TestNG?

@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.

What are the attributes of @test in TestNG?

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.


2 Answers

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");
    }
}
like image 161
niharika_neo Avatar answered Oct 12 '22 23:10

niharika_neo


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">
like image 37
Rick Avatar answered Oct 12 '22 23:10

Rick