Skip to main content

A fixture to support expect tests in pytest

Project description

PyPI version Python versions See Build Status on AppVeyor

A fixture to support expect tests (golden tests) in pytest

This code was mostly copied from ezyang/expecttest who wrote an implementation for unittest.


You can install “pytest-expect-test” via pip from PyPI:

$ pip install pytest-expect-test


Start by writing your test, printing out any interesting output, and then calling expect with an empty string

# Function we are testing
def cumulative_sum(nums):
    cum_sum = 0
    result = []
    for num in nums:
        cum_sum += num
    return result

def test_simple(expect):
    print(cumulative_sum([2, 3, 5]))
    print(cumulative_sum([1, 5, 9]))

Then run:

$ pytest

Note that the test fails because we expected nothing to be printed (e.g. we passed an empty string to the expect function), but there was some text that was printed.

We can automatically fix these tests by running:


Our test will then be updated to look like:

def test_simple(expect):
    print(cumulative_sum([2, 3, 5]))
[2, 5, 10]
    print(cumulative_sum([1, 5, 9]))
[1, 6, 15]


Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.


Distributed under the terms of the MIT license, “pytest-expect-test” is free and open source software

This pytest plugin was generated with Cookiecutter along with @hackebrot’s cookiecutter-pytest-plugin template.


If you encounter any problems, please file an issue along with a detailed description.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-expect-test-0.1.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

pytest_expect_test-0.1.0-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page