Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNg's @BeforeTest on base class only happening once per fixture

I'm trying to use @BeforeTest to get code to ... run once before every test.

This is my code:

public class TestBase {     @BeforeTest     public void before() {         System.out.println("BeforeTest");     } }  public class TestClass extends TestBase{     @Test     public void test1(){}      @Test     public void test2(){} } 

"BeforeTest" is only printed once, not twice. What am I doing wrong?

like image 815
ripper234 Avatar asked Nov 30 '10 05:11

ripper234


People also ask

What is the difference between @BeforeTest and @BeforeMethod in TestNG?

@BeforeTest will execute only one time before any test methods. Methods will run before executing any @Test annotated test method that is part of the <test> tag in testNG. xml file. @BeforeMethod will execute before every method annotated with @Test .

What is the difference between @BeforeTest and @BeforeClass?

@BeforeClass: This will be executed before first @Test method execution. It will be executed one only time throughout the test case. @BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case.

What is the correct execution order in TestNG?

Default Order TestNG executes different tests alphabetically. By default, test1 will run first and after that test2 and finally test3.

What is the sequence of execution of all TestNG annotations?

First of all, beforeSuite() method is executed only once. Lastly, the afterSuite() method executes only once. Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once. beforeMethod() method executes for each test case but before executing the test case.


2 Answers

Use @BeforeMethod, not @BeforeTest.

The meaning of @BeforeTest is explained in the documentation.

like image 128
Cedric Beust Avatar answered Oct 01 '22 07:10

Cedric Beust


"BeforeTest" is only printed once, not twice. What am I doing wrong?

***Sorry. I haven't noticed that you is written @BeforeTest , but in your example @BeforeTest almost equals @BeforeClass , and better to use @BeforeClass , when you haven't anymore test classes.

@BeforeClass" should be declared in same class that your tests methods, not differently!

//Example  package test; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test;  public class Tests { private String bClass; private String bMethod1; private String bMethod2;  @BeforeClass public void beforeClass() {     bClass = "BeforeClass was executed once for this class"; }  @BeforeMethod public void beforeMetodTest1() {     bMethod1 = "It's before method for test1"; }  @Test public void test1() {     System.out.println(bClass);     System.out.println(bMethod1); }  @BeforeMethod public void beforeMethodTest2() {     bMethod2 = "It's before method for test2"; }  @Test public void test2() {     System.out.println(bClass);     System.out.println(bMethod2); } } 

@BeforeClass will executed once, before your all tests methods in this class. @BeforeMethod will executed before test method, before which it is written.

@BeforeClass may be only one in test class, in difference @BeforeMethod!(If it is some @BeforeClass, they are carried out by turns, but it not a correct composition of the test)

P.S. Sorry for my English :)

like image 38
Raman Avatar answered Oct 01 '22 07:10

Raman