This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

TestNg Custom Annotations

The TestNG custom annotations allow you to add a custom defined annotation in to your test class or method and control that test class or method based on the custom annotation defined. This page explains how we use TestNG custom annotations to define the environment in which a given test run should execute, and defines which test cases need to be excluded based on the environment of the test run .

Considering the WSO2 platform, we are subjected to execute tests mainly on three execution environments with two types of uses. Following is a summary of the execution patterns based on the execution environment:

  • Platform - Tests which are designed to cover platform wide use cases. Typically, this kind of test involves more than one product and you cannot run the same test with a standalone product.

  • Standalone - Tests which will operate only with a single WSO2 product. You cannot run the same test on a product platform.

  • All - Tests that can be executed on both standalone products and product platforms. No restriction on the environment.

Considering the above patterns, the following environments have been identified to control the test case executions. The section below describes how to use these custom annotations.

  • Platform - Runs only on a product platform.

  • Standalone - Runs only with a standalone product.

  • All - Runs on both standalone and product execution modes. This is the default option.

Execution control with custom annotations

The main purpose of maintaining annotations is to reduce the complexity of the test execution.

When it comes to platform wide testing, it is a requirement to have a set of tests which run only on a specific environment. The main motivation behind introducing such a constraint is to have control over seamless test execution support provided by the test framework.  To achieve this, WSO2 Test Automation Framework introduces a custom annotation based test execution facility.

The test execution environment can be configured by editing the automation.xml file. The available environment options are standalone and platform. You can edit the following execution environment property under the configurations element.

<executionEnvironment>standalone</executionEnvironment>

To Implement this, you have to add the correct annotation based on the execution environments mentioned above.  The following example assumes you want to execute an integration test only on standalone mode:

 @SetEnvironment(executionEnvironments = {ExecutionEnvironment.STANDALONE})
public void testCreateGroup() throws Exception{     
}

To run the tests on all environments, you can define the test method without any custom annotations or use the SetEnvironment annotation and set the environment to ALL.

public void testCreateGroup() throws Exception {     
}
 @SetEnvironment(executionEnvironments = {ExecutionEnvironment.ALL})
public void testCreateGroup() throws Exception{     
}

To run tests only on a platform environment, you can define the test method with the SetEnvironment annotation and set the environment as platform.

@SetEnvironment(executionEnvironments = {ExecutionEnvironment.PLATFORM})
public void testCreateGroup() throws Exception{    
}