I usually use sbt testOnly "Unit. *", But this works in the context of the project. I canโt find the documentation showing how to do this when there is already a bank.
The test -family tasks in SBT (with testOnly as an example) work with the compile task, which returns a list of compiled files through an sbt.inc.Analysis instance. I could not figure out how to change it and insert the modified Analysis instance into testOnly so that it knew that the test that I would run was there.
I propose another solution - a trick .
Put the test classes in the jar with the test:package task as follows:
[test-lib]> test:package [info] Updating {file:/Users/jacek/sandbox/so/test-lib/}test-lib... [info] Resolving org.fusesource.jansi
When you have a test jar, you can run the test environment on the command line without SBT (suppose you use ScalaTest considering scalatest , but I will use Specs2). Read the test environment documentation on how to do this, and for Specs2 it specs2.run , as described in Console exit .
Performing tests from a test jar requires determining the correct CLASSPATH, which may or may not be easy to correct. That SBT can be of great help is to manage dependencies and therefore CLASSPATH.
Create another SBT project and save the test jar in the lib subdirectory so that it is on CLASSPATH (as described in Unmanaged dependencies ).
Dependencies in lib extend to all class paths (for compilation, testing, launching, and console).
Add a sample build.sbt where you define your test structure as a project dependency. For Specs2, this is as follows (I used the default configuration described in the Specs2 homepage ):
libraryDependencies += "org.specs2" %% "specs2" % "2.3.8" % "test" scalacOptions in Test ++= Seq("-Yrangepos")
The trick is to execute the main class of the test environment, for example. specs2.run for Specs2, as if the class was running on the command line. SBT helps with test:runMain .
[my-another-project]> test:runMain specs2.run HelloWorldSpec [info] Running specs2.run HelloWorldSpec HelloWorldSpec The 'Hello world' string should + contain 11 characters + start with 'Hello' + end with 'world' Total for specification HelloWorldSpec Finished in 62 ms 3 examples, 0 failure, 0 error Exception: sbt.TrapExitSecurityException thrown from the UncaughtExceptionHandler in thread "run-main-0" [success] Total time: 5 s, completed Mar 4, 2014 11:15:14 PM
Donโt worry about this Exception , as it comes from SBT, which catches exit from Specs2 (after running the test), therefore SBT does not work.