I have an SBT project that has a full build definition with a custom config used for testing.
I add a plugin that comes with its own configuration, and I want to be able to execute a task using both configurations if possible.
If not possible, I would like to pull in the configuration from the plugin into my custom config.
The specifics
My custom config is unit
which is used for classifying certain tests as unit tests, and I would like to add in coverage using sbt-scoverage.
My full build definition includes:
lazy val UnitTest = config("unit") extend(Test)
def unitTestFilter(name: String) = name endsWith "UnitSpec"
lazy val myProject = Project(id="MyProject")
.configs( UnitTest )
.settings( inConfig(UnitTest)(Defaults.testTasks) : _*)
.settings(
testOptions in UnitTest := Seq(Tests.Filter(unitTestFilter)),
parallelExecution in UnitTest := true
)
In order to execute a unit test, I run sbt unit:test
.
How would I A) add in sbt-scoverage
to the unit
configuration in the build definition, or B) include both scoverage
and unit
configurations in an SBT run, e.g. sbt "unit,scoverage:test"
?
edit Based on the 2 answers provided, I am getting close to what I want. The problem I have now is with test filters, and applying it to the config. I have updated my original code snippet above to include the filter I used based on filename - see testOptions
in the project section and how i used unitTestFilter
. In order to add coverage, I have added new configs, for Coverage tests, so now have UnitTest
, ComponentTest
, CoverageUnitTest
and CoverageComponentTest
. The issue I have now is that I cannot get the test options / filename filters to propagate through to the scoverage config.
Method 1: creating a config that extends scoverageTest
lazy val CoverageUnitTest = config("coverUnit") extend (scoverageTest)
def unitTestFilter(name: String) = name endsWith "UnitSpec"
lazy val myProject = Project(id="MyProject")
.configs( UnitTest, CoverageUnitTest )
.settings(
testOptions in CoverageUnitTest := Seq(Tests.Filter(unitTestFilter)),
parallelExecution in CoverageUnitTest := true
)
In the above method, the settings are not applied. If I run coverUnit:test
, it executes both component and unit specs. I have also tried using testOptions in scoverageTest
, which works with the filter, but the issue is that I have 2 configs in my project, namely CoverageUnitTest and CoverageComponentTest, and if I use in scoverageTest
, then only 1 filter is applied to both tasks (i.e. coverUnit:test and coverComponent:test both execute unit tests, or component tests, based on order).
Method 2: extending test and adding in the instrumentation
lazy val CoverageUnitTest = config("coverUnit") extend (Test)
def unitTestFilter(name: String) = name endsWith "UnitSpec"
lazy val myProject = Project(id="MyProject")
.configs( UnitTest, CoverageUnitTest )
.settings( inConfig(CoverageUnitTest)(ScoverageSbtPlugin.instrumentSettings) : _* )
.settings(
testOptions in CoverageUnitTest := Seq(Tests.Filter(unitTestFilter)),
parallelExecution in CoverageUnitTest := true
)
Again, this method doesn't filter the tests with unitTestFilter.
From looking at the ScoverageSbtPlugin source, it looks like I might need to somehow look at overriding this: inConfig(scoverageTest)(Defaults.testSettings) ++
, is this the right approach?
edit #2 Here's a work-around, using Method 1, with "CoverageUnitTest" replaced with "scoverageTest":
lazy val dynamicTestFilter(name:String):Boolean = name endsWith scala.util.Properties.envOrElse("TEST_TYPE","UnitSpec")
run with TEST_TYPE=ComponentSpec sbt coverComponent:test