How do I pass parameters to a fixture function which already takes monkeypatch
?
#13161
-
Hi. I've got the following piece of code which successfully mock the current date so that import pytest
import datetime
FAKE_DATE = datetime.datetime(1999, 12, 31)
@pytest.fixture
def patch_datetime(monkeypatch, fake_date=FAKE_DATE):
# subclass date and override today
class MyDate(datetime.date):
@classmethod
def today(cls):
return fake_date.date()
# replace datetime.date with MyDate
monkeypatch.setattr(datetime, "date", MyDate)
def test_date(patch_datetime):
print(datetime.date.today()) However, my actual goal is to parametrize FAKE_DATES = [
datetime.datetime(1999, 12, 31),
datetime.datetime(2000, 12, 31)
]
@pytest.mark.parametrize("patch_datetime", FAKE_DATES, indirect=True)
def test_date(patch_datetime) -> None:
print(datetime.date.today()) If anyone could point me in the right direction, I'd really appreciate it. This is the output I get when I run $ pytest tests.py -s
======================================================================================================== test session starts ========================================================================================================
platform linux -- Python 3.13.1, pytest-8.3.3, pluggy-1.5.0
rootdir: /workspaces/project01
configfile: pyproject.toml
collected 2 items
tests_2.py EE
============================================================================================================== ERRORS ===============================================================================================================
___________________________________________________________________________________________ ERROR at setup of test_date[patch_datetime0] ____________________________________________________________________________________________
file /workspaces/project01/05_next_date/tests.py, line 22
@pytest.mark.parametrize("patch_datetime", FAKE_DATES, indirect=True)
def test_date(patch_datetime):
file /workspaces/project01/05_next_date/tests.py, line 11
@pytest.fixture
def patch_datetime(monkeypatch, fake_date):
E fixture 'fake_date' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, patch_datetime, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/workspaces/project01/05_next_date/tests.py:11
___________________________________________________________________________________________ ERROR at setup of test_date[patch_datetime1] ____________________________________________________________________________________________
file /workspaces/project01/05_next_date/tests.py, line 22
@pytest.mark.parametrize("patch_datetime", FAKE_DATES, indirect=True)
def test_date(patch_datetime):
file /workspaces/python_morsels/05_next_date/tests_2.py, line 11
@pytest.fixture
def patch_datetime(monkeypatch, fake_date):
E fixture 'fake_date' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, patch_datetime, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/workspaces/project01/05_next_date/tests.py:11
====================================================================================================== short test summary info ======================================================================================================
ERROR tests.py::test_date[patch_datetime0]
ERROR tests.py::test_date[patch_datetime1]
========================================================================================================= 2 errors in 0.01s ========================================================================================================= |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Remove the |
Beta Was this translation helpful? Give feedback.
Remove the
fake_date
argument and instead use therequest
fixture and itsrequest.param
, like in the example you linked to.