TestNG is a testing framework mainly used for unit testing of the Java projects. The Testng has pre-defined annotations and attributes associated with the annotations would help to verify the application accordingly as per the expected result. The following attributes are applied for @Test attribute
- timeOut
- expectedExceptions
- invocationCount
timeOut Attribute:
The maximum number of milliseconds this test should take. If the execution time of the test case takes more than the timeOut value provided, the test case will Fail.
timeOut Attribute Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class TimeOutAttributeExample{ @Test(timeOut=25000) public void verifyTitle() { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); String actual = driver.getTitle(); String expected="Google"; Assert.assertEquals(actual, expected,"Verifying the Title of Google Page"); } } |
expectedExceptions Attribute:
The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
expectedExceptions Attribute Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.testng.annotations.Test; public class ExpectedExceptionsAttributeExample{ @Test(expectedExceptions=NullPointerException.class) public void calculation() { int a =1; int b =0 ; int c =a/b; } } |
invocationCount Attribute:
The number of times this method should be invoked.
invocationCount Attribute Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class InvocationCountAttributeExample{ @Test(invocationCount =2) public void verifyTitle() { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); String actual = driver.getTitle(); String expected="Google"; Assert.assertEquals(actual, expected,"Verifying the Title of Google Page"); } } |