TestNg's #BeforeTest on base class only happening once per fixture - java

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.

"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 :)

According 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:
Each #BeforeTest method is run only once
If you have several #BeforeTest methods, the order of their execution depends on the order of the class containing those #BeforeTest method.
You could test this by setting up a simple example.

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 class
All the #befortests within all the classes within a <test> will be executed at the beggining of that test

Official Documentations are so unclear and mostly ambiguous. This is why many don't like to read it. Not to mention no real-life examples shown.
If you are trying to get code to run once before every test, then you are looking for #BeforeMethod, not #BeforeTest. Because each of your tests independently considered methods.
#BeforeMethod runs before each method. I see you have two methods with the names test1() and test2(). Expect BeforeMethod to run before each of them.
#BeforeTest runs only once before ALL of your tests (test-methods). In Selenium, WebDriver is called once, this is better practice for testing.
#BeforeMethod will invoke WebDriver before each test, this is not good practice, especially when you have regression tests with hundreds of them to run.
For example, if you run this code (these are separate classes, not inner to each other, only showing for demonstration purposes here):
public class TestClass extends Base {
#Test
public void test1() {
System.out.println("Running Test 1");
}
#Test
public void test2() {
System.out.println("Running Test 2");
}
}
public class Base {
#BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
#AfterTest
public void afterTest() {
System.out.println("After Test");
}
#BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
#AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
}
And you will get this as an output:
Before Test
Before Method
Running Test 1
After Method
Before Method
Running Test 2
After Method
After Test
As you can see BeforeMethods run before each test while BeforeTest and AfterTest run only once before and after completing ALL tests.
Hope this clarified the difference for you.

Related

How to execute a piece of code once before all test classes start?

How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.

Why doesn't my #BeforeClass method run when executing a subclass test method?

I'm using IntelliJ IDEA CE 2018.3 and JUnit 4.12.
I have a test class that looks like this:
#RunWith(HierarchicalContextRunner.class)
public class TestClass {
#BeforeClass
public static void beforeAll() {
//start a server for all tests to hit
}
#Before
public void before() {
//init a common request object for each test
}
#Test
public void itShouldHaveSomeCommonProperty() {
//check some common thing
}
public class SomeSubTestClass {
#Before
public void before() {
//do some test case-specific setup
}
public class SomeOtherSubTestClass {
#Test
public void itShouldDoSomething() {
//hit the service and assert something about the result
}
}
}
}
When I tell IntelliJ to run the class, everything works as expected. However, if I want to just run the itShouldDoSomething test (which I'm doing by setting up a run configuration that targets the SomeOtherSubTestClass class), the beforeAll method is not executed. Both of the before methods are executed in the correct order, but not the static beforeAll method.
Am I misunderstanding something, or is this a bug?
It is not a bug.
The beforeAll method is static and therefore tied to the class and not the instance. This is why it is not executed when calling tests in inner classes or sub-classes.
To ensure it being called you would have to define a #BeforeClass method in each of your inner classes which then call the method on the outer class.

JUnit - Use method in test from another class

The scenario is the following:
I have a class that executes tests for a section of a project.
public class section1 extends BaseTest {
#Test
public void test1() {
//somecode
}
}
The thing is that in test1, I want to use a Method that's defined in another test class (we're not using Page Objects in the automation project).
public class section2 extends BaseTest {
#Test
public void test2() {
//somecode
methodOne();
}
void methodOne(){
//somecode
}
}
So, I've tried creating an instance and access to the method through it
section2 sec2 = new section2();
.
.
.
#Test
public void test1() {
//somecode
section2.methodOne();
}
But it throws a NullPointerException as soon as it starts executing the method.
Then, I tried just extending the class section1 to section2 and use the method, so here it executes the method but, as soon as test1 finishes, it starts executing the tests from the extended class, in this case, test2.
How can I do to prevent that, or is there another way to use the method without extending and not making the method public?
Section 1 extends BaseTest
Section 2 extends BaseTest
Why don't you try this :
Section 2 extends Section 1
Add the below code in the section1 class:
section2 sec2;
#Before
public void initialize() {
sec2 = new section2();
}
As we are using the #Before JUnit annotation above, this 'initialize()' method will get executed first there by initializing the 'section2' class.
#Test
public void test1() {
sec2.methodOne();
}
Then in the #Test JUnit method will get executed so you won't get NPE.

How to use #BeforeTest in separate java file in testng

I am writing automation testing script for a project . Here , I will have many test classes for which I will be using same #BeforeTest Method . I tried by creating a base class (in which I declared before test method ) and extending them in my test classes . But its not working . Is there any other way to have a common beforeTest method in a seperate java file and use it for all my test classes .
Using a base class for my other classes works for me. You should use #BeforeMethod for your needs since #BeforeTest is used to run any #Test included in the <test> tag in a testNG.xml file.
public class BaseClass {
#BeforeMethod
public void before() {
System.out.println("Before method");
}
}
and then
public class ATestClass extends BaseClass {
#Test
public void testOne() {
System.out.println("testOne run");
}
#Test
public void testTwo() {
System.out.println("testTwo run");
}
}
gave me the result
Try it out!
If you are using PowerMockito or Mockito you can achieve by following.
public abstract class ParentClass{
#Before
public void init() {
System.out.println("inniinii");
}
}
#RunWith(PowerMockRunner.class)
public class ChildClass extends ParentClass{
#Test
public void test() {
System.out.println("hello test");
}
}

JUnit, Selenium and tearDown

Such a junit :
#Test
public void testA {
//some code here...
}
#Test
pulic void testB {
//some code here...
}
#After
public void closeBrowsers() throws Exception {
selenium.stop();
}
Here is the question : closeBrowsers() method called after every test method; in that case it is called twice and i got "Wrong test finished." from JUnit. I need a junit method/annotation which will be called after all tests finised (just called once after all tests finished), is it possible ?
Also i tried to check if selenium is up or not in closeBrowsers() but no way i couldn't find
any solution.
P.S : I 've read this one : How to close a browser on a selenium RC server which lost it's client
but i couldn't understand the solution and also currently http://www.santiycr.com.ar/djangosite/blog/posts/2009/aug/25/close-remaining-browsers-from-selenium-rc blog side is down
You can make your selenium variable static, initialize it in #BeforeClass static method and cleanup in #AfterClass:
public class ...... {
private static Selenium selenium;
#BeforeClass
public static void initSelenium() {
selenium = new DefaultSelenium(...); // or initialize it in any other way
}
#Test
public void testA {...}
#Test
pulic void testB {...}
#AfterClass
public static void closeBrowsers() throws Exception {
selenium.stop();
}
}
Use the #AfterClass annotation.
http://junit.sourceforge.net/doc/faq/faq.htm#organize_3

Categories

Resources