@@ -11,15 +11,6 @@ documented outputs.
1111## Before
1212
1313``` python
14- def calculate_total (items , tax_rate ):
15- """
16- >>> calculate_total([10, 20, 30], 0.1)
17- 55
18- """
19- subtotal = sum (items)
20- return subtotal + (subtotal * tax_rate)
21-
22-
2314def test_calculate_total ():
2415 assert calculate_total([10 , 20 , 30 ], 0.1 ) == 55
2516 assert calculate_total([5 , 5 ], 0.2 ) == 10
@@ -28,17 +19,6 @@ def test_calculate_total():
2819## After ` pytest --accept `
2920
3021``` diff
31- # math_helpers.py
32- def calculate_total(items, tax_rate):
33- """
34- >>> calculate_total([10, 20, 30], 0.1)
35- - 55
36- + 66.0
37- """
38- subtotal = sum(items)
39- return subtotal + (subtotal * tax_rate)
40-
41- # test_math_helpers.py
4222def test_calculate_total():
4323- assert calculate_total([10, 20, 30], 0.1) == 55
4424- assert calculate_total([5, 5], 0.2) == 10
@@ -71,13 +51,13 @@ pytest --accept
7151## Why?
7252
7353- Often it's fairly easy to observe whether something is working by viewing the
74- output it produces.
75- - But often output is verbose, and copying and pasting the output into the test
76- is tedious.
77- - ` pytest-accept ` does the copying & pasting for you.
54+ output it produces
55+ - ...but often output is verbose, and copying and pasting the output into the
56+ test is tedious
57+ - ` pytest-accept ` does the copying & pasting for you
7858- Similarly, lots of folks generally find writing any tests a bit annoying, and
7959 prefer to develop by "running the code and seeing if it works". This library
80- aims to make testing a joyful part of that development loop.
60+ aims to make testing a joyful part of that development loop
8161
8262This style of testing is fairly well-developed in some languages, although still
8363doesn't receive the attention I think it deserves, and historically hasn't had
@@ -91,7 +71,7 @@ blog post [How to Test](https://matklad.github.io//2021/05/31/how-to-test.html).
9171
9272## How it works
9373
94- pytest-accept:
74+ ` pytest-accept ` :
9575
9676- Intercepts test failures from both doctests and assert statements
9777- Parses the files to understand where the documented values are
@@ -105,23 +85,6 @@ Things to know:
10585- ** Overwrite by default** : Pass ` --accept-copy ` to write to ` .py.new ` files
10686 instead.
10787
108- ## What about more complex assertions?
109-
110- We currently don't support:
111-
112- - Assertions that compare against non-literal values (e.g.,
113- ` assert result == some_variable ` )
114- - Comparisons besides ` == `
115- - Assert statements with messages
116-
117- For more complex test scenarios, consider:
118-
119- - [ pytest-regtest] ( https://gitlab.com/uweschmitt/pytest-regtest ) for file-based
120- testing
121- - [ syrupy] ( https://github.com/tophat/syrupy ) for snapshot testing
122- - [ pytest-insta] ( https://github.com/vberlier/pytest-insta ) for insta-style
123- review
124-
12588<details >
12689<summary >Doctest quirks</summary >
12790
@@ -166,6 +129,14 @@ approach is well-established in many languages:
166129- [ ppx_expect] ( https://github.com/janestreet/ppx_expect ) (OCaml)
167130- [ insta] ( https://github.com/mitsuhiko/insta ) (Rust)
168131
132+ For more complex test scenarios, consider:
133+
134+ - [ pytest-regtest] ( https://gitlab.com/uweschmitt/pytest-regtest ) for file-based
135+ testing
136+ - [ syrupy] ( https://github.com/tophat/syrupy ) for snapshot testing
137+ - [ pytest-insta] ( https://github.com/vberlier/pytest-insta ) for insta-style
138+ review
139+
169140thanks to [ @untiaker ] ( https://github.com/untitaker ) , who found how to expand the
170141original doctest solution into an approach that works with standard ` assert `
171142statements.
0 commit comments