Creating a framework test

The GUnit Test Framework provides the following base classes to support different types of tests.

PCServerTestClassBase
Base class for tests that require a running server
PCUnitTestClassBase
Base class for tests that do not need the services that by a running server provides

A test class extends one of the base classes. The test class name must end with the suffix Test, such as MySampleTest. The base class implementation of the framework's setUp method tests for this condition and throws an IllegalStateException if this test fails.

Store all test class files in the Studio modules/configuration/gtest directory hierarchy. You can create new subdirectories in the hierarchy to organize test classes for your convenience.

Optionally, the @Suites annotation can be specified on the test class. The @Suites annotation accepts a unique String argument. All the test classes in a particular suite must specify the same @Suites annotation. An associated suite class also references the suite's String value.

@Suites("UniqueSuiteName")
class MyTest extends PCUnitTestClassBase {
  ...
}
The following sample source classes demonstrate the use of the @Suites annotation.
  • The MySuiteNames class defines a string constant called SAMPLE_SUITE_NAME.
    // ========== File: MySuiteNames.gs ==========
    // Define suite name strings.
    // Test and suite classes reference the same string to group the tests into the suite.
    // A test class references the string in a @Suites annotation.
    
    package doc.example
    
    class MySuiteNames {
    
      public static final var SAMPLE_SUITE_NAME : String = "AnyUniqueString_1"
      // ... Define other suite-name strings here
    }
  • The MyTest class definition uses a @Suites annotation that specifies the SAMPLE_SUITE_NAME constant. If other test classes exist in the suite, each class must specify the same @Suites annotation.
    // ========== File: MyTest.gs ==========
    // Implement test class
    
    package doc.example
    
    uses gw.testharness.v3.Suites
    uses gw.api.test.PCUnitTestClassBase
    
    @Suites(MySuiteNames.SAMPLE_SUITE_NAME)
    class MyTest extends PCUnitTestClassBase {
      ...
    }
  • The MySuiteClass class is a suite class that references SAMPLE_SUITE_NAME.
    // ========== File: MySuiteClass.gs ==========
    // Implement suite class
    
    package doc.example
    
    class MySuiteClass {
      // References MySuiteNames.SAMPLE_SUITE_NAME.
      // All test classes with a @Suites annotation that references SAMPLE_SUITE_NAME are included in the suite.
      // Details of implementing the suite class are described in another topic.
    
      static function suite() : Test {
        return new SuiteBuilder(PCUnitTestClassBase)
              .withSuiteName(MySuiteNames.SAMPLE_SUITE_NAME)
              .build()
      }
    }

The @Suites annotation is optional. Similarly, a suite class does not need to reference a unique suite name. A suite class that does not reference a suite name includes all the test classes in the package that do not specify a @Suites annotation. This catch-all style of suite grouping can be useful early in the development cycle when only a few tests exist and logical criteria for grouping them into suites are not yet evident.

Each test class provides the following Gosu constructors.

construct()
construct(name : String)

The name of a framework test method must begin with the string test as in testAddNumbers. When running the individual tests in a test class, the framework executes each method that begins with the string test.

The following code illustrates the minimal skeletal structure of a test class that extends the PCServerTestClassBase class.

class MyTest extends PCServerTestClassBase {

  construct(testName : String) {
    super(testName)
    ...
  }

  function testBasicSystemCheck() {
    ...
  }

  function testAnotherServerTest() {
    ...
  }

  // Other class methods
  ...
}