Mark
A fixture mark is to mark some particular files. There are a few mark type:
Skip mark
We can use skip mark to skip a test case, for example:
import pytest
class TestDatabase:
@pytest.mark.skip(reason = "Not ready")
def test_givenDatabase_shouldSkipTest(self, database):
raise NotImplemenedError()
[!important]
In this example, we are using the code from Fixture
When running pytest
, this test will be marked as skip (s)
================================================ test session starts =================================================
platform darwin -- Python 3.9.10, pytest-8.2.2, pluggy-1.5.0
rootdir: /Users/austin/projects/learn/python/pytest
collected 5 items
tests/main_test.py . [ 20%]
tests/services/DatabaseMark_test.py s [ 40%]
tests/services/MyServiceFixture_test.py .. [ 80%]
tests/services/MyService_test.py . [100%]
============================================ 4 passed, 1 skipped in 0.01s ============================================
Expect failed
We can mark a test that it's expect to fail, for example:
class TestDatabase:
@pytest.mark.xfail
def test_givenDatabase_shouldExpectFal(self, database):
actual = database.query("some query")
assert actual is not None
As a result, when running, it will show as xfailed (x)
====================================== 4 passed, 1 skipped, 1 xfailed in 0.02s =======================================
And the whole test suit still passed
Custom mark
Custom mark is useful when you want to filter out your test cases. You can then run only your marked tests using
python -m marker
For example, in the root folder, create pytest.ini
├── env
├── pytest.ini
├── requirement.txt
├── src
└── tests
With the content
[pytest]
markers =
regression_test: "tests for regression purpose"
performance_test: "tests for performance purpose"
This will register our markers and let pytest
know that they're valid
Next, we can use these marker in our test
import pytest
class TestDatabase:
@pytest.mark.regression_test
def test_givenDatabase_regression(self, database):
actual = database.query("regression query")
assert actual is None
@pytest.mark.performance_test
def test_givenDatabase_performance(self, database):
actual = database.query("performance query")
assert actual is None
So now, in order to run regression_test
, you use:
pytest -m regression_test
That will only pick up the tests that are masked with regression_test
. Similar for performance_test