Often people comment the code to debug a test. For instance, there is a python’s module with 5 tests. And just one of them has to be used for debugging. So, usually, all other tests will be commented during the debugging or test development. How to improve this experience?
pytest.mark
decorator
pytest.mark
decorator allows assigning specific labels for a test. To find out more details click here.
How does it look like?
# test_something.py
import pytest
@pytest.mark.debug # this test is marked with "debug" label
def test_one():
print("Test #1")
def test_two():
print("Test #2")
def test_three():
print("Test #3")
Okay, and what’s next?
$ py.test -v -m debug
================================= test session starts ==================================
platform darwin -- Python 3.6.1, pytest-3.2.5, py-1.5.2, pluggy-0.4.0 -- /Users/extsoft/.pyenv/versions/3.6.1/envs/pytest-mark/bin/python
cachedir: .cache
rootdir: /Users/extsoft/data/edu/pytest-mark, inifile:
collected 3
test_something.py::test_one PASSED
================================== 2 tests deselected ==================================
======================== 1 passed, 2 deselected in 0.01 seconds ========================
As you can see, the only debug
test was executed.
Can I use the same from PyCharm?
Conclusion
@pytest.mark
s allow to save a lot of time during tests development or debugging. Are you still commenting the code?