S P
S P

Reputation: 69

@AfterClass is not executed

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

Upvotes: 2

Views: 3550

Answers (3)

itkhanz
itkhanz

Reputation: 159

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");
    }
 }

Upvotes: 0

Sadha Nanda
Sadha Nanda

Reputation: 357

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

Upvotes: 0

Rakesh Pullayikodi
Rakesh Pullayikodi

Reputation: 111

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

Upvotes: 1

Related Questions