ScalaTest – How to Disable Test Cases using Tagging

Reading Time: 3 minutes

Scala Test – How to disable Test Cases using Tagging

Tagging your tests

ScalaTest allows you to define some test categories, to “tag” tests as belonging to those categories, and filter tests to run based on their tags. For example, you could tag some tests as being slow and chose to exclude the slow tests during some runs, or say you have some intergration test that does not runs perfectly on your jenkins(or some other CI tool). ScalaTest supports one tag by default: ignore. You can tag a test as ignored to “switch it off” temporarily.

Tagging tests as ignored

For “temporarily” disabling a test(ofcourse you can restore it later), each style trait provides a way to tag tests as ignored. For example, in a FlatSpec you can change an it or an in to ignore.

Lets take an example –

Suppose you have a sample object named calculator that does the common calculation jobs.

/**
* Created by knoldus on 13/10/17.
*/
object Calculator{

def add(value1:Int, value2:Int): Int ={
value1 + value2
}

def sub(value1:Int, value2:Int): Int ={
value1 – value2
}

def mul(value1:Int, value2:Int): Int ={
value1 * value2
}

def div(value1:Int, value2:Int): Int = {
value2 match {
case value2 if value2 > 0 => value1 / value2
case _ => 0
}
}
}

and then comes its test cases –

import org.scalatest.FlatSpec

/**
* Created by knoldus on 13/10/17.
*/
class CalculatorSpec extends FlatSpec {

“A Calculator” should “adds two no correctly” in {
assert(Calculator.add(value1 = 2, value2 = 2) === 4)
}

“A Calculator” should “subtract two no correctly” in {
assert(Calculator.sub(value1 = 2, value2 = 2) === 0)
}

“A Calculator” should “multiply two no correctly” in {
assert(Calculator.mul(value1 = 2, value2 = 2) === 4)
}

“A Calculator” should “divide two no correctly” in {
assert(Calculator.div(value1 = 2, value2 = 2) === 1)
}

“A Calculator” should “only divide two no correctly when second no is > 0 else return 0” in {
assert(Calculator.div(value1 = 2, value2 = 0) === 0)
}
}

Now if you run the test spec you will see that last test cases will not run –

Output :-
[info] CalculatorSpec:
[info] A Calculator
[info] – should adds two no correctly
[info] A Calculator
[info] – should subtract two no correctly
[info] A Calculator
[info] – should multiply two no correctly
[info] A Calculator
[info] – should divide two no correctly
[info] – should A Calculator only divide two no correctly when second no is > 0 else return 0 !!! IGNORED !!!

Defining and using your own tags –

This one is interesting, where you can define you own custom tags and allow/disallow the test cases by “tagging” them, lets see how it can be done.

import org.scalatest.Tag

object CalcTag extends Tag(“CalcTag”)

// we just define a tag named Calctag

To use this tag –

Just use the keyword “taggedAs” and mark the test cases, e.g –

“A Calculator” should “only divide two no correctly when second no is > 0 else return 0” taggedAs CalcTag in {
assert(Calculator.div(value1 = 2, value2 = 0) === 0)
}

Now to run and exclude the test case use the -l CalcTag(L letter n lowercase) test argument –

sbt test-only — -l CalcTag

Output –

> test-only — -l “CalcTag”
[info] CalculatorSpec:
[info] A Calculator
[info] – should adds two no correctly
[info] A Calculator
[info] – should subtract two no correctly
[info] A Calculator
[info] – should multiply two no correctly
[info] A Calculator
[info] – should divide two no correctly
[info] A Calculator
[info] Run completed in 145 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
[info] All tests passed.

And to enable the test case –

sbt test-only — -n CalcTag


KNOLDUS-advt-sticker

1 thought on “ScalaTest – How to Disable Test Cases using Tagging3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading