Group unit tests and run only a set - scala

I want to have an additional battery of tests to run against a database in addition to the unit test battery that requires no IO. What's the best way to do this with specs2 and sbt?

One of the solutions would be to put them in a namespace and use sbt test-only command:
$ sbt
> test-only com.example.utils.*
There's also a notion of tags in Specs2, which could be used to include or exclude tests at run time via SBT configuration.

Related

Run only one package test cases scala

I have the package structure as:
src -> test -> scala -> notification
Inside notification i have two packages unit and integration.
unit package has the unit tests and integration package has integration tests. I want to execute only the unit test cases. Is there i way i can do it by sbt test only?
for one class, i know it can be done like this:
I have tried for the one class but do not know how to do it for a pacakge.
sbt "test:testOnly *LoginServiceSpec"
testOnly allows you to run wildcards using *. So if all your tests are in package namespace x you can call
> testOnly x.*
This wildcard can be put everythere, so if you have a subpackage in x, but all your tests end with e.g. Spec you could run
> testOnly x.*Spec
However if you are using integration test I recommend creating a separate configuration for them so that you would run:
> test
for unit tests and
> it:test
for integration test. Then you would put them in src/it/scala. directory.
Try this
sbt "test:testOnly notification.unit.*"

Getting test-only to work outside SBT console

I'm using Specs2 for tests, with latest Play, Scala and SBT.
In sbt console, this works great, running only tests in UserServiceSpec:
[my-project] $ test-only services.UserServiceSpec
Outside sbt console, in project root directory, this does not work:
$ sbt test-only services.UserServiceSpec
This runs all the tests. (Same happens with testOnly.)
How is test-only supposed to work outside sbt console?
Follow-up question: using Specs2 tags, how to execute only tagged tests on the command line, outside sbt console?
$ sbt test-only -- include unit
The above, again, tries to to execute all tests (while test-only -- include unit in sbt console works fine).
Basically, I'd like to run all unit tests on a CI server, and Specs2 tags seem like a good tool for separating different kinds of tests. In this scenario I couldn't use the sbt console, right?
Sbt consider two parameters as two separate commands. You should mark it as one.
Try: sbt "testOnly services.UserServiceSpec"

Run scalatest in main instead of test through sbt?

Say I have a Scalatest file in the main directory, is there a sbt command to run the test such as testOnly or `runMain'? On IntelliJ, you are given the option to run the test.
You should be able to use test-only. From the scalatest user guide:
test-only org.acme.RedSuite org.acme.BlueSuite

How to run a single test in scalatest from maven

I have not found any documentation on how to do this. For JUnit the equivalent would be:
mvn -Dtest=org.apache.spark.streaming.InputStreamSuite test
tl;dr mvn test -Dsuites="some.package.SpecsClass"
I found an answer from here and it works:(https://groups.google.com/forum/#!topic/scalatest-users/Rr0gy61dg-0)
run test 'a pending test' in HelloSuite, and all tests in HelloWordSpec:
mvn test -Dsuites='org.example.
HelloSuite #a pending test, org.example.HelloWordSpec'
run all tests in HelloSuite containing 'hello':
mvn test -Dsuites='org.example.HelloSuite hello'
for more details: http://scalatest.org/user_guide/using_the_scalatest_maven_plugin
Found the answer: it is
-DwildcardSuites
So here is the example command line:
mvn -pl streaming -DwildcardSuites=org.apache.spark.streaming.InputStreamSuite test
Update Newer versions of scalatest use
-Dsuites
So the syntax would be:
mvn -pl streaming -Dsuites=org.apache.spark.streaming.InputStreamSuite test
Note that if you have some Java tests in the same module, as much of spark does, you need to turn them off -which you can do by telling surefire to run a test that isn't there
Here is the test that I've just been running
mvn test -Dtest=moo -DwildcardSuites=org.apache.spark.deploy.yarn.ClientSuite
That skips the java test and only runs the scala one.
One thing which scalatest doesn't seem to do is let you run a single test within a suite, the way maven surefire does. That's not ideal if you have one failing test in a big suite.
[Correction 2016-08-22: looks like you can ask for a specific suite by name; look at the other answers below. Happy to be wrong].

How to compile tests with SBT without running them

Is there a way to build tests with SBT without running them?
My own use case is to run static analysis on the test code by using a scalac plugin. Another possible use case is to run some or all of the test code using a separate runner than the one built into SBT.
Ideally there would be a solution to this problem that applies to any SBT project. For example, Maven has a test-compile command that can be used just to compile the tests without running them. It would be great if SBT had the same thing.
Less ideal, but still very helpful, would be solutions that involve modifying the project's build files.
Just use the Test / compile command.
Test/compile works for compiling your unit tests.
To compile integration tests you can use IntegrationTest/compile.
Another hint to continuously compile on every file change: ~Test/compile
We have a build.sbt file that is used for multiple projects. Doing sbt test:compile compiled the tests for every single project and took over 30 minutes.
I found out I can compile only the tests for a specific project named xyz by doing:
sbt xyz/test:compile
Using sbt version 1.5.0 and higher test:compile returns deprecation warning.
Use Test / compile.
(docs)