Skip to content

Automation with JUnit

Test reference in Squash TM

In order to bind a Squash TM test case to a JUnit automated test, the content of the Automated test reference field of the Automation block of a test case must have the following format:

[1] / [2] # [3]

With:

  • [1]: Name of the project on the source code repository.

  • [2]: Qualified name of the test class.

  • [3]: Name of the method to test in the test class. This parameter is optional.

Below is an example of a test class and the corresponding Squash TM test case automation:

junit-1

junit-2

junit-1

junit-2

Nature of the exploitable Squash TM parameters

The exploitable Squash TM parameters in a JUnit script will differ depending on whether you're using the Community or Premium version of Squash DEVOPS.

Here is a table showing the exploitable parameters (these parameters are transmitted as test parameters, see below, Squash TM does not generate global parameters):

Nature Key Community Premium
Name of the dataset DSNAME βœ… βœ…
Dataset parameter DS_[name] βœ… βœ…
Test case reference TC_REFERENCE βœ… βœ…
Test case custom field TC_CUF_[code] βœ… βœ…
Iteration custom field IT_CUF_[code] ❌ βœ…
Campaign custom field CPG_CUF_[code] ❌ βœ…
Test suite custom field TS_CUF_[code] ❌ βœ…

Legend:

  • [code]: Value of the "Code" of a custom field
  • [name]: Parameter name as filled in Squash TM

Parameters usage

It is possible, when running JUnit tests, to exploit parameters within it. A parameter can be a test parameter or a global parameter. Squash TM transmits only test parameters. Test parameters and global parameters can be used in Squash DevOps with the junit/params action.

To do this, the following steps must be followed:

  • Import opentestfactory-java-param-library into the JUnit project containing the tests to run by adding to the pom.xml file:

    • the following Maven repository:
    <repositories>
        <repository>
            <id>org.squashtest.repo.release</id>
            <name>Squashtest repository for releases</name>
            <url>https://repo.squashtest.org/maven2/release</url>
        </repository>
    </repositories>
    
    • the following Maven dependency:
    <dependencies>
        <dependency>
            <groupId>org.opentestfactory.util</groupId>
            <artifactId>opentestfactory-java-param-library</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
    
  • You can then retrieve the value of a parameter of the desired type using the following syntax:

    ParameterService.INSTANCE.getString("paramName");
    ParameterService.INSTANCE.getInt("paramName");
    ParameterService.INSTANCE.getFloat("paramName");
    ParameterService.INSTANCE.getDouble("paramName");
    ParameterService.INSTANCE.getBoolean("paramName");
    

    The above methods look for the desired parameter in the test parameters; if they cannot find it, they then look for it in the global parameters.
    These methods throw a ParameterNotFoundException if the parameter is not found. If the parameter is found but cannot be converted to the desired type, a ParameterFormatException is thrown. Consider handling these exceptions in your test classes.
    Warning: the conversion of Boolean data returns true when the character string to be converted is equal to "true" (whatever the case), false in all other cases; but never propagates an exception.

  • It is also possible to define a default value in the case where the parameter does not exist by using the following syntax:

    ParameterService.INSTANCE.getString("paramName", defaultValue);
    ParameterService.INSTANCE.getInt("paramName", defaultValue);
    ParameterService.INSTANCE.getFloat("paramName", defaultValue);
    ParameterService.INSTANCE.getDouble("paramName", defaultValue);
    ParameterService.INSTANCE.getBoolean("paramName", defaultValue);
    

    The above methods therefore do not propagate a ParameterNotFoundException when the parameter sought is not found but propagate a ParameterFormatException if the parameter is found, but cannot be converted to the desired type.

  • It is also possible to target a test parameter or a global parameter with specific methods. As with the previous methods, they are available in versions with and without default values. Here are a few examples:

    ParameterService.INSTANCE.getTestString("paramName");
    ParameterService.INSTANCE.getGlobalInt("paramName");
    ParameterService.INSTANCE.getTestFloat("paramName", defaultValue);
    ParameterService.INSTANCE.getGlobalBoolean("paramName", defaultValue);
    

Below is an example of a JUnit test file and the automation of the associated Squash TM test case:

junit-params-1

junit-params-2

junit-params-3

junit-params-1

junit-params-2

junit-params-3

Supported versions

Squash AUTOM and Squash DEVOPS have been validated with

  • JUnit 4.12, any JUnit 4 version more recent should work properly
  • JUnit 5.3.2, any JUnit 5 version should work properly

Additionally, it is advised to use Surefire 2.19.1 or later.

Back to top