Skip to content

Instantly share code, notes, and snippets.

@hkage
Last active September 2, 2018 15:42
Show Gist options
  • Save hkage/b208866ebae37af6c129a3df5c9c8e62 to your computer and use it in GitHub Desktop.
Save hkage/b208866ebae37af6c129a3df5c9c8e62 to your computer and use it in GitHub Desktop.
Cheatsheet for pytest features

pytest Cheatsheet

@pytest.fixture

  • scope: the scope for which this fixture is shared, one of “function” (default), “class”, “module”, “session”.
  • params: an optional list of parameters which will cause multiple invocations of the fixture function and all of the tests using it.
  • autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture.
  • ids: list of string ids each corresponding to the params so that they are part of the test id. If no ids are provided they will be generated automatically from the params.

@pytest.mark.hookwrapper

@pytest.mark.parametrize

@pytest.mark.skip

  • msg: Reason why the test will be skipped.

@pytest.mark.skipif

  • condition: Skip the test if the given condition is true.
  • reason: Reason why the test will be skipped

@pytest.mark.usefixtures

  • *fixturenames: One or more fixture names to be loaded for the test.

The test function doesn't need a direct access to the fixture (object).

@pytest.mark.tryfirst

@pytest.mark.trylast

@pytest.mark.xfail

  • reason: Reason why the test will be skipped

You expect a test to fail, e.g. due to implementation errors or known bugs.

@pytest.raises

  • expected_exception

Assert that a code block/function call raises expected_exception and raise a failure exception otherwise.

@pytest.yield_fixture

(in pytest 3: @pytest.fixture(yield=True))

pytest-django

@pytest.mark.django_db

@pytest.mark.urls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment