Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion approvaltests/approvals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
return ScenarioNamer(get_default_namer(), scenario_name)

def get_pytest_namer(pytest_request):
return PytestNamer(get_default_namer(), pytest_request)
24 changes: 24 additions & 0 deletions approvaltests/core/pytest_namer.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 28 additions & 2 deletions tests/pytest/test_namer.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure where to place this fixture, so I placed it here for now. It should probably move before the code is merged.

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!')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!