4

I'm running sbt-assembly in order to build a single jar file that can be deployed elsewhere. I would like to run my tests against this jar file rather than against the local .class files. Running against the local .class files is the default with sbt test but I want to test the jar instead (but without incorporating the test class files into the jar).

3

1 Answer 1

1

To build assembly jar in test you need to configuration

import AssemblyKeys._

Project.inConfig(Test)(baseAssemblySettings)

jarName in (Test, assembly) := s"${name.value}-test-${version.value}.jar"

So now you can prepare uber-jar with test:assembly. However I don't know easy way to run tests from sbt with this jar. I would go for custom command, something like test:run-test-assembly that will do something like this internally

scala -classpath uber-jar-test.jar classpath scalatest-<version>.jar org.scalatest.tools.Runner -p compiled_tests

sbt-assembly is running tests during assembly phase, but I'm pretty sure it's doing it agains not yet packaged classes. Sou you probably want to exclude them from assembly phase with

test in (Test, assembly) := {}
3
  • thanks but including scalatest on the command line seems unnecessary (presumably it gets included automatically when working out the dependencies). And more seriously, when I run scala -classpath uber-jar-test.jar -classpath org.scalatest.tools.Runner -R target/scala-2.10/test-classes/, the scalatest GUI pops up and correctly finds my 25 tests, but fails with a java.lang.NoClassDefFoundError (as the jar is assembled then all the classes ought to be there, right?)
    – TooTone
    Commented Oct 17, 2014 at 16:53
  • 1
    Downvoting because this doesn't answer the question of running tests against an assembly jar; it answers how to build an assembly jar that includes tests. Commented Mar 30, 2017 at 2:22
  • This doesn't answer question how to run tests against jar
    – Abhay
    Commented Oct 4, 2020 at 15:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.