# Unit testing in Jalview

In June 2015, the Jalview team adopted TestNG for handling unit tests, and all existing JUnit tests were ported to TestNG.

## Test Groups

TestNG provides the ability to perform sophisticated grouping of tests using method annotations. This enables TestNG to be invoked and asked to include a certain set of groups (or regular expression) while excluding another set. This gives maximum flexibility in how tests are partitioned and doesn't require you to recompile anything if you want to run two different sets of tests back to back.

The following test groups are available within the Jalview test environment:

- Functional - basic unit tests that should always be successfully executed
- Network - depends on network connection to execute successfully
- External - depends on external third party resource to execute
- Interactive - depends on human interaction to execute successfully

## Writing new tests

To write a new TestNG tests in Jalview, create a new java class and create test methods which are properly annotated with appropriate TestNG annotations. Here is a simple example test:

        package jalview.test;

        import org.testng.annotations.*;

        public class SimpleTest {

     	 @BeforeClass(alwaysRun = true)
     	 public void setUp() {
    	   // code that will be invoked when this test is instantiated
    	 }

    	 @Test(groups = { "Functional" })
    	 public void aFunctionalTest() {
    	   System.out.println("Functional test");
    	 }

    	 @Test(groups = { "Network" })
    	 public void aNetworkDependentTest() {
    	    System.out.println("Network dependent test");
    	 }

    	 @Test(groups = { "External" })
    	 public void anExternalDependencyTest() {
    	    System.out.println(“Test with external dependence to a third party resource");
    	 }

    	 @Test(groups = { "Interactive" })
    	 public void anInteractiveTest() {
    	    System.out.println("Human interactive test");
    	 }
         }

## Test Execution

The TestNG tests for Jalview can be executed in any of the following ways:

- From Eclipse IDE: To execute Jalview unit test in eclipse please take the following steps:
  - Ensure that you have TestNG plugin correctly install for eclipse
  - Ensure that your test classes are error free and properly annotated
  - Create a launch configuration "Select the Run / Run... (or Run / Debug...) menu and create a new TestNG configuration"
  - Run the tests by executing the configuration target created above
  A more detailed guide for installing and executing TestNG in eclipse is available at testng.org/doc/eclipse.html
- From Ant: The ant task 'testng' will run Jalview's tests. You should:
  - Ensure that you have ant installed
  - Ensure that your test classes are error free and properly annotated
  - Specify the desired group of tests by passing a comma separated list of one or more groups via -Dtestng-groups=""
  A more detailed guide for executing TestNG from ant is available at http://testng.org/doc/ant.html

## TestNG Tutorial

You can find more up-to-date documentations and tutorials on TestNG from the following recommended links: http://http://testng.org/doc/documentation-main.html http://www.tutorialspoint.com/testng/