Tagging and Ignoring Tests
Learn how to tag your test suites to run them selectively.
We'll cover the following...
When and why should you tag?
In large projects, the ability to tag tests is fundamental. There are two main reasons to tag a test or a set of tests:
Ignoring them
Running them selectively
There are several reasons you might want to ignore tests. For example, they might be too slow to run many times, so we might want to exclude them from some test runs to speed up our development. Or perhaps they’re incomplete or target a feature that hasn’t been fully developed yet, or we’re in the middle of a refactoring and don’t want to run tests because we know they won’t pass.
Sometimes it’s easier to run tests selectively. Most IDEs give us the ability to run single test cases or test suites in isolation, but what if we want to run tests that don’t belong to the same suite? In these cases, it’s helpful to be able to run only the tests with a given tag.
ScalaTest supports both scenarios. In this lesson, we’ll learn how to ignore tests and apply tags to them.
Ignoring a test
Each style trait in ScalaTest has a way to specify which test cases to ignore. We can use the ignore
keyword to tell ScalaTest to skip the execution of a given test case. The output will state clearly that the test was ignored.
testOnly mdipirro.educative.io.effectiveunitandintegrationtestinginscala.matchers.EducativeSpec[info] EducativeSpec:[info] Educative[info] - should enforce uniqueness when adding a new course (15 milliseconds)[info] - should add the new course if there's no course with the same title (3 milliseconds)[info] - should delete more than one course (1 millisecond)[info] - should not delete the course if there's no course with the same title (13 milliseconds)[info] - should filter the courses by tag (9 milliseconds)[info] - should return a valid course, if it exists (1 millisecond)[info] - should group the courses by author !!! IGNORED !!![info] Run completed in 402 milliseconds.[info] Total number of tests run: 6[info] Suites: completed 1, aborted 0[info] Tests: succeeded 6, failed 0, canceled 0, ignored 1, pending 0
Note: You should ignore as few tests as possible.
The trouble is the more tests you ignore (for whatever reason), the more tests you’re likely to ignore. This is explained by the broken windows theory: if a window in a building is broken and not repaired, more windows in the same building will soon be broken and not repaired. In criminology this theory means that visible signs of crime encourage further crime. Similarly, the more tests you ignore, the more tests you will ignore because you can always fix them later. But sometimes tests don’t get fixed, and you end up forgetting them. If you’re into test-driven development, you should ...