Skip to main content
https://travis-ci.org/pytest-dev/pytest-describe.svg?branch=master

Describe-style plugin for pytest

pytest-describe is a plugin for pytest that allows tests to be written in arbitrary nested describe-blocks, similar to RSpec (Ruby) and Jasmine (JavaScript).

The main inspiration for this was a video by Gary Bernhardt.

Installation

You guessed it:

pip install pytest-describe

Example

def describe_list():

    @pytest.fixture
    def list():
        return []

    def describe_append():

        def adds_to_end_of_list(list):
            list.append('foo')
            list.append('bar')
            assert list == ['foo', 'bar']

    def describe_remove():

        @pytest.fixture
        def list():
            return ['foo', 'bar']

        def removes_item_from_list(list):
            list.remove('foo')
            assert list == ['bar']

Why bother?

I’ve found that quite often my tests have one “dimension” more than my production code. The production code is organized into packages, modules, classes (sometimes), and functions. I like to organize my tests in the same way, but tests also have different cases for each function. This tends to end up with a set of tests for each module (or class), where each test has to name both a function and a case. For instance:

def test_my_function_with_default_arguments():
def test_my_function_with_some_other_arguments():
def test_my_function_throws_exception():
def test_my_function_handles_exception():
def test_some_other_function_returns_true():
def test_some_other_function_returns_false():

It’s much nicer to do this:

def describe_my_function():
    def with_default_arguments():
    def with_some_other_arguments():
    def it_throws_exception():
    def it_handles_exception():

def describe_some_other_function():
    def it_returns_true():
    def it_returns_false():

It has the additional advantage that you can have marks and fixtures that apply locally to each group of test function.

With pytest, it’s possible to organize tests in a similar way with classes. However, I think classes are awkward. I don’t think the convention of using camel-case names for classes fit very well when testing functions in different cases. In addition, every test function must take a “self” argument that is never used.

The pytest-describe plugin allows organizing your tests in the nicer way shown above using describe-blocks. The functions inside the describe-blocks need not follow any special naming convention, they are always executed as tests unless they start with an underscore. The functions used for describe-blocks must start with describe_, but you can configure this prefix with the setting describe_prefixes in the pytest configuration file.

Shared Behaviors

If you’ve used rspec’s shared examples or test class inheritance, then you may be familiar with the benefit of having the same tests apply to multiple “subjects” or “suts” (system under test).

from pytest import fixture
from pytest_describe import behaves_like

def a_duck():
    def it_quacks(sound):
        assert sound == "quack"

@behaves_like(a_duck)
def describe_something_that_quacks():
    @fixture
    def sound():
        return "quack"

    # the it_quacks test in this describe will pass

@behaves_like(a_duck)
def describe_something_that_barks():
    @fixture
    def sound():
        return "bark"

    # the it_quacks test in this describe will fail (as expected)

Fixtures defined in the block that includes the shared behavior take precedence over fixtures defined in the shared behavior. This rule only applies to fixtures, not to other functions (nested describe blocks and tests). Instead, they are all collected as separate tests.

Metadata

Release files for pytest-describe 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-describe 1.0.0
File Size Uploaded
pytest-describe-1.0.0.tar.gz 8.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pytest-describe 1.0.0
File Interpreter ABI Platform
pytest_describe-1.0.0-py3-none-any.whl Python 3 none any Details
pytest_describe-1.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 22.0 kB

Release files / pytest-describe-1.0.0.tar.gz

Download URL pytest-describe-1.0.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3e2ea0e77efa09edb98cf90423bf1da21a462ed90bd3120f8f98fe7519a167d5
BLAKE2b-256 checksum
How to use checksums
1132e9d1e27791be3fe83bf38f2165d67ba8c52686de9ccc3796a294ddf0d4cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.2

Release files / pytest_describe-1.0.0-py3-none-any.whl

Download URL pytest_describe-1.0.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95fe78639d4d16c4a1e7d62c70f63030b217c08d2ee6dca49559fe6e730c6696
BLAKE2b-256 checksum
How to use checksums
c04a072a2f91f54cb83578ae10ebbe22c1d3d7f4ada388be774e8e39cd738af2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.2

Release files / pytest_describe-1.0.0-py2-none-any.whl

Download URL pytest_describe-1.0.0-py2-none-any.whl
Size 6.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
cc3862662faa5a6fb721927aaef46b46cf787e4a8163e5459fc8778e650fabad
BLAKE2b-256 checksum
How to use checksums
efb519515c49f45658d54f60d6a7d2162e2819d163bb5d304edc9355ffe33660
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.2

Release history Release notifications | RSS feed

3.2.0

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

This release

1.0.0 This release

3 release files

0.12.0

1 release file

0.11.1

1 release file

0.11.0

1 release file

0.10.3

1 release file

0.10.2

1 release file

0.10.1

1 release file

0.10

1 release file

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page