0
 @Test(priority = 0)
public void verify_Templete_BG(){
    logger =report.startTest("Verify TempleteBG");
    String expectedBG = "White";
    for(int pageNo = 1; pageNo<=3 ; pageNo++){
        Assert.assertTrue(expectedBG.equals("White"));
    }
    System.out.println("TC1 Pass");
    logger.log(LogStatus.PASS, "TC1 Pass");
}

In this above sample program, I have some doubts.

  1. If loop 2 fails(loop 1 and loop 3 pass) what will be output. Whether this testcase pass or fail.
  2. If loop 3 fails(loop 1 and loop 2 pass) what will be output. Whether this testcase pass or fail.
  3. Or else, how to know which loop got fails.
Anandh R
  • 109
  • 1
  • 1
  • 9
  • Case 1: If it fails in loop 2, the test case fails and stops immediately. It wont even run the 3rd loop. Same with case 2. For case 3: For assert.assertTrue(), I think you can have another parameter, give some message there. For example: `Assert.assertTrue(your condition, "Failed in loop - " + pageno);`. Please check the syntax, I use C# so have no idea about the syntax in Java – Sudeepthi Oct 26 '16 at 13:03

2 Answers2

0

If any assert fails, the test case will fail immediately.

You can add a little code and test out the different outcomes for yourself. Adjust the actualBG values for the different loops to be whatever you want. The code below is set up for Case 1, to fail on loop 2.

@Test(priority = 0)
public void verify_Templete_BG()
{
    logger = report.startTest("Verify TempleteBG");
    String expectedBG = "White";
    String actualBG = "";
    for (int pageNo = 1; pageNo <= 3; pageNo++)
    {
        switch (pageNo)
        {
            case 1:
                actualBG = "White";
                break;
            case 2:
                actualBG = "Black";
                break;
            case 3:
                actualBG = "White";
                break;
            default:
                break;
        }
        Assert.assertTrue(expectedBG.equals(actualBG));
    }
    System.out.println("TC1 Pass");
    logger.log(LogStatus.PASS, "TC1 Pass");
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

Assuming yours is an Extent Reports. Extent reports actually prints step wise results for each Test cases. So you could actually modify it to like the below.

@Test(priority = 0)
public void verify_Templete_BG(){
    logger =report.startTest("Verify TempleteBG");
    String expectedBG = "White";
    for(int pageNo = 1; pageNo<=3 ; pageNo++){
       if(Assert.assertTrue(expectedBG.equals("White")))
          logger.log(LogStatus.PASS, pageNo +" Loop Passed");
       else
          logger.log(LogStatus.Fail, pageNo +" Loop Failed");
     }  
     report.endTest(logger);
     report.flush();
 }

assertTrue will return true or false based on the condition you are passing in. When it is true, you will print PageNo - Loop is passed. Else you will print it as failed.

ARN
  • 175
  • 1
  • 1
  • 13