How do I can specify the test from the build.sbt file , I wanted to run one test only and I used the filter as in the sbt docs, but it doesn't work with me, this is my code I have two test classes and in my sbt I specify test1 to be rub but it seems that the two test are running at the same time any one know what I should do ?
Test1Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test1Demo extends FlatSpec with Matchers{
"value of x " should " be 9 " in { assert(my.App.x == 9) }
}
Test2Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test2Demo extends FlatSpec with Matchers{
"value of y " should " be 8 " in { assert(my.App2.y == 8) }
}
build.sbt
version := "0.1"
scalaVersion := "2.12.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
testOptions in Test := Seq(Tests.Filter(s => s.startsWith("Test1")))
the output :
[info] Done updating.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/classes ...
[info] Done compiling.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] Test2Demo:
[info] value of y
[info] - should be 8
[info] Test1Demo:
[info] value of x
[info] - should be 9
[info] Run completed in 6 seconds, 365 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 264 s, completed Apr 15, 2019 2:47:10 PM
If you want to run value of x test from Test1Demo:
testOnly *Test1Demo -- -z value
This sbt command will run only the tests whose name includes the substring "value".
For exact match rather than substring, use -t instead of -z.
Pay attention to -- (two -, not one)
Related
Please assume the normal "newbie" appologies.
I'm using scala 2.12.10; In build.sbt I added ScalaTest:
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
I'm adding some scala.js to an existing java project so my scala source paths are normal but I made some empty directories for java and resources and use a different target so there would be no collision with existing code:
javaSource in Compile := baseDirectory.value / "src/main/scalajs_java"
javaSource in Test := baseDirectory.value / "src/test/scalajs_java"
resourceDirectory in Compile := baseDirectory.value / "src/main/scalajs_resources"
resourceDirectory in Test := baseDirectory.value / "src/test/scalajs_resources"
target := baseDirectory.value / "scalajs_target"
I put the ScalaTest example file ExampleSpec.scala in src/test/scala/ExampleSpec.scala. The example ran fine using org.scalatest.run as described in http://www.scalatest.org/.
The test classpath looks reasonable, I thought:
sbt:RedsPro-ScalaJS> show test:fullClasspath
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/test-classes)
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/classes)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-library_2.12/jars/scalajs-library_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-dom_sjs0.6_2.12/jars/scalajs-dom_sjs0.6_2.12-0.9.7.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-bridge_2.12/jars/scalajs-test-bridge_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-interface_2.12/jars/scalajs-test-interface_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalatest/scalatest_2.12/bundles/scalatest_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalactic/scalactic_2.12/bundles/scalactic_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.2.0.jar)
After compiling, the test is where I would expect it:
> ls scalajs_target/scala-2.12/test-classes/
ExampleSpec.class ExampleSpec.sjsir JS_DEPENDENCIES org/
However, sbt apparently isn't convinced there are any tests:
sbt:RedsPro-ScalaJS> show definedTestNames
[info] *
[success] Total time: 1 s, completed Dec 1, 2019, 11:37:51 AM
sbt:RedsPro-ScalaJS> testOnly ExampleSpec
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for Test / testOnly
[success] Total time: 1 s, completed Dec 1, 2019, 12:44:35 PM
A point in the right direction would be greatly appreciated.
You need to use %%% instead of %% when depending on ScalaTest. In general, you always need to use %%% in Scala.js.
I have a test class as
import org.scalatest.FlatSpec
import scala.collection.mutable
class Tags101Spec extends FlatSpec {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new mutable.Stack[Int]
stack.push(1)
stack.push(2)
assert(stack.pop() === 2)
assert(stack.pop() === 1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new mutable.Stack[String]
intercept[NoSuchElementException] {
emptyStack.pop()
}
}
"A String" should "return 0 size when empty" taggedAs (Fast) in {
assert("".size === 0)
}
"A Sorted List of 10 numbers" must "return 10 as the first element when reversed" taggedAs (Slow) in {
assert(10 === (1 to 10).toList.reverse.head)
}
}
In the same directory, I have a class called Tags which looks like
import org.scalatest.Tag
object Slow extends Tag("Slow Tests")
object Fast extends Tag("Fast Tests")
I run my tests using sbt by including a tag using -n flag and it works
sbt:Unit Testing in Scala> testOnly -- -n Fast
[info] Tags101Spec:
[info] A Stack
[info] A String
[info] A Sorted List of 10 numbers
[info] Run completed in 137 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
Since only 1 test was taggedAs(Fast), the test ran only one test.
Now, I want to do the opposite, exclude the Fast tag and run remaining tests. Here is what I tried
sbt:Unit Testing in Scala> testOnly -- -l Fast
[info] Tags101Spec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] A String
[info] - should return 0 size when empty
[info] A Sorted List of 10 numbers
[info] - must return 10 as the first element when reversed
[info] Run completed in 252 milliseconds.
[info] Total number of tests run: 4
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0
And as you see, it ran 4 tests, which is all the tests, including 1 Fast tagged test.
What am I missing here? How can I make exclusion tag work with sbt?
Thanks
The argument to -l or -n should be the name string argument passed to Tags constructor, not the name of the object. For example, given
object Slow extends Tag("SlowTests")
object Fast extends Tag("FastTests")
then exclude with
testOnly -- -l FastTests
instead of
testOnly -- -l Fast
which outputs
[info] Tags101Spec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] A String
[info] A Sorted List of 10 numbers
[info] - must return 10 as the first element when reversed
[info] Run completed in 187 milliseconds.
[info] Total number of tests run: 3
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 3, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
where we see A string test did not execute.
Personally I would use a fully qualified name as the name argument when constructing Tags like so
package example
import org.scalatest.Tag
object Slow extends Tag("example.Slow")
object Fast extends Tag("example.Fast")
and execute with
testOnly -- -n example.Fast
which outputs
[info] Tags101Spec:
[info] A Stack
[info] A String
[info] - should return 0 size when empty
[info] A Sorted List of 10 numbers
[info] Run completed in 158 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
I'm using Scalatest 3.1.0-SNAP13 and cannot find how to specify the init seed option from this PR.
I'm using SBT to run the test so if there is a way to specify this option in build.sbt would be ideal.
The -S flag seems to be disabled in 3.1.x:
parseLongArgument(seedArgs, "-S") match {
case Some(seed) => // Randomizer.defaultSeed.getAndSet(Some(seed))
println("Note: -S for setting the Randomizer seed is not yet supported.")
case None => // do nothing
}
However it is seems to be enabled in 3.2.x, so try
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0-M1" % Test
and
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-S", "1568769615146")
I managed to get it working with:
// specify an initial seed 0
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-S", "0")
Result:
[info] SummaryTest:
[info] - hello *** FAILED *** (49 milliseconds)
[info] GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation. (SummaryTest.scala:8)
[info] Falsified after 0 successful property evaluations.
[info] Message: 0 was not greater than 1
[info] Location: (SummaryTest.scala:10)
[info] Occurred when passed generated values (
[info] ""
[info] )
[info] Init Seed: 0
I'm trying to get uTest to work with ScalaJS and SBT. SBT is compiling the files, and uTest is running, but it simply ignores my tests. Try as I might I cannot find any difference between my code and the tutorial examples.
build.sbt:
enablePlugins(ScalaJSPlugin)
name := "Scala.js Stuff"
scalaVersion := "2.11.5" // or any other Scala version >= 2.10.2
scalaJSStage in Global := FastOptStage
libraryDependencies += "com.lihaoyi" %% "utest" % "0.3.0"
testFrameworks += new TestFramework("utest.runner.Framework")
src/test/scala/com/mysite/jovian/GeometryTest.scala:
package com.mysite.jovian
import utest._
object GeometryTest extends TestSuite {
def tests = TestSuite {
'addPoints {
val p: Point = new Point(3,4)
val q: Point = new Point(4,3)
val expected: Point = new Point(8,8)
assert(p.plus(q).equals(expected))
throw new Exception("foo")
}
'fail {
assert(1==2)
}
}
}
Output:
> reload
[info] Loading project definition from /Users/me/Dropbox (Personal)/mysite/flocks/project
[info] Set current project to Scala.js Stuff (in build file:/Users/me/Dropbox%20(Personal)/mysite/flocks/)
> test
[success] Total time: 1 s, completed Mar 6, 2015 7:01:41 AM
> test-only -- com.mysite.jovian.GeometryTest
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for test:testOnly
[success] Total time: 1 s, completed Mar 6, 2015 7:01:49 AM
If I introduce a syntax error, sbt test does see it:
> test
[info] Compiling 1 Scala source to /Users/me/Dropbox (Personal)/mysite/flocks/target/scala-2.11/test-classes...
[error] /Users/me/Dropbox (Personal)/mysite/flocks/src/test/scala/com/mysite/jovian/GeometryTest.scala:21: not found: value blablablablabla
[error] blablablablabla
[error] ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 1 s, completed Mar 6, 2015 7:03:54 AM
So it's definitely seeing the code, it just doesn't seem to think that "tests" contains any tests.
Otherwise, in the non-test code, SBT+ScalaJS seems to be working fine...
Thanks for any help, I am mystified.
Your mistake lies in the dependency on uTest:
libraryDependencies += "com.lihaoyi" %% "utest" % "0.3.0"
This is a JVM dependency. To use the Scala.js-enabled dependency, use %%% instead of %%, like this:
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0"
Additionally, you probably want this dependency only in the Test configuration, so add % "test" a the end:
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test"
I have a multi-module Play! application built with sbt and I have a problem on CI server with parallel tests and evolutions: when sbt starts tests and first starts evolution script, other fails because of "Database 'default' is in inconsistent state" for a while (until evolution script stops).
I know how to execute evolutions manually and how to run them from sbt, but I don't know how can I plug this configuration to sbt to ensure evolutions are applied only once and before all tests from all modules.
For reference I'm using following for running evolutions:
object ApplyEvolutions extends App {
OfflineEvolutions.applyScript(
new File("."),
this.getClass.getClassLoader,
args.headOption.getOrElse("default"))
}
And my sbt settings:
val runEvolution = taskKey[Unit]("Applies evolutions")
lazy val runEvolutionTask = runEvolution := {
val log = streams.value.log
val dbName = "default"
Run.executeTrapExit( {
log.info(s"Applying evolutions for database $dbName")
Run.run("controllers.ApplyEvolutions",
(fullClasspath in Compile).value.map { _.data },
Seq(dbName),
log
)(runner.value)
}, log )
}
lazy val runEvolutionsBeforeTests = Seq(
Tasks.runEvolutionTask,
(test in Test) <<= (test in Test) dependsOn Tasks.runEvolution
)
tl;dr Use alias
I use SBT 0.13.2-M3.
[task-dependson]> about
[info] This is sbt 0.13.2-M3
[info] The current project is {file:/Users/jacek/sandbox/so/task-dependsOn/}task-dependson 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: sbt.plugins.IvyModule, sbt.plugins.JvmModule, sbt.plugins.GlobalModule, com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, sbtman.Plugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3
Here's an example of a task - sayHelloT - that's executed once before test in all subprojects. I use Specs2 as the testing framework.
import java.util.concurrent._
lazy val a = project
lazy val b = project
lazy val sayHelloT = taskKey[Unit]("Say Hello")
sayHelloT := {
val log = streams.value.log
log.info("Sleeping for 5 secs...")
TimeUnit.SECONDS.sleep(5)
log.info("Hello, my friend")
}
libraryDependencies in ThisBuild += "org.specs2" %% "specs2" % "2.3.10" % "test"
scalacOptions in Test ++= Seq("-Yrangepos")
addCommandAlias("sht", ";sayHelloT; test")
As you can see there are two subprojects - a and b - and a single alias sht that will execute sayHelloT and only after it finishes successfully which takes 5 secs, test kicks in.
[task-dependson]> sht
[info] Sleeping for 5 secs...
[info] Hello, my friend
[success] Total time: 5 s, completed Mar 12, 2014 10:19:39 PM
[info] ASpec
[info]
[info] A project should
[info] + contain 11 characters
[info]
[info] Total for specification ASpec
[info] Finished in 205 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for task-dependson/test:test
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info] BSpec
[info]
[info] B project should
[info] + contain 11 characters
[info]
[info] Total for specification BSpec
[info] Finished in 240 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed Mar 12, 2014 10:19:42 PM