What is successPercentage and ExpectedExceptions Attribute?

What is successPercentage and ExpectedExceptions Attribute?

successPercentage Attribute-

This attribute can be used when we know the expected passing parcentage required for certain method. Here we have to use 2 attribute togather i.e. Invocationcount and successPercentage. This attribute can be used only with invocation count. If invocation count is not mentioned then it will be void.

In below example, scenario meet the passed Percentage criteria. Hence test case is marked as passed. However in normal case below method will fail.

Here the expected pass percentage is 10% for all 4 iteration

Java Code-

public class Testing_successPercentage {
int count=1;
@Test(invocationCount = 4,successPercentage=10)
public void testNG1()
{
if(count==2) { 
assertTrue(false);		 
}
count++;
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}
    

OutCome-

[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
PASSED: testNG1

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


===============================================
Default suite
Total tests run: 4, Passes: 1, Failures: 3, Skips: 0
===============================================

In below example, scenario doesn't meet the passed Percentage criteria. Hence test case is failed.

Here the expected pass percentage is 50% for all 4 iteration. This script doesn't meed the passing criteria.

Java Code-

public class Testing_successPercentage {
int count=1;
@Test(invocationCount = 4,successPercentage=50)
public void testNG1()
{
if(count==2) { 
assertTrue(false);	 
}
count++;
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}
    

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
PASSED: testNG1
FAILED: testNG1
java.lang.AssertionError: expected [true] but found [false]
.
.

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


===============================================
Default suite
Total tests run: 4, Passes: 1, Failures: 3, Skips: 0
===============================================

Related topics-















ExpectedExceptions Attribute-

This attribute is used when we are expecting certain exception from our method. If we are getting the same excetion mentioned in "expectedExceptionMessageRegExp" attribute then the test method will pass, else it will be failed.

In below example, we have taken 3 methods, which has 2 attribute ie. "expectedExceptions" and "expectedExceptionsMessageRegExp".

In normal case, below all 3 methos will throw exception, but in this case only one method is throwing exception rest 2 will get passed as the message is expected

Here for the first 2 scenarioes, we have mentioned expected message as "/ by zero" and ".* zero" in expectedExceptionsMessageRegExp. Which is expected exception.

Java Code-

public class Testing_exception {
	
@Test(expectedExceptions = { ArithmeticException.class }, expectedExceptionsMessageRegExp = "/ by zero")
public void testNG1() {
		
int i=2/0;	
}
@Test(expectedExceptions = { ArithmeticException.class }, expectedExceptionsMessageRegExp = ".* zero")
public void testNG2() {	
int i=2/0;	
}
@Test(expectedExceptions = { ArithmeticException.class }, expectedExceptionsMessageRegExp = "This test case passed")
public void testNG3() {	
int i=2/0;	
}

}

OutCome-

[RemoteTestNG] detected TestNG version 7.4.0
PASSED: testNG1
PASSED: testNG2
FAILED: testNG3
org.testng.TestException: 
The exception was thrown with the wrong message: expected "This test case passed" but got "/ by zero"
.
.
===============================================
    Default test
    Tests run: 3, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
===============================================
Related topics-

No comments:

Post a Comment