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?
Use @BeforeMethod, not @BeforeTest.
The meaning of @BeforeTest is explained in the documentation.
***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!
@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 :)
If you use @beforeTest, that method will be run once in the beginning of every
<test>
(we specify in the test suit xml file) if that test contains that classAll the @befortests within all the classes within a
<test>
will be executed at the beggining of that testAccording to documentation, a method annotated with @BeforeTest is run before any @Test method belonging to the classes inside the tag is run.
From my experience:
You could test this by setting up a simple example.