Jackranda
Jackranda

Reputation: 57

Selenium - Testng : SoftAssert to be called inside each method or inside @afterTest

I am new to Selenium, using testNG framework. Below is the code I am trying to understand

@Test
public void softAssertionTest() {
    try{
        softAssert.assertTrue(name.equals("HardAssertionTest"));
        softAssert.assertEquals(1, 1,"Error 1");
        softAssert.assertEquals("a","b","Error ab");
        System.out.println("Line after assertions");
    } catch (Throwable t){
        System.out.println("+++++++++++");
        verificationErrors.append(t);
        System.out.println("Verification error - method1"+ verificationErrors);
    }

}

@Test
public void softAssertionTest2() {
    softAssert.assertTrue(name.equals("HardAssertionTest"));
    softAssert.assertEquals(3, 4,"Error 3,4");
    softAssert.assertEquals("c","d","Error cd");     
    System.out.println("Line after assertions method 2");
}

@AfterTest
public void afterTest(){
    softAssert.assertAll();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
        System.out.println(verificationErrorString);
    }
}

Q1) I read it is not a good practice to do assertions in configuration methods. Rather do them in each test method. Hence I modify the code as below

@Test
public void softAssertionTest() {
    try{
        softAssert.assertTrue(name.equals("HardAssertionTest"));
        softAssert.assertEquals(1, 1,"Error 1");
        softAssert.assertEquals("a","b","Error ab");
        System.out.println("Line after assertions");
        doSoftAssert();
    } catch (Throwable t){
        System.out.println("+++++++++++");
        verificationErrors.append(t);
        System.out.println("Verification error - method1"+ verificationErrors);
    }
}

@Test
public void softAssertionTest2() {
    try{
        softAssert.assertTrue(name.equals("HardAssertionTest"));
        softAssert.assertEquals(3, 4,"Error 3,4");
        softAssert.assertEquals("c","d","Error cd");     
        System.out.println("Line after assertions method 2");
        doSoftAssert();
    } catch (Throwable t){
        System.out.println("+++++++++++");
        verificationErrors.append(t);
        System.out.println("Verification error - method2"+ verificationErrors);
    }           
}

public void doSoftAssert(){
    softAssert.assertAll();
    /*String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
    fail(verificationErrorString);
    System.out.println(verificationErrorString);
    }*/
}

When I run this, I see calling softAssert.assertAll() from softAssertionTest2() executes the assertions in softAssertionTest1() as well, duplicating the asserts. How can I have assertions being asserted only once?

Q2) Is appending the errors to verificationErrorString the right way or is it advisable to add these errors in a HashMap?

Upvotes: 0

Views: 737

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

How can I have assertions being asserted only once?

Every test should instantiate its own SoftAssert object and it should not be shared across multiple @Test methods.

Is appending the errors to verificationErrorString the right way or is it advisable to add these errors in a HashMap?

You should never wrap your assertions with a try..catch block because it can cause you to gobble exceptions and it adds a lot of boiler plate code to your tests. If your intent is basically to capture the verification messages, then you should be looking at sub-classing org.testng.asserts.SoftAssert where you:

  • Override org.testng.asserts.Assertion#onAssertSuccess (if you want to capture passed assertions) and
  • Override org.testng.asserts.Assertion#onAssertFailure(org.testng.asserts.IAssert<?>, java.lang.AssertionError) (if you want to capture failed assertions)

Upvotes: 1

Related Questions