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
|
from __future__ import unicode_literals
from textwrap import dedent
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(testdir, monkeypatch):
"""
:type testdir: _pytest.pytester.TmpTestdir
:type monkeypatch: _pytest.monkeypatch.monkeypatch
"""
import sys
import yaml
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 = testdir.tmpdir / "test_file" / "test_1.yml"
assert yaml_filename.check(file=1)
with yaml_filename.open() as f:
return yaml.safe_load(f)
check_regression_fixture_workflow(
testdir,
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(testdir, tmpdir):
"""
Test data_regression with ``fullpath`` parameter.
:type testdir: _pytest.pytester.TmpTestdir
"""
fullpath = tmpdir.join("full/path/to").ensure(dir=1).join("contents.yaml")
assert not fullpath.isfile()
source = """
def test(data_regression):
contents = {'data': [1, 2]}
data_regression.check(contents, fullpath=%s)
""" % (
repr(str(fullpath))
)
testdir.makepyfile(test_foo=source)
# First run fails because there's no yml file yet
result = testdir.inline_run()
result.assertoutcome(failed=1)
# ensure now that the file was generated and the test passes
assert fullpath.isfile()
result = testdir.inline_run()
result.assertoutcome(passed=1)
def test_data_regression_no_aliases(testdir):
"""
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.
:type testdir: _pytest.pytester.TmpTestdir
"""
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)
"""
testdir.makepyfile(test_file=source)
result = testdir.inline_run()
result.assertoutcome(failed=1)
yaml_file_contents = testdir.tmpdir.join("test_file", "test.yml").read()
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 = testdir.inline_run()
result.assertoutcome(passed=1)
def test_not_create_file_on_error(testdir):
"""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)
"""
testdir.makepyfile(test_file=source)
result = testdir.inline_run()
result.assertoutcome(failed=1)
yaml_file = testdir.tmpdir.join("test_file", "test.yml")
assert not yaml_file.isfile()
|