Skip to content

Testing

Write unit tests

Test utilities are available to make it easier to test your script. Start by adding the cereal-test-utils dependency to your build.gradle file if it's not there already:

testImplementation("com.cereal-automation:cereal-test-utils:<latest-version>")

There is a TestScriptRunner available which accepts an instance of your script:

val script = MyScript()
val scriptRunner = TestScriptRunner(script)

To run your script using the TestScriptRunner, call the run method. This method accepts the script's configuration and a ComponentProviderFactory, which is used by the runner to create a ComponentProvider. This way, both the configuration and the component provider can be mocked to test the behavior of your app under different circumstances. A full example:

class TestSampleScript {

    @Test
    fun testSuccess() = runBlocking {
        // Initialize script and the test script runner.
        val script = MyScript()
        val scriptRunner = TestScriptRunner(script)

        // Mock the LicenseChecker
        mockkConstructor(LicenseChecker::class)
        coEvery { anyConstructed<LicenseChecker>().checkAccess() } returns LicenseState.Licensed

        // Mock the configuration values
        val configuration = mockk<SampleConfiguration>(relaxed = true) {
            every { myConfigurationProperty() } returns "Some random string"
        }
        val componentProviderFactory = TestComponentProviderFactory()

        try {
            // Run the script with a 10s timeout. This is needed because most scripts don't end within a reasonable time.
            // If your script is expected to end automatically please remove the surrounding try catch block.
            withTimeout(10000) { scriptRunner.run(configuration, componentProviderFactory) }
        } catch(e: Exception) {
            // Ignore timeouts because they're expected.
        }
    }

}

Test in Cereal application

Using unit tests, you're probably able to test a large portion of your script. Nonetheless, it's recommended to test the behavior of your script in a real environment, such as the Cereal application, to also verify if your script is correctly obfuscated by ProGuard.

To do this, create a new script in the Cereal Developer Console and upload your script as a draft. Afterwards, open the Cereal desktop application and make sure to log in with an account that you used to upload the script, or any account that belongs to the same team under which the script is uploaded. Go to Settings and enable "Show development scripts" to also download scripts in development. The script should now be visible in the list of scripts. If an update is released, it will automatically be updated in the Cereal application after a restart or a manual sync.