Integrate µTest (micro test) for Scala into a Gradle project

237 Views Asked by At

I'm using Gradle for my Scala projects and a bit frustrated about documentation of scalatest. So I searched for a alternative test framework. The only one I found was µTest (micro test). But so far I could not found a way to integrate µTest into Gradle.

1

There are 1 best solutions below

0
On

After time of investigation I found a solution. If I have a sample test:

import utest._

object SampleTests extends TestSuite {
  val tests:Tests = Tests {
    var x = 0
    'outer1 - {
      x += 1
      'inner1 - {
        x += 2
        assert(x == 3) // 0 + 1 + 2
        x
      }
      'inner2 - {
        x += 3
        assert(x == 4) // 0 + 1 + 3
        x
      }
    }
    'outer2 - {
      x += 4
      'inner3 - {
        x += 5
        assert(x == 9) // 0 + 4 + 5
        x
      }
    }
  }

  def main(args: Array[String]): Unit = {
    val results = TestRunner.runAndPrint(SampleTests.tests, "SampleTests")
  }
}

Importent is that there is a main funtion which calls a method of the TestRunner with a parameter of type Tests. This parameter can be a value or a method defined with def.

Furthermore the test code should be inside test source location (test instead of main).

For triggering this code you need to modify the build.gradle file. There you can insert a user defined task like this:

task microTest(type: JavaExec) {
    main = 'package.SampleTests'
    classpath = sourceSets.test.runtimeClasspath
}

Of course you need to declare the dependency inside build.gradle to the test framework:

testImplementation "com.lihaoyi:utest_2.a:x.y.z"

Fazit:

There is a way to trigger the tests with

./gradlew microTest

or click with the mouse inside your IDE at the listed gradle task. The output of the test framework micro test will be printed to the console in the expected way. But when you call this task indirectly by defining the following line inside build.gradle:

test.finalizedBy microTest

with click at the test task in the IDE (Intellij), then the colored output is replaced by special characters.

When not clicking (entering the command line: ./gradlew test) all output is printed correctly.