Skip to content

Commit

Permalink
Merge pull request #16 from joseph-roitman/full-coverage
Browse files Browse the repository at this point in the history
Full coverage
  • Loading branch information
joseph-roitman authored May 10, 2020
2 parents bd9173d + a6b3764 commit dcfb0c7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
4 changes: 0 additions & 4 deletions pytest_snapshot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
return False
if self._created_snapshots or self._updated_snapshots or self._snapshots_to_delete:
message_lines = ['Snapshot directory was modified: {}'.format(shorten_path(self.snapshot_dir))]
if self._created_snapshots:
Expand Down Expand Up @@ -109,8 +107,6 @@ def _snapshot_path(self, snapshot_name):
"""
if isinstance(snapshot_name, Path):
snapshot_path = snapshot_name.absolute()
if self._snapshot_dir is None:
self.snapshot_dir = snapshot_path.parent
else:
snapshot_path = self.snapshot_dir.joinpath(snapshot_name)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def read(fname):
'packaging',
'pathlib2>=2.2.0;python_version<"3.6"', # identical to pytest's setup.py
'pytest>=3.0.0',
'typing',
'typing;python_version<"3.5"',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
43 changes: 43 additions & 0 deletions tests/test_assert_match.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from pytest_snapshot.plugin import text_type
from tests.utils import assert_pytest_passes


Expand Down Expand Up @@ -57,6 +58,20 @@ def test_sth(snapshot):
assert result.ret == 1


def test_assert_match_invalid_type(testdir, basic_case_dir):
testdir.makepyfile("""
def test_sth(snapshot):
snapshot.snapshot_dir = 'case_dir'
snapshot.assert_match(b'incorrect typed obj', 'snapshot1.txt')
""")
result = testdir.runpytest('-v')
result.stdout.fnmatch_lines([
'*::test_sth FAILED*',
'E* TypeError: value must be {}'.format(text_type.__name__),
])
assert result.ret == 1


def test_assert_match_missing_snapshot(testdir, basic_case_dir):
testdir.makepyfile("""
def test_sth(snapshot):
Expand Down Expand Up @@ -134,6 +149,34 @@ def test_sth(snapshot):
assert_pytest_passes(testdir) # assert that snapshot update worked


def test_assert_match_update_existing_snapshot_and_exception_in_test(testdir, basic_case_dir):
"""
Tests that `Snapshot.assert_match` works when updating an existing snapshot and then the test function fails.
In this case, both the snapshot update error and the test function error are printed out.
"""
testdir.makepyfile("""
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path
def test_sth(snapshot):
snapshot.snapshot_dir = 'case_dir'
snapshot.assert_match(u'the NEW value of snapshot1.txt', 'snapshot1.txt')
assert False
""")
result = testdir.runpytest('-v', '--snapshot-update')
result.stdout.fnmatch_lines([
'*::test_sth FAILED*',
'*::test_sth ERROR*',
"E* AssertionError: Snapshot directory was modified: case_dir",
'E* Updated snapshots:',
'E* snapshot1.txt',
'E* assert False',
])
assert result.ret == 1


def test_assert_match_create_new_snapshot(testdir, basic_case_dir):
testdir.makepyfile("""
def test_sth(snapshot):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from pytest_snapshot.plugin import shorten_path
from tests.utils import assert_pytest_passes

try:
from pathlib import Path
except ImportError:
from pathlib2 import Path


def test_help_message(testdir):
result = testdir.runpytest('--help')
Expand Down Expand Up @@ -42,3 +48,12 @@ def test_sth(snapshot, param):
'*::test_sth?b? PASSED*',
])
assert result.ret == 0


def test_shorten_path_in_cwd():
assert shorten_path(Path('a/b').absolute()) == Path('a/b')


def test_shorten_path_outside_cwd():
path_outside_cwd = Path().absolute().parent.joinpath('a/b')
assert shorten_path(path_outside_cwd) == path_outside_cwd
13 changes: 6 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# For more information about tox, see https://tox.readthedocs.io/en/latest/
[tox]
envlist =
py27-pytest{3,4},
py35-pytest{3,4,5,},
py36-pytest{3,4,5,},
py37-pytest{3,4,5,},
py38-pytest{3,4,5},
py38-pytest-coverage,
pypy3-pytest{3,4,5,},
py27-pytest{3,4}-coverage,
py35-pytest{3,4,5,}-coverage,
py36-pytest{3,4,5,}-coverage,
py37-pytest{3,4,5,}-coverage,
py38-pytest{3,4,5,}-coverage,
pypy3-pytest{3,4,5,}-coverage,
flake8

[testenv]
Expand Down

0 comments on commit dcfb0c7

Please sign in to comment.