I created and used custom markers orange, apple and pineapple as shown below:
# "pytest.ini"
[pytest]
markers =
orange: Orange marker
apple: Apple marker
pineapple: Pineapple marker
import pytest
@pytest.mark.orange
def test1():
assert True
@pytest.mark.apple
def test2():
assert True
@pytest.mark.pineapple
def test3():
assert True
Then, I ran pytest with -k orange and -m orange as shown below:
pytest -k orange
pytest -m orange
Then, there was the exactly same output as shown below:
=================== test session starts ===================
platform win32 -- Python 3.9.13, pytest-7.4.0, pluggy-1.2.0
django: settings: core.settings (from ini)
rootdir: C:\Users\kai\test-django-project2
configfile: pytest.ini
plugins: django-4.5.2
collected 3 items / 2 deselected / 1 selected
tests\test_store.py . [100%]
============= 1 passed, 2 deselected in 0.09s =============
So, what is the difference between -k and -m in Pytest?
-kcan run the markers which contain the given string.-mcan run the markers which is exactly same as the given string.So, if you run
pytestwith-k appleas shown below:Then,
appleandpineappleare run as shown below:Next, if you run
pytestwith-m appleas shown below:Then, only
appleis run as shown below: