Skip to content

Commit

Permalink
Use a cleaner way of forcing snapshot updates in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidshepherd7 committed Jul 28, 2020
1 parent 3fbe9e1 commit cfb2d10
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
11 changes: 4 additions & 7 deletions snapshottest/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,15 @@ def get_module_for_testpath(cls, test_filepath):
class SnapshotTest(object):
_current_tester = None

def __init__(self):
def __init__(self, should_update_snapshot):
self.curr_snapshot = ''
self.snapshot_counter = 1
self.should_update_snapshot = should_update_snapshot

@property
def module(self):
raise NotImplementedError("module property needs to be implemented")

@property
def update(self):
return False

@property
def test_name(self):
raise NotImplementedError("test_name property needs to be implemented")
Expand Down Expand Up @@ -242,10 +239,10 @@ def assert_value_matches_snapshot(self, test_value, snapshot_value):
def assert_equals(self, value, snapshot):
assert value == snapshot

def assert_match(self, value, name="", force_update_snapshot=False):
def assert_match(self, value, name=""):
self.curr_snapshot = name or self.snapshot_counter
self.visit()
if self.update or force_update_snapshot:
if self.should_update_snapshot:
self.store(value)
else:
try:
Expand Down
6 changes: 1 addition & 5 deletions snapshottest/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ class PyTestSnapshotTest(SnapshotTest):

def __init__(self, request=None):
self.request = request
super(PyTestSnapshotTest, self).__init__()
super(PyTestSnapshotTest, self).__init__(request.config.option.snapshot_update)

@property
def module(self):
return SnapshotModule.get_module_for_testpath(self.request.node.fspath.strpath)

@property
def update(self):
return self.request.config.option.snapshot_update

@property
def test_name(self):
cls_name = getattr(self.request.node.cls, '__name__', '')
Expand Down
7 changes: 1 addition & 6 deletions snapshottest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ def __init__(self, test_class, test_id, test_filepath, should_update, assertEqua
self.test_id = test_id
self.test_filepath = test_filepath
self.assertEqual = assertEqual
self.should_update = should_update
super(UnitTestSnapshotTest, self).__init__()
super(UnitTestSnapshotTest, self).__init__(should_update)

@property
def module(self):
return SnapshotModule.get_module_for_testpath(self.test_filepath)

@property
def update(self):
return self.should_update

def assert_equals(self, value, snapshot):
self.assertEqual(value, snapshot)

Expand Down
20 changes: 12 additions & 8 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,39 @@ def pytest_snapshot_test(request, _apply_options):

class TestPyTestSnapShotTest:
def test_property_test_name(self, pytest_snapshot_test):
pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.should_update_snapshot = True
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'TestPyTestSnapShotTest.test_property_test_name 1'

pytest_snapshot_test.assert_match('named', 'named_test', force_update_snapshot=True)
pytest_snapshot_test.assert_match('named', 'named_test')
assert pytest_snapshot_test.test_name == \
'TestPyTestSnapShotTest.test_property_test_name named_test'

pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'TestPyTestSnapShotTest.test_property_test_name 2'


def test_pytest_snapshottest_property_test_name(pytest_snapshot_test):
pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.should_update_snapshot = True
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name 1'

pytest_snapshot_test.assert_match('named', 'named_test', force_update_snapshot=True)
pytest_snapshot_test.assert_match('named', 'named_test')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name named_test'

pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name 2'


@pytest.mark.parametrize('arg', ['single line string'])
def test_pytest_snapshottest_property_test_name_parametrize_singleline(pytest_snapshot_test, arg):
pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.should_update_snapshot = True
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name_parametrize_singleline[single line string] 1'

Expand All @@ -63,6 +66,7 @@ def test_pytest_snapshottest_property_test_name_parametrize_singleline(pytest_sn
'''
])
def test_pytest_snapshottest_property_test_name_parametrize_multiline(pytest_snapshot_test, arg):
pytest_snapshot_test.assert_match('counter', force_update_snapshot=True)
pytest_snapshot_test.should_update_snapshot = True
pytest_snapshot_test.assert_match('counter')
assert pytest_snapshot_test.test_name == \
'test_pytest_snapshottest_property_test_name_parametrize_multiline[ multi line string ] 1'
10 changes: 6 additions & 4 deletions tests/test_snapshot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, snapshot_module, update=False, current_test_id=None):
'snapshot_module': snapshot_module,
'update': update,
'current_test_id': current_test_id or "test_mocked"}
super(GenericSnapshotTest, self).__init__()
super(GenericSnapshotTest, self).__init__(update)

@property
def module(self):
Expand All @@ -32,7 +32,7 @@ def test_name(self):

def reinitialize(self):
"""Reset internal state, as though starting a new test run"""
super(GenericSnapshotTest, self).__init__()
super(GenericSnapshotTest, self).__init__(False)


def assert_snapshot_test_ran(snapshot_test, test_name=None):
Expand Down Expand Up @@ -87,7 +87,8 @@ def fixture_snapshot_test(tmpdir):
@pytest.mark.parametrize("value", SNAPSHOTABLE_VALUES, ids=repr)
def test_snapshot_matches_itself(snapshot_test, value):
# first run stores the value as the snapshot
snapshot_test.assert_match(value, force_update_snapshot=True)
snapshot_test.should_update_snapshot = True
snapshot_test.assert_match(value)
assert_snapshot_test_succeeded(snapshot_test)

# second run should compare stored snapshot and also succeed
Expand All @@ -104,7 +105,8 @@ def test_snapshot_matches_itself(snapshot_test, value):
])
def test_snapshot_does_not_match_other_values(snapshot_test, value, other_value):
# first run stores the value as the snapshot
snapshot_test.assert_match(value, force_update_snapshot=True)
snapshot_test.should_update_snapshot = True
snapshot_test.assert_match(value)
assert_snapshot_test_succeeded(snapshot_test)

# second run tries to match other_value, should fail
Expand Down

0 comments on commit cfb2d10

Please sign in to comment.