Substring Matching of Test Names



To execute the tests containing a string in its name we can use the following syntax −

pytest -k <substring> -v

-k <substring> represents the substring to search for in the test names.

Now, run the following command −

pytest -k great -v

This will execute all the test names having the word ‘great’ in its name. In this case, they are test_greater() and test_greater_equal(). See the result below.

test_compare.py::test_greater FAILED
test_compare.py::test_greater_equal PASSED
============================================== FAILURES 
==============================================
____________________________________________ test_greater 
____________________________________________
def test_greater():
num = 100
>  assert num > 100
E  assert 100 > 100
test_compare.py:3: AssertionError
========================== 1 failed, 1 passed, 3 deselected in 0.07 seconds 
==========================

Here in the result, we can see 3 tests deselected. This is because those test names do not contain the word great in them.

Note − The name of the test function should still start with ‘test’.

Advertisements