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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
import sys
from textwrap import dedent
import yaml
from pytest_regressions.testing import check_regression_fixture_workflow
def test_example(data_regression):
"""Basic example"""
contents = {"contents": "Foo", "value": 11}
data_regression.check(contents)
def test_basename(data_regression):
"""Basic example using basename parameter"""
contents = {"contents": "Foo", "value": 11}
data_regression.check(contents, basename="case.normal")
def test_custom_object(data_regression):
"""Basic example where we register a custom conversion to dump objects"""
class Scalar:
def __init__(self, value, unit):
self.value = value
self.unit = unit
def dump_scalar(dumper, scalar):
return dumper.represent_dict(dict(value=scalar.value, unit=scalar.unit))
from pytest_regressions import add_custom_yaml_representer
add_custom_yaml_representer(Scalar, dump_scalar)
contents = {"scalar": Scalar(10, "m")}
data_regression.check(contents)
def test_usage_workflow(pytester, monkeypatch):
monkeypatch.setattr(
sys, "testing_get_data", lambda: {"contents": "Foo", "value": 10}, raising=False
)
source = """
import sys
def test_1(data_regression):
contents = sys.testing_get_data()
data_regression.check(contents)
"""
def get_yaml_contents():
yaml_filename = pytester.path / "test_file" / "test_1.yml"
assert yaml_filename.is_file()
with yaml_filename.open() as f:
return yaml.safe_load(f)
check_regression_fixture_workflow(
pytester,
source=source,
data_getter=get_yaml_contents,
data_modifier=lambda: monkeypatch.setattr(
sys,
"testing_get_data",
lambda: {"contents": "Bar", "value": 20},
raising=False,
),
expected_data_1={"contents": "Foo", "value": 10},
expected_data_2={"contents": "Bar", "value": 20},
)
def test_data_regression_full_path(pytester, tmp_path):
"""
Test data_regression with ``fullpath`` parameter.
"""
fullpath = tmp_path.joinpath("full/path/to/contents.yaml")
fullpath.parent.mkdir(parents=True)
assert not fullpath.is_file()
source = """
def test(data_regression):
contents = {'data': [1, 2]}
data_regression.check(contents, fullpath=%s)
""" % (
repr(str(fullpath))
)
pytester.makepyfile(test_foo=source)
# First run fails because there's no yml file yet
result = pytester.inline_run()
result.assertoutcome(failed=1)
# ensure now that the file was generated and the test passes
assert fullpath.is_file()
result = pytester.inline_run()
result.assertoutcome(passed=1)
def test_data_regression_no_aliases(pytester):
"""
YAML standard supports aliases as can be seen here:
http://pyyaml.org/wiki/PyYAMLDocumentation#Aliases.
Even though this is a resourceful feature, data regression intends to be as human readable as
possible and it was deemed that YAML aliases make it harder for developers to understand
contents.
This test makes sure data regression never uses aliases when dumping expected file to YAML.
"""
source = """
def test(data_regression):
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
contents = {
'color1': red,
'color2': green,
'color3': blue,
'color4': red,
'color5': green,
'color6': blue,
}
data_regression.Check(contents)
"""
pytester.makepyfile(test_file=source)
result = pytester.inline_run()
result.assertoutcome(failed=1)
yaml_file_contents = pytester.path.joinpath("test_file/test.yml").read_text()
assert yaml_file_contents == dedent(
"""\
color1:
- 255
- 0
- 0
color2:
- 0
- 255
- 0
color3:
- 0
- 0
- 255
color4:
- 255
- 0
- 0
color5:
- 0
- 255
- 0
color6:
- 0
- 0
- 255
"""
)
result = pytester.inline_run()
result.assertoutcome(passed=1)
def test_not_create_file_on_error(pytester):
"""Basic example where we serializing the object should throw an error and should not create the file"""
source = """
def test(data_regression):
class Scalar:
def __init__(self, value, unit):
self.value = value
self.unit = unit
contents = {"scalar": Scalar(10, "m")}
data_regression.Check(contents)
"""
pytester.makepyfile(test_file=source)
result = pytester.inline_run()
result.assertoutcome(failed=1)
yaml_file = pytester.path.joinpath("test_file/test.yml")
assert not yaml_file.is_file()
def test_regen_all(pytester, tmp_path):
source = """
def test_1(data_regression):
contents = {"contents": "Foo", "value": 11}
data_regression.check(contents, basename="test_1_a")
contents = {"contents": "Bar", "value": 12}
data_regression.check(contents, basename="test_1_b")
def test_2(data_regression):
contents = {"contents": "Baz", "value": 33}
data_regression.check(contents, basename="test_2_a")
contents = {"contents": "BazFoo", "value": 34}
data_regression.check(contents, basename="test_2_b")
"""
pytester.makepyfile(source)
result = pytester.runpytest("--regen-all")
result.stdout.fnmatch_lines("* 2 passed *")
data_dir = pytester.path.joinpath("test_regen_all")
assert {x.name for x in data_dir.iterdir()} == {
"test_1_a.yml",
"test_1_b.yml",
"test_2_a.yml",
"test_2_b.yml",
}
result = pytester.runpytest("--regen-all")
result.stdout.fnmatch_lines("* 2 passed *")
data_dir = pytester.path.joinpath("test_regen_all")
assert {x.name for x in data_dir.iterdir()} == {
"test_1_a.yml",
"test_1_b.yml",
"test_2_a.yml",
"test_2_b.yml",
}
result = pytester.runpytest()
result.stdout.fnmatch_lines("* 2 passed *")
data_dir = pytester.path.joinpath("test_regen_all")
assert {x.name for x in data_dir.iterdir()} == {
"test_1_a.yml",
"test_1_b.yml",
"test_2_a.yml",
"test_2_b.yml",
}
|