Skip to content

Commit

Permalink
Merge pull request #50 from joseph-roitman/assert-plain
Browse files Browse the repository at this point in the history
Test --assert=plain behaviour and fix related bug.
  • Loading branch information
joseph-roitman authored Feb 11, 2022
2 parents 2d5fb6c + 48457aa commit c1762ed
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pytest_snapshot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def assert_match(self, value: Union[str, bytes], snapshot_name: Union[str, Path]
else:
snapshot_diff_msg = None

if snapshot_diff_msg:
if snapshot_diff_msg is not None:
snapshot_diff_msg = 'value does not match the expected value in snapshot {}\n{}'.format(
shorten_path(snapshot_path), snapshot_diff_msg)
raise AssertionError(snapshot_diff_msg)
Expand Down Expand Up @@ -255,6 +255,6 @@ def _get_default_snapshot_dir(node: _pytest.python.Function) -> Path:
parametrize_name = parametrize_match.group(1)
parametrize_name = get_valid_filename(parametrize_name)
default_snapshot_dir = test_module_dir.join('snapshots', test_module, test_name)
if parametrize_name:
if parametrize_name is not None:
default_snapshot_dir = default_snapshot_dir.join(parametrize_name)
return Path(str(default_snapshot_dir))
32 changes: 32 additions & 0 deletions tests/test_assert_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from pytest_snapshot._utils import simple_version_parse
from pytest_snapshot.plugin import _file_encode
from tests.utils import assert_pytest_passes

Expand Down Expand Up @@ -90,6 +91,37 @@ def test_sth(snapshot):
assert result.ret == 1


@pytest.mark.skipif(simple_version_parse(pytest.__version__) < (5, 0, 0), reason="consecutive flag not supported.")
def test_assert_match_failure_assert_plain(testdir, basic_case_dir):
"""
Testing plugin behavior when users run "pytest --assert=plain" is complicated and fragile since the outer pytest
and inner pytest affect one another. To get around this, we mock "--assert=plain" behavior.
"""
testdir.makepyfile(r"""
from unittest import mock
def _assert_equal_mock(value, snapshot):
raise AssertionError()
def test_sth(snapshot):
snapshot.snapshot_dir = 'case_dir'
with mock.patch('pytest_snapshot.plugin._assert_equal', _assert_equal_mock):
snapshot.assert_match('the INCORRECT value of snapshot1.txt\n', 'snapshot1.txt')
""")

result = testdir.runpytest('-v')
result.stdout.fnmatch_lines([
'*::test_sth FAILED*',
])
# Use consecutive=True to verify that the mock was not accidentally broken.
result.stdout.fnmatch_lines([
">* raise AssertionError(snapshot_diff_msg)",
'E* AssertionError: value does not match the expected value in snapshot case_dir?snapshot1.txt',
'',
'*plugin.py:*: AssertionError',
], consecutive=True)
assert result.ret == 1


def test_assert_match_invalid_type(testdir, basic_case_dir):
testdir.makepyfile(r"""
def test_sth(snapshot):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# For more information about tox, see https://tox.readthedocs.io/en/latest/
[tox]
envlist =
# Pytest <6 doesn't work on Python >=3.10
# Pytest <6.2.5 not supported on Python >=3.10
py{36,37,38,39}-pytest{3,4,5}-coverage
py{35,36,37,38,39,310,3}-pytest{6,}-coverage
# Coverage is slow in pypy
Expand Down

0 comments on commit c1762ed

Please sign in to comment.