File: README.md

package info (click to toggle)
python-expecttest 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 140 kB
  • sloc: python: 535; makefile: 6; sh: 4
file content (92 lines) | stat: -rw-r--r-- 3,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# expecttest [![PyPI version](https://badge.fury.io/py/expecttest.svg)](https://badge.fury.io/py/expecttest)

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:
     ```py
     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.
     ```py
     self.assertExpectedInline(some_func(), """my_value""")
     ```

## A minimal working example

```python
# 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`:

```python
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`:

```python
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

- [expect-test](https://docs.rs/expect-test) for Rust