Package: pytest-it
Decorate your pytest suite withRSpec-inspiredmarkersdescribe,contextandit. Then runpytest--itto see a plaintext, org-mode compatible spec of the
test structure.Installpytest-itisavailable on PyPi:pip installpytest-it.ExamplesA basic example that usespytest.mark.describe,pytest.mark.contextandpytest.mark.it:frompytestimportmarkasm@m.describe("The test function report format")classTestPytestItExample(object):@m.context("When @pytest.mark.it is used")@m.it("Displays an '- It: ' block matching the decorator")deftest_it_decorator(self,testdir):passThis produces:- Describe: The test function report format...

  - Context: When @pytest.mark.it is used...
    - ✓ It: Displays an '- It: ' block matching the decoratorDescribeandContextblocks can be nested arbitrarily by using multiple
markers, eg:frompytestimportmarkasm@m.describe("The test function report format")classTestPytestItExample(object):@m.context("When @pytest.mark.it is not used")@m.it("Displays the test function name")deftest_no_argument(self,testdir):pass@m.context("When @pytest.mark.it is not used")@m.context("but the test name starts with 'test_it_'")@m.it("Prettifies the test name into the 'It: ' value")deftest_populates_the_it_marker_using_function_name(self,testdir):passThis produces:- Describe: The test function report format...

  - Context: When @pytest.mark.it is not used...
    - ✓ It: Displays the test function name

    - ...but the test name starts with 'test_it_'...
      - ✓ It: Prettifies the test name into the 'It: ' valueBehaviourPytest markers are used to specify theDescribe:,Context:andIt:sections. You can set these in all the usual ways that you specify pytest
markers.DescribeandContextcan be nested arbitrarily.If--collect-onlyis used, it displays the samepytest-itspec as usual, but
without the test result (✓/F/s).If-vis higher than 0, the full path to the test function is include in the
test name.Ifpytest.mark.itis not used on a test, the test name is displayed instead
of theIt: does somethingoutput.Ifpytest.mark.itis not used but the test name starts withtest_it,pytest-itwill prettify the test name into anIt: does somethingvalue.The test output should be able to be copied directly into anorg-modefile.BackgroundPytest provides a lot of useful features for testing in Python, but when testing
complex systems, it can be hard to clearly communicate the intent of a test
using the standardtest_module.py::TestClass::test_functionstructure.One way to improve clarity is to use a BDD testing framework
(eg.Behave,Mamba,Rspec), but
it’s not always desirable to restructure existing test and program code.There are some pytest plugins that attempt to bridge this gap, by providing
alternative ways to structure the tests (eg.pytest-describe,pytest-bdd), or
altering the test report output (eg.pytest-testdox,pytest-pspec).pytest-ittakes a similar approach topytest-testdox, by providing pytest
markers that can describe the test output.pytest-itsupports a few other
features, such as:A plaintext test structure that can easily be copied to markdown/org-mode documents.Arbitrary nesting ofdescribeandcontextmarkers.Supporting the--collect-onlypytest flag to display test structure.Displaying the full path to each test if-vis used.Neatly integrating tests in the output if they don’t use thepytest-itmarkers.Althoughpytest-itdoes not change the behaviour of pytest tests, you may find it
a useful tool for thinking about test structure, and communicating the intention
of both the test code and the system under test.