Run scalatest in main instead of test through sbt? - scala

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

Related

compile/package multiple configurations from command line sbt scala

is there a way to build/compile all configurations at once? I have a project that has a Dev configuration in addition to the default Compile and Test configuration, and i am looking for a command or a setting in my build.sbt that would allow me to compile/package all 3 configurations at once.
Basically looking for a way to avoid having to do these 3 commands to build the entire source tree:
sbt compile
sbt dev:compile
sbt test:compile
When I use sbt from IntelliJ it is able to do this on building the project, but I am looking to do this from the command line.
First, you can run multiple tasks with a single sbt invocation:
sbt compile dev:compile test:compile
Second, you could define an alias in your build which does what you want:
addCommandAlias("compileAll", "; compile; dev:compile; test:compile")
Then, just run sbt compileAll.

Run single test suite with ScalaTest

I have a simple Scala 2.11.8 + ScalaTest 3.0.1 + SBT project.
I want to run a single ScalaTest Suite class. I created a trivial example test suite org.example.TestSuite:
package org.example
import org.scalatest.FunSuite
class TestSuite extends FunSuite {
test("just a simple example test") {
assert(1 + 1 === 2)
}
}
I can run it in isolation from IntelliJ perfectly fine.
When I try to run this in isolation on the command line:
sbt test-only org.example.TestSuite
That runs all of my tests.
I look at the docs here: http://www.scalatest.org/user_guide/using_scalatest_with_sbt
And it says to do exactly what I'm doing, test runs everything, while test-only <qualified suite class names> runs just specified suites. However, it just isn't working.
This seems outrageously simple but it's just not working and running all tests. Thank you!
In your command line, SBT will take commands with arguments such as yours by enclosing the command and the arguments in quotes:
sbt 'test-only org.example.TestSuite'
If you enter SBT's command line, then you don't need to specify the quotes and just run your command like:
$ sbt
> test-only org.example.TestSuite
Note that this last example is how the examples in the documentation link you have posted were meant to be used.
Please note also that in more recent SBT versions such as 0.13 the command invocation was changed to testOnly.

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"

SBT run single test class in submodule

I have a multi module project with moduleA, moduleB, and moduleC. I want to run my class com.helpme.run.MyTest from moduleB.
My guess is the sbt command should look like:
sbt "project moduleA" --mainClass com.helpme.run.MyTest test
But no luck. Please help!! Thanks!
First of all you can run a test by using testOnly
$ sbt testOnly MyTest
But if your project is a multi-project sbt project and you have the same test class with the same name in more than one project you can navigate between projects by project command and then run the test
$ sbt
> project moduleA
> testOnly MyTest
Note that you have to first run sbt and then run the rest of commands from the sbt shell.
Depends on your project configuration testOnly couldn't work
You can try this command too:
sbt "project myProject" "testOnly com.this.is.my.Test"

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].