简体   繁体   中英

Next test(s) ignored when afterMethod fails

I want to continue test execution even if the afterMethod fails, is there any solution for that? I tried to use alwaysRun = true , but only before and after methods are executed, the tests are always ignored.

Here is a simple example, where the afterMethod fails and the rest tests are ignored.

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

import static org.testng.Assert.assertTrue;

public class Test1 {

    @BeforeMethod
    public void setup() {
        assertTrue(true);
    }

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

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

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

    @AfterMethod
    public void clearData() {
        assertTrue(false);
    }
}

您可以尝试使用软断言(例如参见https://www.seleniumeasy.com/testng-tutorials/soft-asserts-in-testng-example

You are using hard assertion. If you use softAssertion like below :

softAssertion.assertTrue(false);

In code :

@BeforeMethod
public void setup() {
    assertTrue(true);
}

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

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

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

@AfterMethod
public void clearData() {
    SoftAssert softAssertion= new SoftAssert();
    softAssertion.assertTrue(false);
}

You should get this output :

firstTest
secondTest
thirdTest
PASSED: test2
PASSED: test3
PASSED: test1

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Please see here for Reference

Also, If you use hard assertion in a @Test not in configuration annotation , your test cases would have run if there was any failure.

But you are intentionally failing the testng CONFIGURATION , so when a configuration fails in testng next or other configuration are gonna be skipped, You may be seeing this kind of output.

SKIPPED CONFIGURATION: @BeforeMethod setup
SKIPPED CONFIGURATION: @AfterMethod clearData
SKIPPED CONFIGURATION: @AfterMethod clearData
SKIPPED CONFIGURATION: @BeforeMethod setup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM