question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestNG softAssert in @AfterMethod makes all tests skip

See original GitHub issue

I use a SoftAssert that I throw after my test method, and I use a dataProvider in my test methods running parallel through maven failsafe, it’s running parallel per method. It doesn’t matter if I use parallel=true on my dataprovider or not. Whenever an assert is thrown using my aftermethod, all tests that start in that thread fail immediately because of the dataProvider.

DataProvider:

@DataProvider(name = "environment", parallel = true)
public Object[][] getEnvironments() { return propertiesHelper.getBrowsers() ; }

The dataprovider returns a list of enums, and not null. TestNG also sees no errors in the dataprovider (confirmed through debugging)

Aftermethod:

@AfterMethod(alwaysRun = true)
public void throwSoftAssert() {

    if(SoftAssertThreadLocal.get() == softAssert && softAssert != null) {
            softAssert.assertAll();
    }
}

This is what I see in the log, as can be seen from when my assert fails every test in the thread [pool-2-thread-3] is started and stopped within 1ms, it is then shown as skipped in the test report.

13:23:58.318 [pool-2-thread-3] FATAL nl.adaption.it.selenium_test.util.assertion.SoftAssert - Is seaFreight revenue created? copying D:\Jenkins\workspace\Selenium\workspace\selenium test runner\target\screenshot_Is_seaFreight_revenue_created__1462361038604.png 13:23:58.612 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.dossier.financial.revenue.ITgenerateVesselRevenue - ### STOPPING TEST: whenGeneratingVesselRevenue_RevenuesShouldBeCreated ### 13:23:59.565 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.contract.ITtransportContract - ### STARTING TEST: whenCreatingValidTransportContractWithAgreementAndScenario_ActivitiesShouldBeAutomaticallyCreated ### 13:23:59.566 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.contract.ITtransportContract - ### STOPPING TEST: whenCreatingValidTransportContractWithAgreementAndScenario_ActivitiesShouldBeAutomaticallyCreated ### 13:23:59.569 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.relation.ITuploadRelationLogo - ### STARTING TEST: whenUploadingNewRelationLogo_SpinnerShouldAutomaticallyClose ### 13:23:59.569 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.relation.ITuploadRelationLogo - ### STOPPING TEST: whenUploadingNewRelationLogo_SpinnerShouldAutomaticallyClose ### 13:23:59.571 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.app_currency.ITcreateAppCurrency - ### STARTING TEST: whenCreatingAppCurrencyWithValidData_AndClickingSaveAndClose_AppCurrenciesPageShouldBecomeVisible ### 13:23:59.571 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.app_currency.ITcreateAppCurrency - ### STOPPING TEST: whenCreatingAppCurrencyWithValidData_AndClickingSaveAndClose_AppCurrenciesPageShouldBecomeVisible ### 13:23:59.573 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.app_currency.ITeditAppCurrency - ### STARTING TEST: whenEditingAppCurrencyWithValidData_AndClickingSaveAndClose_AppCurrenciesPageShouldBecomeVisible ### 13:23:59.573 [pool-2-thread-3] INFO nl.adaption.it.selenium_test.tests.app_currency.ITeditAppCurrency - ### STOPPING TEST: whenEditingAppCurrencyWithValidData_AndClickingSaveAndClose_AppCurrenciesPageShouldBecomeVisible ###

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:25 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anandbagmarcommented, Nov 6, 2018

@hubertgrzeskowiak - that info helps. I solved the problem the following way:

I made my BaseTest implement IHookable interface. And then implemented the methods as below:

    private static final String SOFT_ASSERT = "softAssert";

    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        SoftAssert softAssert = new SoftAssert();
        testResult.setAttribute(SOFT_ASSERT, softAssert);
        callBack.runTestMethod(testResult);
        softAssert = getSoftAssert(testResult);
        if (!testResult.isSuccess()) {
            Throwable throwable = testResult.getThrowable();
            if (null != throwable) {
                if (null != throwable.getCause()) {
                    throwable = throwable.getCause();
                }
                softAssert.assertNull(throwable, ExceptionUtils.getStackTrace(throwable));
            }
        }
        softAssert.assertAll();
    }

    public static SoftAssert getSoftAssert() {
        return getSoftAssert(Reporter.getCurrentTestResult());
    }

    private static SoftAssert getSoftAssert(ITestResult result) {
        Object object = result.getAttribute(SOFT_ASSERT);
        if (object instanceof SoftAssert) {
            return (SoftAssert) object;
        }
        throw new IllegalStateException("Could not find a soft assertion object");
    }

This solution helps to fail the test with all soft asserts + any hard assert as well.

1reaction
hubertgrzeskowiakcommented, Jul 19, 2018

@anandbagmar apologies for not uploading the whole code. My WebDriverTestBase testinstance is having a private list of verification errors. Whenever a verification (a.k.a. soft assert) fails, I add an entry to that list. clearVerificationErrors() is simply clearing the list. assertNoVerificationErrors() is basically assert this.verification_errors.isEmpty() with some fancy printing of the list’s contents.

I can’t give you the original code, because I switched companies since.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Checking Soft Assertions in @AfterMethod - Stack Overflow
I want to use the @AfterMethod so I don't have to worry about false positives if I forget to put sa.assertAll() at the...
Read more >
automatically verify soft assertions for all tests - Google Groups
I found the configfailurepolicy="continue" option to make all tests run even if an @afterMethod 'fails', and it kind of works, however the test...
Read more >
How To Use Soft Assert In TestNG | TestNG Tutorial
Soft Assert. When an assert fails the test script stops execution unless handled in some form. We call general assert as Hard Assert....
Read more >
How to continue tests if any tests fails in between and how to ...
yes when we use in catch softAssert. e.g. try-catch catching error and we report it in softAssert which does not stop test BUT...
Read more >
I have implemented Custom Listener to capture skipped test ...
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found