-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
42 lines (32 loc) · 1.17 KB
/
conftest.py
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
import json
from pathlib import Path
import pytest
@pytest.fixture
def testdata():
base_path = Path(__file__).parent / "tests" / "data"
return base_path.joinpath
@pytest.fixture
def test_snapshots(gen_snapshots):
def inner(output, output_path: Path):
if gen_snapshots:
with output_path.open("w+", encoding="utf-8") as f:
json.dump(
output, f, indent=4, sort_keys=True, ensure_ascii=False
)
else:
with output_path.open("r", encoding="utf-8") as f:
expected = json.load(f)
assert output == expected
return inner
def pytest_addoption(parser):
parser.addoption(
"--gen-snapshots",
action="store_true",
help="recreates outputs instead of testing",
)
def pytest_generate_tests(metafunc):
# This is called for every test. Only get/set command line arguments
# if the argument is specified in the list of test "fixturenames".
option_value = metafunc.config.option.gen_snapshots
if "gen_snapshots" in metafunc.fixturenames and option_value is not None:
metafunc.parametrize("gen_snapshots", [option_value])