pytest: don't comment the code to debug a test

Reading time ~1 minute

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?

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 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?


@pytest.marks allow to save a lot of time during tests development or debugging. Are you still commenting the code?

Test design techniques

The list of approaches for designing a great system tests coverage.
Continue reading

A message of the commit

Published on January 23, 2018

Establishing lazy connections in Python

Published on January 09, 2018