From 9d2f8a8d9acba829779b188224d0530486b1d333 Mon Sep 17 00:00:00 2001 From: Tamir Bahar Date: Tue, 17 Dec 2019 12:27:44 +0200 Subject: [PATCH 1/3] Add a Pytest namer. This commit adds support for parameterized tests in Pytest. Closes #55 --- approvaltests/approvals.py | 6 +++- approvaltests/core/pytest_namer.py | 24 +++++++++++++++ tests/pytest/test_namer.py | 30 +++++++++++++++++-- ...amer.test_pytest_namer[Hello].approved.txt | 1 + ...amer.test_pytest_namer[World].approved.txt | 1 + 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 approvaltests/core/pytest_namer.py create mode 100644 tests/pytest/test_namer.test_pytest_namer[Hello].approved.txt create mode 100644 tests/pytest/test_namer.test_pytest_namer[World].approved.txt 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..4447e2d9 --- /dev/null +++ b/approvaltests/core/pytest_namer.py @@ -0,0 +1,24 @@ + +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 + self.parameters = pytest_request.node.name[len(pytest_request.node.originalname):] + + 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..e5411e8e 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,26 @@ 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 + + +@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 From 39be84e0280760d6c96062f96a99ad447e8c7f57 Mon Sep 17 00:00:00 2001 From: Tamir Bahar Date: Tue, 17 Dec 2019 15:10:34 +0200 Subject: [PATCH 2/3] Add support for non-parameterized tests. --- approvaltests/core/pytest_namer.py | 5 ++++- tests/pytest/test_namer.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/approvaltests/core/pytest_namer.py b/approvaltests/core/pytest_namer.py index 4447e2d9..900db231 100644 --- a/approvaltests/core/pytest_namer.py +++ b/approvaltests/core/pytest_namer.py @@ -7,7 +7,10 @@ class PytestNamer: """ def __init__(self, base_namer, pytest_request): self.base_namer = base_namer - self.parameters = pytest_request.node.name[len(pytest_request.node.originalname):] + 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() diff --git a/tests/pytest/test_namer.py b/tests/pytest/test_namer.py index e5411e8e..46b5359f 100644 --- a/tests/pytest/test_namer.py +++ b/tests/pytest/test_namer.py @@ -23,6 +23,10 @@ def pytest_verify(data, reporter=None): 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) From 75a15b6856833da98a04530176b99b2255383c85 Mon Sep 17 00:00:00 2001 From: Tamir Bahar Date: Tue, 17 Dec 2019 15:18:36 +0200 Subject: [PATCH 3/3] Forgot to add the approved file --- tests/pytest/test_namer.test_pytest_namer_sanity.approved.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/pytest/test_namer.test_pytest_namer_sanity.approved.txt 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