Running scalatest classes from sbt file - scala

I am running my test class from sbt file using task
scalaTaskRun := {
val test = (runMain in Compile).fullInput(" org.scalatest.tools.Runner -s package.tests.TestClass1 -h ReportOutput").evaluated
}
to get a html report output for single test class. But I don't want to add -s TestClass2 again for running one more tests and so on...
If I want to run many test classes from sbt file just like running testNG suite xml which contains more than one test classes. How can the same thing be achieved in sbt scalatest??
I tried running with the runpath command...
Runner -R target\\folder\\classes -w package.testcases -h reportFolder
But it's not running the compiled test classes from classes folder.
Kindly help in fixing it.

You can tell sbt which tests you want to run:
testOnly package.testcases.* -- -h reportFolder
everything after -- gets passed through to the test framework (e.g. ScalaTest).

Related

Run sbt tests in a particular order

In my sbt project, I have two packages in it folder.
I want package.one tests to run first and then package.two. This is needed because tests in package.two are dependent on a Map created and populated by tests from the first package. Is there any way to achieve this?
Lets assume you have following package structure:
p1/
Test1.scala
p2/
Test2.scala
Then you can run following commands:
sbt> testOnly -- -m "p1"
sbt> testOnly -- -m "p2"
This will execute tests from p1 first and then from p2

Cannot run tests with org.scalatest.tools.Runner from command line

I have my test compiled in a directory: samplesuite
It works when running one Suite using org.scalatest.run.
Nothing happens when I try to run the directory that contains several suites.
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.run samplesuite.SomeSpec
But it won't run when trying:
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.tools.Runner -R samplesuite
Assuming tests are defined in package samplesuite, for example
package samplesuite
class SomeSpec extends FlatSpec with Matchers
and current working directory . is one level above samplesuite, then discover and run all suites with
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.tools.Runner -R .
In general, when specifying runpath from which tests will be discovered with -R point to the root directory where your packages start, or to the .jar file generated with sbt test:package:
scala -cp target/scala-2.12/classes:scalatest_2.12-3.0.5.jar:scalactic_2.12-3.0.5.jar org.scalatest.tools.Runner -R target/scala-2.12/test-classes
scala -cp target/scala-2.12/classes:scalatest_2.12-3.0.5.jar:scalactic_2.12-3.0.5.jar org.scalatest.tools.Runner -R target/scala-2.12/how-can-i-perform-an-or-with-scalatest_2.12-0.1.0-SNAPSHOT-tests.jar

Build subproject in Spark with sbt

I want to build subproject in Spark with sbt. I found this example and it works
$ ./build/sbt -Phive -Phive-thriftserver (build)
sbt (spark)> project hive (switch to subproject)
sbt (hive)> testOnly *.HiveQuerySuite -- -t foo ( run test case)
However, I tried the following but it does not build but quit
./build/sbt -mllib
I do not know how does the author figure out -Phive -Phive-thriftserver. I cannot find this in Spark source code.
I just want to do the exact same thing as the example but with a different subproject.
This is not asking how to use projects to print out all available projects.
Specify the project scope:
./build/sbt mllib/compile
refer to: http://www.scala-sbt.org/0.13/docs/Scopes.html

scala sbt test-only parameters are only picked up when run from sbt shell

I've tagged a few of my slower running tests with a "SlowTest" tag. When I run my test suite from the sbt shell via the following command:
test-only * -- -l com.company.tags.SlowTest
the 'SlowTest' tests aren't run. However, when I attempt the same from the bash shell via:
sbt test-only * -- -l com.company.tags.SlowTest
all the tests are run, including the slow ones I'm trying to filter out. What am I missing here?
sbt treats each commandline argument as a separate target to run. You simply need to quote each individual command you wish to run, which means all of the arguments you've provided:
sbt "test-only * -- -l com.company.tags.SlowTest"

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"