How to Filter Test Results with Tags
Intro
As your test suite grows you'll likely employ some kind of folder structure to help make sense of everything -- grouping similar tests together. But what do you do when you want to run a set of tests that span across your organizational structure? Especially when your organizational structure isn't set in stone?
A Solution
By applying some metadata to your tests, you can specify a filter at runtime to execute a focused group of tests, returning just the ones you want.
Here are some tagging ideas to help get you started:
wip(e.g., work in progress)smoke(e.g., smoke tests)moneymakers(e.g., the user paths that correlate to the bottom line of the business)component namestory number(e.g., traceability to your development stories)
Let's start with an example.
Example
Each example has two tests. One is tagged as smoke: it loads the homepage of
the-internet and checks the title. The other one is tagged as wip:
it's not finished yet, so it fails.
Each test framework has its own name and syntax for tags (e.g., markers in pytest, categories in MSTest, tags in JUnit
and RSpec, and a naming convention with --grep in Mocha), and a way to filter by them at run time. We configure each
of them to run only the smoke tests by default, so the unfinished test doesn't run. If the filter didn't work, the
wip test would run and fail.
Expected Behavior
- Run the test suite
- The test suite identifies which tests to run based on the tags provided
- Executes the filtered set of tests, ignoring the rest
Summary
By utilizing metadata tags and filtering a set of tests, you have learned to effectively create a dynamic test suite that takes into account all of your tests.
Happy Testing!