TestNG does not continue execute tests after failure

ぐ巨炮叔叔 提交于 2019-12-23 02:32:32

问题


I have test automation framework with a page object model. All my test are located in separate classes in same package.

In testng.xml i have

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test">
    <test name="SmokeTest">
        <classes>
            <class name="name.test1"/>
            <class name="name.test2"/>
            <class name="name.test3"/>
        </classes>
    </test>
</suite>

Problem is that after running TestNG.xml if the 1st test will fail, it will stop test execution. But i want to continue executing of all test cases.

I use Jenkins on my project and if one of the tests are failed it stops execution immediately.

Example of test

public class LoginTestTest {
    public AndroidDriver<AndroidElement> driver;
    public AOWebClient aoWebClient;

    AOWebClient aoWeb;
    public LoginTestTest(AndroidDriver<AndroidElement> driver, AOWebClient aoWeb){
        this.driver = driver;
        this.aoWeb = aoWeb;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    public LoginTestTest() {
    }

    SoftAssert softAssert = new SoftAssert();

    @BeforeClass
    public void setUp() throws Exception {

        driver = DesiredCapabilitiesSetup.startAppiumServer();
        aoWebClient = DesiredCapabilitiesSetup.getAOWeb();

        LogIn logIn = new LogIn(driver,aoWebClient);
        logIn.logIn();
    }

    @org.testng.annotations.Test
    public void goToSettings() throws InterruptedException {
        HeaderMenu header = new HeaderMenu(driver,aoWeb);
        HamburgerMenuList ham = new HamburgerMenuList(driver);

        header.clickHamburgerButton();
        header.clickHamburgerButton();

        header.editButtonClick();


        softAssert.assertAll();
    }   

    @AfterClass
    public void tearDown(ITestResult result) throws Exception {
            if (result.getStatus() == ITestResult.FAILURE) {
                TakeScreenshot screenshot = new TakeScreenshot();
                screenshot.TakeScreenshot("screenshots/");
            }
        LogOut logOut = new LogOut(driver,aoWeb);
        logOut.logOut();
    }
}

If my test will fail in @Test it will never continue to @AfterClass method. And I want that if @Test fail it will continue to @AfterClass method and After This Class continue executes test from other classes from testng.xml.


回答1:


Your suite tag in your xml should include configfailurepolicy="continue". This tells testng that even though a config failed in one class, you still want to run other classes in that suite. See "configfailurepolicy" in the documentation.

So your xml would become:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Smoke Test" configfailurepolicy="continue">
    <test name="SmokeTest">
        <classes>
            <class name="name.test1"/>
            <class name="name.test2"/>
            <class name="name.test3"/>
        </classes>
    </test>
</suite>



回答2:


From the documentation:

alwaysRun: For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

Then, just replace your @AfterClass by @AfterClass(alwaysRun = true).



来源:https://stackoverflow.com/questions/38900090/testng-does-not-continue-execute-tests-after-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!