0

I have two classes and each class contains 2 test cases and Test1 class having one method with @BeforeClass as per me this method should run before Test2 class too but it is not running.

    package WebPackage;

    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    public class Test1 {
        @BeforeClass
        public void test1() {

            System.out.println("printing Before Class Method");
        }
        @Test (priority = 1)
    public void test2() {

            System.out.println("printing test_2");
        }

        @Test (priority = 3)
    public void test3() {

            System.out.println("printing test_3");
        }
    }

Test2

    package WebPackage;

    import org.testng.annotations.Test;

    public class Test2 {

        @Test (priority = 1)
        public void test4() {

                System.out.println("printing test_4");
            }

            @Test (priority = 3)
        public void test5() {

                System.out.println("printing test_5");
            }
    }

Xml file


    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    <suite name="Menu">
      <test name="WebPackage">
        <classes>
          <class name="WebPackage.Test1"/>
         <class name="WebPackage.Test2"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

Console

[RemoteTestNG] detected TestNG version 7.0.0
printing Before Class Method
printing test_2
printing test_3
printing test_4
printing test_5

===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
halfer
  • 19,824
  • 17
  • 99
  • 186
Tester
  • 37
  • 6
  • The `@BeforeClass` in your example behaves exactly as I would expect. see http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html. You could put the `@BeforeClass` in a superclass of test1 and test2, would that do what you try to achieve? – Len Nov 07 '19 at 12:59

3 Answers3

0

BeforeClass runs only once when Test class is started. Hence. it executes only once per test class. Use @Before if you want to execute it with every test method in test class.

Read ref - Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
0

@BeforeClass will run only once before running any method annotated with @Test in that class. It will not run again for any method in your Test2 class.

Moro
  • 781
  • 4
  • 14
0

You can make a BaseTest class with @BeforeClass annotation and then each Test class extend with BaseTest.

BaseTest:

public class BaseTest {
    @BeforeClass
    public void test1() {
        System.out.println("printing Before Class Method");
    }
}

Test1:

public class Test1 extends BaseTest {
    @Test (priority = 1)
    public void test2() {
        System.out.println("printing test_2");
    }

    @Test(priority = 3)
    public void test3() {
        System.out.println("printing test_3");
    }
}

Test2:

public class Test2 extends BaseTest {
    @Test(priority = 1)
    public void test4() {
        System.out.println("printing test_4");
    }

    @Test (priority = 3)
    public void test5() {
        System.out.println("printing test_5");
    }
}

Output:

printing Before Class Method
printing test_2
printing test_3
printing Before Class Method
printing test_4
printing test_5
===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
Omeniq
  • 178
  • 11