Imagine the situation, when you have several test suites for your application. And the problem arise, when you need to run only specific test or tests, to check the new fix, for example. There is absolutely no need to run all of them. To see, how you can do it with ScalaTest in sbt, let’s create a simple project ScalaTestDemo with three suites and some tests inside them.
- TestSuiteA1
- TestA1 – N1
- TestA1 – N2
- TestA1 – N11
- TestSuiteA2
- TestA2 – N1
- TestSuiteB1
- TestB1 – N1
Now your project should look like in the picture below.
If you want to run particular suites, you can use testOnly and provide their names in a space separated list
[IJ]sbt:ScalaTestDemo> testOnly TestSuiteA1 TestSuiteB1 [info] TestSuiteA1: [info] - TestA1 - N1 [info] - TestA1 - N2 [info] - TestA1 - N11 [info] TestSuiteB1: [info] - TestB1 - N1 [info] Run completed in 244 milliseconds. [info] Total number of tests run: 4 [info] Suites: completed 2, aborted 0 [info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.
Or you can specify a mask.
[IJ]sbt:ScalaTestDemo> testOnly TestSuiteA* [info] TestSuiteA2: [info] - TestA2 - N1 [info] TestSuiteA1: [info] - TestA1 - N1 [info] - TestA1 - N2 [info] - TestA1 - N11 [info] Run completed in 210 milliseconds. [info] Total number of tests run: 4 [info] Suites: completed 2, aborted 0 [info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.
But what if you want to run only specific test inside a particular suite? You can also pass arguments for individual runs by using testOnly and placing them after –. Parameter -t, for example, allows you to specify the name of the test.
[IJ]sbt:ScalaTestDemo> testOnly TestSuiteA1 -- -t "TestA1 - N2" [info] TestSuiteA1: [info] - TestA1 - N2 [info] Run completed in 272 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.
Moreover, you can specify the names of the tests by substring and -z parameter.
[IJ]sbt:ScalaTestDemo> testOnly TestSuiteA* -- -z N1 [info] TestSuiteA2: [info] - TestA2 - N1 [info] TestSuiteA1: [info] - TestA1 - N1 [info] - TestA1 - N11 [info] Run completed in 222 milliseconds. [info] Total number of tests run: 3 [info] Suites: completed 2, aborted 0 [info] Tests: succeeded 3, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.
That’s it. Simple and powerful. Now you are all set and able to run the tests you really want to.