@Test

Let's build the test class from the ground up; we will use the Rock Bands test class and data file as an example:

/**
* Rock Bands Test Class (JavaDoc left out)
*
* @author Name
*
*/
public class RockBandsTest {

// setup/teardown methods
@BeforeSuite
protected void suiteSetup(ITestContext context) throws Exception {
}

@AfterSuite
protected void suiteTeardown(ITestContext context) throws Exception {
}

@BeforeTest
protected void testSetup(ITestContext context) throws Exception {
}

@AfterTest
protected void testTeardown(ITestContext context) throws Exception {
}

@BeforeGroups
protected void groupsSetup() throws Exception {
}

@AfterGroups
protected void groupsTeardown() throws Exception {
}

@BeforeClass
protected void testClassSetup() throws Exception {
}

@AfterClass
protected void testClassTeardown() throws Exception {
}

@BeforeMethod
protected void testMethodSetup(ITestResult rslt) throws Exception {
}

@AfterMethod
protected void testMethodTeardown(ITestResult rslt) throws Exception {
}

// testcases

@Test
public void tc001_getBandInfo(String rowID,
String description,
JSONObject testData)
throws Exception {
}
}

So, in the page object classes, we created various Java methods to perform actions on elements in web or mobile pages. Now, when we build the test class, we have to tag the test methods with the @Test annotation. This tells TestNG that this method is a "test" and it should be executed when the user runs the class. Some of the attributes available with the @Test annotation include:

  • alwaysRun
  • dataProvider
  • dataProviderClass
  • dependsOnGroups
  • dependsOnMethods
  • description
  • enabled
  • expectedExceptions
  • groups
  • invocationCount
  • invocationTimeOut
  • priority
  • successPercentage
  • singleThreaded
  • timeOut
  • threadPoolSize

Let's discuss a few of the more common ones in the following example:

@Test(groups={"POSITIVE",
"NEGATIVE",
"BOUNDRY",
"LIMIT",
"SMOKETEST",
"REGRESSION"
},
dataProvider="fetchData_JSON",
dataProviderClass=JSONDataProvider.class,
enabled=true,
alwaysRun=true,
priority=1)
public void
tc001_getBandInfo() {
...
}

When the groups attribute is used, it allows the user to tag specific test cases to be part of an overall group, a subset, or a feature test set.

So, in this example, the user defines which group or groups to run in the TestNG suite XML file, and only that subset of groups will be run. It makes sense to tag only a couple of test methods in each class as SMOKETEST, so as you develop the functional test classes, you build a SmokeTest at the same time and "most" of the test methods would be tagged REGRESSION, except for the LIMIT tests, which would stress out or break the product.

The next two attributes in the example, dataProvider and dataProviderClass, are used to tell TestNG which DataProvider class and method to use to extract data to pass to the test method. The last chapter covered building the JSON DataProvider; this is where the user calls it.

The enabled attribute tells TestNG whether or not the test method should be run; great for disabling tests that aren't working, are blocked by defects, or for debugging purposes.

The dependsOnMethods attribute will tie the test method to other test methods in the class. This is a rather tricky one to use, as it will force all test methods to "skip" if the dependent method fails. It is at times more practical to set up a test class using one of the setup/teardown annotations rather than using this attribute.

The alwaysRun attribute tells TestNG to run the test method regardless of a failure to a method it may depend on.

And finally, the priority attribute tells TestNG which priority order to run the tests in.

The TestNG documentation covers all the attributes in detail; we've discussed a few of the more common ones here.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.216.229