Sunday 15 March 2020

TestNG: successPercentage: Specify success percentage

Using ‘successPercentage’ property, you can specify the percentage of success expected from this method. Default is 100%.

Example
@Test(invocationCount = 5, successPercentage = 50)
public void a() {

}

Find the below working application.

MyTestListener.java
package com.sample.app.tests;

import org.testng.ITestListener;
import org.testng.ITestResult;

public class MyTestListener implements ITestListener {
 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
  System.out.println("The name of the testcase failed but within success percentage :" + result.getName());
 }
}

SuccessPercentageDemo.java
package com.sample.app.tests;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MyTestListener.class)
public class SuccessPercentageDemo {
 int count = 0;

 @Test(invocationCount = 5, successPercentage = 50)
 public void a() {
  count++;
  System.out.println("Invoked count : " + count);

  if (count % 2 == 0) {
   assertTrue(false);
  } else {
   assertTrue(true);
  }
 }
}

Run SuccessPercentageDemo class, you will see below messages in console.
[RemoteTestNG] detected TestNG version 7.0.0
Invoked count : 1
Invoked count : 2
The name of the testcase failed but within success percentage :a
Invoked count : 3
Invoked count : 4
The name of the testcase failed but within success percentage :a
Invoked count : 5
PASSED: a
PASSED: a
PASSED: a

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 5, Passes: 3, Failures: 2, Skips: 0
===============================================



Previous                                                    Next                                                    Home

1 comment:

  1. Now, I got the use of onTestFailedButWithinSuccessPercentage(). Here is little more details: Well, actually this method is not of so much use. If we want to use it properly, then we need to have two parameters in Test method, which are successPercentage & invocationCount: @Test(successPercentage= 50, invocationCount=3). So when executing this method, it will be executed 3 times (even if we are running once) due to invocationCount. Now, out of three runs, if one run is failed and two runs are passed, then, onTestFailedButWithinSuccessPercentage() will be executed only certain times which satisfies the passing percentage. e.g.: we have defined this listener for following method: @Test(successPercentage= 30, invocationCount=3) public void myTest4(){ Assert.assertTrue(false); }// Now, the success percentage is 30 and invocation count is 3. So, if the method gets failed in each run, then, each failure will result in 33% success rate. So now, in first run it is having success rate below expected. So in this case it will not execute this listener for all three times. Now, if we increase the listener's success percent to 35 then, it will execute the listener only two times. And if we increase the success percentage to 67, then, listener will be executed only once and up to 99% it will be executed only once. If keep the success % above 99, then this listener won't be executed once also. Note: If onTestFailedButWithinSuccessPercentage doesn't gets executed, then, it executes onTestFailure listener.

    ReplyDelete