Skip to content

Unit Testing

Platform 6 has embedded the Spock testing framework to provide a feature rich test environment for P6 application developers.

https://spockframework.org/

Tests are written as Specifications using a clear and concise Groovy DSL, so making it a natural choice for integration with Platform 6 scripting.

There are a series of great tutorials to get you started here: https://www.softwaretestinghelp.com/spock-and-groovy/

Note

The Spock framework can also be used for Integration testing

Testing Conventions

  • A Spock test Specification is a Groovy Resource with the name extension of _test

New test specification

  • The name of the Specification class must match the resource name (without the _test extension)

Note

Creating a new test via the UI will automatically create a templated Specification class using the correct class name

  • When using p6sync all _test resources will be created under a test source root within the project filesystem:

    • src/test/groovy

Testing Guidelines

  • Specifications cannot test code embedded in the main script resource as this is not represented as a true Object and cannot be instantiated.
  • Specifications can only test Groovy resource classes
  • Specifications must be able to run independently without the services provided by a running P6 instance; these are UNIT tests Specifications
  • Specifications provide a simple, powerful DSL to mock other Objects. Mocking Objects is the key for writing Specifications that exercise code that interacts with other parts of Platform 6
  • Scripts created and updated via the scripts service UI are autoPackaged by default. i.e., Package declarations are not required. This is fine for small UI only developed projects. However, it is strongly recommended that larger project maintained via p6sync in an IDE should be manually packaged (all scripts requiring a package declaration)

Application Packaging

Test Specifications that become part of an application will be run each time the application is packaged

Any Specification that fails during the packaging process will cause the packaging to fail. This is an important step in enforcing a Test Driven Development principal within the Platform 6 development lifecycle.

Example Specification

import spock.lang.*

class TestSpec extends Specification {

    def "two plus two should equal four"() {
        given:
        int left = 2
        int right = 2

        when:
        int result = left + right

        then:
        result == 4
    }
}

Test Specifications can be run directly in the scripts service UI via the Run tests button.

Users of p6sync can take advantage of the gradle profile when creating a new application development project which will ensure Spock and Groovy are correctly configured allowing Specifications to be run within the IDE:

Test Specification in IDE