How to include or exclude tests for execution based on their name using PyTest?

 You can use the -k command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that -m provides. This makes it easy to select tests based on their names:

def mytest1():
    pass

def mytest2():
    pass
pytest -v -k mytest1

To exclude test case using not keyword inside double quotes. Below code will execute mytest2 not mytest1.

pytest -k "not mytest1" -v

You can use or keyword to select multiple test cases matching name or keyword. Below code will execute both test cases.

pytest -k "mytest1 or mytest2" -v