Debian Codegraph @

src/pytest_regressions/testing.py

raw symbols SCIP
1 from typing import Any
2 from typing import Callable
3 from typing import Optional
4
5 import pytest
6
7
8 def check_regression_fixture_workflow(
9 pytester: pytest.Pytester,
10 source: str,
11 data_getter: Callable[[], Any],
12 data_modifier: Callable[[], Any],
13 expected_data_1: Any,
14 expected_data_2: Any,
15 compare_fn: Optional[Callable[[object, object], None]] = None,
16 ) -> None:
17 """
18 Helper method to test regression fixtures like `data_regression`. Offers a basic template/script
19 able to validate main behaviors expected by regression fixtures.
20
21 Usage
22 -----
23
24 ```
25 import sys
26
27 monkeypatch.setattr(sys, 'get_data', lambda: 'foo', raising=False)
28 source = '''
29 import sys
30 def test_1(fake_regression):
31 data = sys.get_data()
32 fake_regression.Check(data)
33 '''
34
35 def get_data():
36 fake_filename = os.path.join(str(testdir.tmpdir), 'test_file', 'test_1.fake')
37 assert os.path.isfile(fake_filename)
38 with open(fake_filename) as f:
39 return f.read()
40
41 check_regression_fixture_workflow(
42 pytester,
43 source,
44 data_getter=get_data,
45 data_modifier=lambda: monkeypatch.setattr(sys, 'get_data', lambda: 'bar', raising=False),
46 expected_data_1='foo',
47 expected_data_2='bar',
48 )
49 ```
50
51 :param pytester: pytester fixture.
52 :param source: Source code using regression fixture.
53 :param data_getter: Function without arguments that returns contents of file
54 created by regression test when it fails first time (i.e. the expected file for future
55 runs).
56 :param data_modifier: Function without arguments that must change data compared by
57 regression fixture so it fails in next comparison.
58 :param expected_data_1: Expected data in expected file for first state of data.
59 :param expected_data_2: Expected data in expected file for second state of data.
60 :param compare_fn: function with signature (obtained, expected) used to ensure
61 both data are equal. Should raise an assertion error if both objects are not equal.
62 """
63 if compare_fn is None:
64
65 def compare_fn(x: object, y: object) -> None:
66 assert x == y
67
68 assert compare_fn is not None
69 pytester.makepyfile(test_file=source)
70
71 # First run fails because there's no expected file yet
72 result = pytester.inline_run()
73 result.assertoutcome(failed=1)
74
75 # ensure now that the file was generated and the test passes
76 xx = data_getter()
77 compare_fn(xx, expected_data_1)
78 result = pytester.inline_run()
79 result.assertoutcome(passed=1)
80
81 # changing the regression data makes the test fail (file remains unchanged)
82 data_modifier()
83 result = pytester.inline_run()
84 result.assertoutcome(failed=1)
85 compare_fn(data_getter(), expected_data_1)
86
87 # force regeneration (test fails again)
88 result = pytester.inline_run("--force-regen")
89 result.assertoutcome(failed=1)
90 compare_fn(data_getter(), expected_data_2)
91
92 # test should pass again
93 result = pytester.inline_run()
94 result.assertoutcome(passed=1)