2

In my test automation runs tests on two mobile devices in Parallel with testNG feamework. The @BeforeClass and the @AfterClass methods are in the base (super) class that all other test classes inherit. The BeforeClass method initialized the driver session and the AfterClass terminates the session.

The problem is that the @AfterClass is not executed. Because of that, the driver session is not terminated and the subsequent call to BeforeClass fails becasue of that.This results in execution of only the first test in each class - the rest of them are not.

I am wondering if anyone has come across this...and what the cause/remedy is!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Mobile Tests" parallel="tests" thread-count="2"
    preserve-order="true" configfailurepolicy="continue">
    <parameter name="browserTypes" value="Mobile OS" />

    <listeners>
        <listener class-name="my.listeners.TestListener" />
        <listener class-name="my.listeners.CustomReportListener" />
    </listeners>

    <test verbose="2" name="IPHONE_6SPLUS : Parallel Tests">
        <parameter name="device" value="IPHONE_6SPLUS" />
        <parameter name="deviceOS" value="iOS" />
        <classes>
            <!-- ALL INSTALL TESTS -->
            <class name="my.tests.Install" />
            <class name="my.tests.Class1_tests" />
            <class name="my.tests.Class2_tests" />
            <class name="my.tests.Class3_tests" />
            <class name="my.tests.Class4_tests" />
            <class name="my.tests.Class5_tests" />
        </classes>
    </test>

    <test verbose="2" name="IPHONE_SE : Parallel Tests">
        <parameter name="device" value="IPHONE_SE" />
        <parameter name="deviceOS" value="iOS" />
        <classes>
            <!-- ALL INSTALL TESTS -->
            <class name="my.tests.Install" />
            <class name="my.tests.Class1_tests" />
            <class name="my.tests.Class2_tests" />
            <class name="my.tests.Class3_tests" />
            <class name="my.tests.Class4_tests" />
            <class name="my.tests.Class5_tests" />
        </classes>
    </test>

</suite> <!-- Suite -->
S P
  • 101
  • 2
  • 13

3 Answers3

1

Is your base class public? Following sample works for me.

public abstract class A {

 @AfterClass
 tearDown() {...}

}

class B extends A {  

 @Test
 doTests() {...}

}

You can try @AfterClass (alwaysRun = true) as well

Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29
  • 1
    Since '@BeforeClass' and '@AfterClass' methods both are in same class and '@BeforeClass' is executed, this cannot be the right answer. Instead, the OP should check whether his '@AfterClass' method is public or atleast protected or not. – Shivam Mishra Jul 31 '18 at 12:44
  • Thanks for the pointers. I checked, the annotation being used is '@AfterClass(alwaysRun = true)' - do the spaces around the "=" matter? Secondly, the methods are public for '@AfterClass' and '@BeforeClass' – S P Aug 01 '18 at 13:07
  • Problem still exists... :( – S P Aug 16 '18 at 14:32
0

I was also facing similar issue but the root cause was the same methodname exist under multiple files i.e method name under "Afterclass" was exist under anothe class file due to which method override was happening.Renaming the method name solved my issue

Sadha Nanda
  • 337
  • 4
  • 15
0

This happens if your TestNG annotated methods in the parent class are declared as private.

You need to set the access modifier to either public or protected in the parent class so the inherited test classes can access the Before and After methods to initialize and close the driver session.

public class BaseTest {
        protected AppiumDriver driver;
        protected WebDriverWait wait;

        @BeforeMethod
        protected void setup() {
               driver = DriverManager.initializeDriver();
                wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            }
        
            @AfterMethod(alwaysRun = true)
            protected void tearDown() {
                DriverManager.shutdownDriver();
            }
        
        }

My Test Class:

public class SampleTest extends BaseTest {
    @Test
    public void ABCTest() {
        System.out.println("Perform your actual test");
    }
 }
itkhanz
  • 121
  • 2
  • 6