## Accelerators

### Go to Overview

KDP KDSP

PremonR Studio9

#### TechHub

Akka Scala Rust Spark Functional Java Kafka Flink ML/AI DevOps Data Warehouse

## Insights # Scala Test – How to disable Test Cases using Tagging

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{

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 ## 1 thought on “ScalaTest – How to Disable Test Cases using Tagging3 min read”

1. joecamel says:

Reblogged this on Coding, Unix & Other Hackeresque Things.
{"ft_lang_label":"__label__en","ft_lang_prob":0.72704774,"math_prob":0.97007346,"size":3816,"snap":"2022-40-2023-06","text_gpt3_token_len":1041,"char_repetition_ratio":0.19805877,"word_repetition_ratio":0.24397591,"special_character_ratio":0.2827568,"punctuation_ratio":0.10699002,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9741156,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-01T08:53:05Z\",\"WARC-Record-ID\":\"<urn:uuid:5d35b044-111e-4e0d-8957-551246fb7c1a>\",\"Content-Length\":\"249297\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:285e4106-2c62-47d1-9d90-a732cbda22a6>\",\"WARC-Concurrent-To\":\"<urn:uuid:1e2ff104-db06-449f-9b5f-af0ea639dd88>\",\"WARC-IP-Address\":\"192.0.78.151\",\"WARC-Target-URI\":\"https://blog.knoldus.com/scalatest-how-to-disable-test-cases-using-tagging/\",\"WARC-Payload-Digest\":\"sha1:MHICOWFEULQ5LVUHG2VJTDVGFBWCKP3O\",\"WARC-Block-Digest\":\"sha1:7VNFIWGUT5BEIQGEO2AEOWGV2CC4NUIR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499919.70_warc_CC-MAIN-20230201081311-20230201111311-00809.warc.gz\"}"}