Skip to main content

No project description provided

Project description

expecttest PyPI version

This library implements expect tests (also known as "golden" tests). Expect tests are a method of writing tests where instead of hard-coding the expected output of a test, you run the test to get the output, and the test framework automatically populates the expected output. If the output of the test changes, you can rerun the test with the environment variable EXPECTTEST_ACCEPT=1 to automatically update the expected output.

Somewhat unusually, this library implements inline expect tests: that is to say, the expected output isn't saved to an external file, it is saved directly in the Python file (and we modify your Python file when updating the expect test.)

The general recipe for how to use this is as follows:

  1. Write your test and use assertExpectedInline() instead of a normal assertEqual. Leave the expected argument blank with an empty string:

    self.assertExpectedInline(some_func(), """""")
    
  2. Run your test. It should fail, and you get an error message about accepting the output with EXPECTTEST_ACCEPT=1

  3. Rerun the test with EXPECTTEST_ACCEPT=1. Now the previously blank string literal will contain the expected value of the test.

    self.assertExpectedInline(some_func(), """my_value""")
    

A minimal working example

# test.py
import unittest
from expecttest import TestCase

class TestStringMethods(TestCase):
    def test_split(self):
        s = 'hello world'
        self.assertExpectedInline(str(s.split()), """""")

if __name__ == '__main__':
    unittest.main()

Run EXPECTTEST_ACCEPT=1 python test.py , and the content in triple-quoted string will be automatically updated.

For people who use pytest:

from expecttest import assert_expected_inline

def test_split():
    s = 'hello world'
    assert_expected_inline(str(s.split()), """""")

Run EXPECTTEST_ACCEPT=1 pytest test.py , and the content in triple-quoted string will be automatically updated.

For parameterized tests, advanced fixturing and other cases where the expectation is in a different place than the assertion, use Expect:

from expecttest import Expect

def test_removing_spaces(s: str, expected: Expect) -> None:
    expected.assert_expected(s.replace(" ", ""))

test_removing_spaces("foo bar", Expect("""foobar"""))
test_removing_spaces("foo bar !!", Expect(""""""))

Run EXPECTTEST_ACCEPT=1 pytest test.py , and the content in triple-quoted string will be automatically updated.

Some tips and tricks

  • Often, you will want to expect test on a multiline string. This framework understands triple-quoted strings, so you can just write """my_value""" and it will turn into triple-quoted strings.

  • Take some time thinking about how exactly you want to design the output format of the expect test. It is often profitable to design an output representation specifically for expect tests.

Similar libraries

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

expecttest-0.3.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

expecttest-0.3.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file expecttest-0.3.0.tar.gz.

File metadata

  • Download URL: expecttest-0.3.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.11 Darwin/24.1.0

File hashes

Hashes for expecttest-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6e8512fb86523ada1f94fd1b14e280f924e379064bb8a29ee399950e513eeccd
MD5 ed3e4bd74405b22d4c01b1dc914e99ed
BLAKE2b-256 220a8868083a2b25c28811a88da6a4cb561f84feab0c9fa953c45820a3eb3a65

See more details on using hashes here.

File details

Details for the file expecttest-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: expecttest-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.8.11 Darwin/24.1.0

File hashes

Hashes for expecttest-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60f88103086e1754240b42175f622be83b6ffeac419434691ee5a5be819d0544
MD5 cfb292f758a971106871f069296d4360
BLAKE2b-256 27fbdeeefea1ea549273817ca7bed3db2f39cc238a75a745a20e3651619f7335

See more details on using hashes here.

Supported by

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