St.Antario
St.Antario

Reputation: 27375

Run method before each test

Im using testng 6.11 and writing tests in the following test class:

public class MyTest{

    public int i;

    public void init(){
        //initialize i
    }

    @Test
    public void test1(){
        //test some
    }

    @Test
    public void test2(){
        //Here I need fresh value of i as it would be
        //right after invocation of init()
        //...
        //test something else
    }
}

Is it possible to make testng run init() method before invocation of each test in a test class?

Upvotes: 2

Views: 8963

Answers (3)

Rohan
Rohan

Reputation: 3078

You can use @BeforeMethod annotatation to execute any method before every test.

Example

Upvotes: 0

Guilherme
Guilherme

Reputation: 623

Sure, your can use the annotation for that

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

Upvotes: 0

Michael
Michael

Reputation: 1467

Annotate init() with @BeforeMethod annotation. See http://testng.org/doc/documentation-main.html#annotations

Upvotes: 6

Related Questions