Showing posts with label successPercentage. Show all posts
Showing posts with label successPercentage. Show all posts

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