40

Both annotations runs before the @test in testNG then what is the difference between two of them.

Sameer Dev
  • 401
  • 1
  • 4
  • 4
  • Possible duplicate of [Difference between BeforeClass and BeforeTest in TestNG](https://stackoverflow.com/questions/30587454/difference-between-beforeclass-and-beforetest-in-testng) – Ori Marko Jun 12 '18 at 10:38

9 Answers9

38

check below code and output

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_BeforeTestAndBeforeMethod {

    @BeforeTest
    public void beforeTest()
    {
        System.out.println("beforeTest");
    }

    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("\nbeforeMethod");
    }


    @Test
    public void firstTest()
    {
        System.out.println("firstTest");
    }

    @Test
    public void secondTest()
    {
        System.out.println("secondTest");
    }

    @Test
    public void thirdTest()
    {
        System.out.println("thirdTest");
    }
}

output:

beforeTest

beforeMethod
firstTest

beforeMethod
secondTest

beforeMethod
thirdTest
irfan
  • 878
  • 9
  • 10
30

@BeforeTest : It will call Only once, before Test method.

@BeforeMethod It will call Every time before Test Method.

Example:

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_BeforeTestAndBeforeMethod {

    @BeforeTest
    public void beforeTestDemo()
    {       
        System.out.println("This is before test calling.");   
    }
    
    @BeforeClass
    public void beforeClassDemo()
    {
        System.out.println("This is before class calling.");
    }

    @BeforeMethod
    public void beforeMethodDemo()
    {
        System.out.println("This is before method calling.");
    }
    
    @Test
    public void testADemo()
    {
        System.out.println("This is Test1 calling.");
    }

    @Test
    public void testBDemo()
    {
        System.out.println("This is Test2 calling.");
    }

    @Test
    public void testCDemo()
    {
        System.out.println("This is Test3 calling.");
    }
    
    @AfterMethod
    public void afterMethodDemo()
    {
        System.out.println("This is after method calling.");
    }
    
    @AfterClass
    public void afterClassDemo()
    {
        System.out.println("This is after class calling.");
    }
    
    @AfterTest
    public void afterTestDemo()
    {
        System.out.println("This is after test calling.");
    }
}

Reference O/P Example

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
  • 1
    testCDemo, `This is Test2 calling` -> `This is Test3 calling.` – Vitalii Diravka Jun 17 '20 at 08:28
  • @IshitaShah - All my test methods instantiate a variable and use it. Should I convert the variable into class member and instantiate it inside "@BeforeMethod" ? Is there another way of doing it ? – MasterJoe Jul 01 '20 at 19:15
12

@BeforeTest : It will be called Only one time befor any test methods, no matter how many method annotaed with @Test, it will be called only one time

@BeforeMethod It will be called befor every methode annotated with @Test, if you have 10 @Test methods it will be called 10 times

To know what is the Difference between BeforeClass and BeforeTest, please refer to the answer https://stackoverflow.com/a/57052272/1973933

Melad Basilius
  • 3,847
  • 10
  • 44
  • 81
5

I know there have already been several good answers to this question, I just wanted to build on them to visually tie the annotations to the xml elements in testng.xml, and to include before/after suite as well.
I tried to keep it as newbie friendly as possible, I hope it helps.

My java example is basically just a reformatted version of Ishita Shah's code.


BeforeAfterAnnotations.java (assumes this file is in a package called "test")

package test;




import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;



public class BeforeAfterAnnotations
{
    @BeforeSuite
    public void beforeSuiteDemo()
    {
        System.out.println("\nThis is before a <suite> start tag.");
    }


    @BeforeTest
    public void beforeTestDemo()
    {
        System.out.println("\tThis is before a <test> start tag.");
    }


    @BeforeClass
    public void beforeClassDemo()
    {
        System.out.println("\t\tThis is before a <class> start tag.\n");
    }


    @BeforeMethod
    public void beforeMethodDemo()
    {
        System.out.println("\t\t\tThis is before a method that is annotated by @Test.");
    }


    @Test
    public void testADemo()
    {
        System.out.println("\t\t\t\tThis is the testADemo() method.");
    }


    @Test
    public void testBDemo()
    {
        System.out.println("\t\t\t\tThis is the testBDemo() method.");
    }


    @Test
    public void testCDemo()
    {
        System.out.println("\t\t\t\tThis is the testCDemo() method.");
    }


    @AfterMethod
    public void afterMethodDemo()
    {
        System.out.println("\t\t\tThis is after a method that is annotated by @Test.\n");
    }


    @AfterClass
    public void afterClassDemo()
    {
        System.out.println("\t\tThis is after a </class> end tag.");
    }


    @AfterTest
    public void afterTestDemo()
    {
        System.out.println("\tThis is after a </test> end tag.");
    }


    @AfterSuite
    public void afterSuiteDemo()
    {
        System.out.println("This is after a </suite> end tag.");
    }
}



testng.xml (testng.xml --> run as --> TestNG Suite)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Before/After Annotations Suite">
    <test name="Before/After Annotations Test">
        <classes>
            <class name="test.BeforeAfterAnnotations" />
        </classes>
    </test>
</suite> 



Output to console

[RemoteTestNG] detected TestNG version 7.0.0

This is before a <suite> start tag.
    This is before a <test> start tag.
        This is before a <class> start tag.

            This is before a method that is annotated by @Test.
                This is the testADemo() method.
            This is after a method that is annotated by @Test.

            This is before a method that is annotated by @Test.
                This is the testBDemo() method.
            This is after a method that is annotated by @Test.

            This is before a method that is annotated by @Test.
                This is the testCDemo() method.
            This is after a method that is annotated by @Test.

        This is after a </class> end tag.
    This is after a </test> end tag.
This is after a </suite> end tag.

===============================================
Before/After Annotations Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

TGandhi
  • 93
  • 1
  • 5
2

In TestNG

@BeforeMethod - BeforeMethod executes before each and every test method. All methods which uses @Test annotation. @BeforeMethod works on test defined in Java classes.

@BeforeTest - BeforeTest executes only before the tag given in testng.xml file. @BeforeTest works on test defined in testng.xml

Reference:- https://examples.javacodegeeks.com/enterprise-java/testng/testng-beforetest-example/ and http://howtesting.blogspot.com/2012/12/difference-between-beforetest-and.html

nandal
  • 2,544
  • 1
  • 18
  • 23
2

@BeforeTest- runs before each test declared inside testng.xml

@BeforeMethod- runs before each test method declared within class and under an @Test annotation

Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
1

@BeforeTest is executed before any beans got injected if running an integration test. In constrast to @BeforeMethod which is executed after beans injection. Not sure why this was designed like this.

nanachimi
  • 406
  • 6
  • 23
0

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

Don
  • 3,876
  • 10
  • 47
  • 76
Sonal
  • 1
  • 1
0

@BeforeTest To execute a set-up method before any of the test methods included in the < test > tag in the testng.xml file. @BeforeMethod To execute a set-up method before any of the test methods annotated as @Test.