def test_file_regression_workflow(testdir, monkeypatch): """ :type testdir: _pytest.pytester.TmpTestdir :type monkeypatch: _pytest.monkeypatch.monkeypatch """ import sys monkeypatch.setattr(sys, "get_data", lambda: "foo", raising=False) source = """ import sys def test_1(file_regression): contents = sys.get_data() file_regression.check(contents, extension='.test') """ def get_file_contents(): fn = Path(str(testdir.tmpdir)) / "test_file" / "test_1.test" assert fn.is_file() return fn.read_text() check_regression_fixture_workflow( testdir, source, data_getter=get_file_contents, data_modifier=lambda: monkeypatch.setattr( sys, "get_data", lambda: "foobar", raising=False), expected_data_1="foo", expected_data_2="foobar", )
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_usage_workflow(testdir, monkeypatch): """ :type testdir: _pytest.pytester.TmpTestdir :type monkeypatch: _pytest.monkeypatch.monkeypatch """ # stdlib import sys monkeypatch.setattr(sys, "testing_get_data", lambda: {"data": 1.1 * np.ones(50)}, raising=False) source = """ import sys def test_1(num_regression): contents = sys.testing_get_data() num_regression.check(contents) """ def get_csv_contents(): filename = testdir.tmpdir / "test_file" / "test_1.csv" frame = pd.read_csv(str(filename)) return {"data": frame["data"].values} def compare_arrays(obtained, expected): assert (obtained["data"] == expected["data"]).all() check_regression_fixture_workflow( testdir, source=source, data_getter=get_csv_contents, data_modifier=lambda: monkeypatch.setattr(sys, "testing_get_data", lambda: {"data": 1.2 * np.ones(50)}, raising=False), expected_data_1={"data": 1.1 * np.ones(50)}, expected_data_2={"data": 1.2 * np.ones(50)}, compare_fn=compare_arrays, )
def test_image_regression_workflow(testdir, monkeypatch, datadir): """ :type testdir: _pytest.pytester.TmpTestdir :type monkeypatch: _pytest.monkeypatch.monkeypatch """ # stdlib import sys # 3rd party from PIL import Image # type: ignore def get_image(color): f = io.BytesIO() img = Image.new("RGB", (100, 100), color) img.save(f, "PNG") return f.getvalue() monkeypatch.setattr(sys, "get_image", partial(get_image, "white"), raising=False) source = """ import sys def test_1(image_regression): contents = sys.get_image() image_regression.check(contents) """ def get_file_contents(): fn = Path(str(testdir.tmpdir)) / "test_file" / "test_1.png" assert fn.is_file() return fn.read_bytes() check_regression_fixture_workflow( testdir, source, data_getter=get_file_contents, data_modifier=lambda: monkeypatch. setattr(sys, "get_image", partial(get_image, "black"), raising=False), expected_data_1=partial(get_image, "white"), expected_data_2=partial(get_image, "black"), )