I would like to add an Integration Test phase to my SBT + Spray app.
Ideally it would be just like Maven, with the following phases:
compile
: The app is builttest
: The unit tests are runpre-integration-test
: The app is launched in a separate processintegration-test
: The integration tests are run; they issue requests to the app running in the background and verify that the correct results are returnedpost-integration-test
: The instance of the app previously launched is shut down
I'm having a lot of trouble getting this to work. Is there a worked example that I can follow?
1) Separate "it" codebase:
I started by adding the code shown in the "Integration Test" section of the SBT docs to a new file at project/Build.scala
.
This allowed me to add some integration tests under "src/it/scala" and to run them with "sbt it:test", but I can't see how to add a pre-integration-test
hook.
The question "Ensure 're-start' task automatically runs before it:test" seems to address how to set up such a hook, but the answer doesn't work for me (see my comment on there).
Also, adding the above code to my build.scala has stopped the "sbt re-start" task from working at all: it tries to run the app in "it" mode, instead of in "default" mode.
2) Integration tests in "test" codebase:
I am using IntelliJ, and the separate "it" codebase has really confused it. It can't compile any of the code in that dir, as it thinks that all the dependencies are missing.
I tried to paste instead the code from "Additional test configurations with shared sources" from the SBT docs, but I get a compile error:
[error] E:\Work\myproject\project\Build.scala:14: not found: value testOptions
[error] testOptions in Test := Seq(Tests.Filter(unitFilter)),
Is there a worked example I can follow?
I'm considering giving up on setting this up via SBT and instead adding a test flag to mark tests as "integration" and writing an external script to handle this.
I have now written my own code to do this. Issues that I encountered:
I found that converting my
build.sbt
to aproject/Build.scala
file fixed most of the compile errors (and made compile errors in general much easier to fix, as IntelliJ could help much more easily).The nicest way I could find for launching the app in a background process was to use
sbt-start-script
and to call that script in a new process.Killing the background process was very difficult on Windows.
The relevant code from my app is posted below, as I think a few people have had this problem. If anyone writes an sbt plugin to do this "properly", I would love to hear of it.
Relevant code from
project/Build.scala
:This helper code is in
project/FunctionTestHelper.scala
: