diff --git a/approvaltests/approvals.py b/approvaltests/approvals.py index 317ab525..26c480a4 100644 --- a/approvaltests/approvals.py +++ b/approvaltests/approvals.py @@ -5,6 +5,7 @@ from approvaltests.approval_exception import ApprovalException from approvaltests.core.namer import Namer from approvaltests.core.scenario_namer import ScenarioNamer +from approvaltests.core.pytest_namer import PytestNamer from approvaltests.file_approver import FileApprover from approvaltests.list_utils import format_list from approvaltests.reporters.diff_reporter import DiffReporter @@ -73,4 +74,7 @@ def verify_all(header, alist, formatter=None, reporter=None): def get_scenario_namer(scenario_name): - return ScenarioNamer(get_default_namer(), scenario_name) \ No newline at end of file + return ScenarioNamer(get_default_namer(), scenario_name) + +def get_pytest_namer(pytest_request): + return PytestNamer(get_default_namer(), pytest_request) \ No newline at end of file diff --git a/approvaltests/core/pytest_namer.py b/approvaltests/core/pytest_namer.py new file mode 100644 index 00000000..900db231 --- /dev/null +++ b/approvaltests/core/pytest_namer.py @@ -0,0 +1,27 @@ + +class PytestNamer: + """ + For use with parameterized pytest tests. + + Use this namer when the same test case needs to verify more than one value, and produce more than one file. + """ + def __init__(self, base_namer, pytest_request): + self.base_namer = base_namer + if pytest_request.node.originalname: + self.parameters = pytest_request.node.name[len(pytest_request.node.originalname):] + else: + self.parameters = '' + + def get_basename(self): + basename = self.base_namer.get_basename() + return basename + self.parameters + + def get_approved_filename(self, basename=None): + if basename is None: + basename = self.get_basename() + return self.base_namer.get_approved_filename(basename) + + def get_received_filename(self, basename=None): + if basename is None: + basename = self.get_basename() + return self.base_namer.get_received_filename(basename) diff --git a/tests/pytest/test_namer.py b/tests/pytest/test_namer.py index 66136711..46b5359f 100644 --- a/tests/pytest/test_namer.py +++ b/tests/pytest/test_namer.py @@ -1,4 +1,8 @@ -from approvaltests.approvals import get_default_namer, verify +import os + +import pytest + +from approvaltests.approvals import get_default_namer, verify, get_pytest_namer def test_basic_approval(): @@ -7,4 +11,30 @@ def test_basic_approval(): def test_received_filename(): namer = get_default_namer() - assert namer.get_received_filename().endswith("ApprovalTests.Python/tests/pytest/test_namer.test_received_filename.received.txt") + assert namer.get_received_filename().endswith( + os.path.normpath("ApprovalTests.Python/tests/pytest/test_namer.test_received_filename.received.txt")) + + +@pytest.fixture +def pytest_verify(request): + def pytest_verify(data, reporter=None): + verify(data, reporter=reporter, namer=get_pytest_namer(request)) + + return pytest_verify + + +def test_pytest_namer_sanity(pytest_verify): + pytest_verify('Sanity') + + +@pytest.mark.parametrize('arg', ['Hello, World!']) +def test_pytest_received_filename(arg, request): + namer = get_pytest_namer(request) + assert namer.get_received_filename().endswith( + os.path.normpath( + "ApprovalTests.Python/tests/pytest/test_namer.test_pytest_received_filename[Hello, World!].received.txt")) + + +@pytest.mark.parametrize('arg', ['Hello', 'World']) +def test_pytest_namer(arg, pytest_verify): + pytest_verify('Hello, World!') diff --git a/tests/pytest/test_namer.test_pytest_namer[Hello].approved.txt b/tests/pytest/test_namer.test_pytest_namer[Hello].approved.txt new file mode 100644 index 00000000..b45ef6fe --- /dev/null +++ b/tests/pytest/test_namer.test_pytest_namer[Hello].approved.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/tests/pytest/test_namer.test_pytest_namer[World].approved.txt b/tests/pytest/test_namer.test_pytest_namer[World].approved.txt new file mode 100644 index 00000000..b45ef6fe --- /dev/null +++ b/tests/pytest/test_namer.test_pytest_namer[World].approved.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/tests/pytest/test_namer.test_pytest_namer_sanity.approved.txt b/tests/pytest/test_namer.test_pytest_namer_sanity.approved.txt new file mode 100644 index 00000000..a04a4e34 --- /dev/null +++ b/tests/pytest/test_namer.test_pytest_namer_sanity.approved.txt @@ -0,0 +1 @@ +Sanity \ No newline at end of file