Skip to main content

Scala Test Cases Generation

Since: 2.74.0

This document explains how to generate test cases in scala from saved morphir tests.

Morphir offers a way to generate scala test cases from test cases created using the morphir develop UI.

Test cases can be generated by adding additional flags to the gen command, i.e morphir scala-gen [test-gen-flag]. The gen flags for tests can be mixed together to generate different kinds of test. Generating tests creates a sub-package called _morphirtests, at the package root of the generated scala, containing scala files matching the selected kind of test(s).

Here's what the packages look like if your Morphir project is named **foo**

- foo/  /* package root */  
- (...) /* other sub packages and files */
- _morphirtests/
- [TestKind].scala /* eg: GenericTest.Scala */

Flags for Tests Generation​

This tooling also provides some flags to generate a runnable test suite using scala test or just generate generic test cases.

Using --generate-test-generic​

Usage:
morphir scala-gen --generate-test-generic

This generates a scala value with a TestCase case class. Example:

/*
testInput =
{
input = "Foo",
expectedOutput = "Foo",
description = "foo description"
}
*/

object GenericTest {
case class TestCase(input: Any, expectedOutput: Any, description: String)

val testCases : List[TestCase] = List(
TestCase(
input = "Foo",
expectedOutput = "Foo",
description = "testInput test1 - foo description"
)
)
}

Using the generated test cases​

You will need to manually write out the tests in scala and import the generated testcases

example:

test("morphir tests should not fail") {
val suite = GenericTest.testCases

suite.foreach((testcase) => {
assertResult(testcase.expectedOutput)(testcase.input)
})
}

Using --generate-test-scalatest​

Usage:
morphir scala-gen --generate-test --test-strategy=ScalaTest

This generates a complete runnable test suite that just can be run. Using this also introduces dependency on org.scalatest::scalatest-funsuite that you can manually add as a project dependency. To create a test description, it uses

  • the fully qualified function name
  • a sequential integer
  • and the original description provided.

example:

/*
testInput =
{
input = "Foo",
expectedOutput = "Foo",
description = "foo description"
}
*/

class ScalaTest extends AnyFunSuite {
test("testInput test1 - foo description") {
assertResult("Foo")("Foo")
}
}