Test Filter Patterns

There are times when a user needs to concisely specify a set of tests to maelstrom-pytest. One of those is on the command line: maelstrom-pytest can be told to only run a certain set of tests, or to exclude some tests. Another is the filter field of maelstrom-pytest.toml directives. This is used to choose which tests a directive applies too.

In order to allow users to easily specify a set of tests to maelstrom-pytest, we created the domain-specific pattern language described here.

If you are a fan of formal explanations check out the BNF. Otherwise, this page will attempt to give a more informal explanation of the language.

Simple Selectors

The most basic patterns are "simple selectors". These are only sometimes useful on their own, but they become more powerful when combined with other patterns. Simple selectors consist solely of one of the these identifiers:

Simple SelectorWhat it Matches
true, any, allany test
false, noneno test

Simple selectors can optionally be followed by (). That is, all() and all are equivalent patterns.

Compound Selectors

"Compound selector patterns" are patterns like name.equals(foo). They combine "compound selectors" with "matchers" and "arguments". In our example, name is the compound selector, equals is the matcher, and foo is the argument.

These are the possible compound selectors:

Compound SelectorSelected Name
namethe name of the test
filethe name of the test's file
packagethe name of the test's package
node_idthe name of the test's Pytest "nodeid"

These are the possible matchers:

MatcherMatches If Selected Name...
equalsexactly equals argument
containscontains argument
starts_withstarts with argument
ends_withends with argument
matchesmatches argument evaluated as regular expression
globsmatches argument evaluated as glob pattern

Compound selectors and matchers are separated by . characters. Arguments are contained within delimiters, which must be a matched pair:

LeftRight
()
[]
{}
<>
//

Let's put this all together with some examples:

PatternWhat it Matches
node_id.equals(test_mod.py::TestClass::test_method)Any test named "test_mod.py::TestClass::test_method".
file.contains/maelstrom/Any test in file whose name contains the substring "maelstrom".
package.matches{(foo)*bar}Any test whose package name matches the regular expression (foo)*bar.

Markers Selector

The Pytest markers can be used to select tests as well. Each test has a set of markers associated with it. Using a markers selector, one can select tests that contain a specific marker. This is basically a compound selector named markers with only one matcher: contains.

In this case, contains doesn't do substring matching, but instead matches if the argument is one of the markers for the test.

For example:

PatternWhat it Matches
markers.contains(foo)Any test named that has a foo marker.
markers.contains/bar/Any test named that has a bar marker.

Compound Expressions

Selectors can be joined together with operators to create compound expressions. These operators are:

OperatorsAction
!, ~, notLogical Not
&, &&, andLogical And
|, ||, orLogical Or
\, -, minusLogical Difference
(, )Grouping

The "logical difference" action is defined as follows: A - B == A && !B.

As an example, to select tests named foo or bar in package baz:

(name.equals(foo) || name.equals(bar)) && package.equals(baz)

As another example, to select tests named bar in package baz or tests named foo from any package:

name.equals(foo) || (name.equals(bar) && package.equals(baz))

Abbreviations

Selector and matcher names can be shortened to any unambiguous prefix.

For example, the following are all the same

name.equals(foo)
name.eq(foo)
n.eq(foo)

We can abbreviate name to n since no other selector starts with "n", but we can't abbreviate equals to e because there is another selector, ends_with, that also starts with an "e".