testableapple
testableapple

Reputation: 325

SoftAssert is pulling results from other tests

When I do .assertAll(), SoftAssert pulls results from other tests.

example:

public SoftAssert softAssert = new SoftAssert();

@Test(priority = 1)
public void newTest1() {
    softAssert.assertTrue(false, "test1");
    softAssert.assertAll();
}

@Test(priority = 2)
public void newTest2() {
    softAssert.assertTrue(false, "test2");
    softAssert.assertAll();
}

newTest1 test result:

java.lang.AssertionError: The following asserts failed:
test1 expected [true] but found [false]`

newTest2 test result:

java.lang.AssertionError: The following asserts failed:
test1 expected [true] but found [false],
test2 expected [true] but found [false]`

Any ideas?

testng.version   == 6.9.10
selenium.version == 2.53.1
java.version     == 1.8.0_65-b17

Upvotes: 1

Views: 691

Answers (1)

Aaron Davis
Aaron Davis

Reputation: 1731

Unlike JUnit, TestNG does not create new instances of the test class. So, if you save the SoftAssert directly on the test class both test methods will be using the same SoftAssert instance. Either instantiate the SoftAsset inside the test methods or initialize it with configuration methods like:

public SoftAssert softAssert;

@BeforeMethod
public void setup() {
    softAssert = new SoftAssert();
}

@AfterMethod
public void tearDown() {
    softAssert = null;
}

// ...

Be careful of that though. In general with TestNG I find it best to not save any state on the test class object as there will be contention if you ever want to run methods in parallel. In that case either look into TestNG parameter injection or be more verbose:

@Test
public void test1() {
    SoftAssert softAssert = new SoftAssert();
    // ...
}

@Test
public void test2() {
    SoftAssert softAssert = new SoftAssert();
    // ...
}

Upvotes: 5

Related Questions