[Tutor] problem with pytest

alexkleider alexkleider at protonmail.com
Sun Dec 27 21:50:47 EST 2020


I appreciate that pytest is not part of the standard library but since it appears to be so widely used...
was hoping for some help anyway.

Here's my directory structure:
main project directory: proj
under it are __init__.py
helpers.py
Tests/
helpers_test.py
__init__.py
Using Debian/GNU/Linux, python3.7 with PYTHONPATH set to my project directory.
Content of helpers.py:
#!/usr/bin/env python3
# File: helpers.py
import datetime
date_template = "%b %d, %Y"
def get_datestamp(date=None):
"""
Returns a string (for postal letters,) in the format 'Jul 03, 1945'.
If <date> is provided it must be type datetime.datetime or
datetime.date. If not provided, today's date is used.
"""
if date:
if (isinstance(date,datetime.date)
or isinstance(date, datetime.datetime)):
d = date
else:
print("helpers.get_datestamp got a bad argument")
sys.exit()
else:
d = datetime.date.today()
return d.strftime(date_template)

if __name__ == '__main__':
assert (get_datestamp() ==
datetime.date.today().strftime(date_template)
)
assert (get_datestamp(datetime.date(1945, 7, 3)) ==
"Jul 03, 1945")
print("Assertions passed.")

Content of Tests/helpers_test.py:
#!/usr/bin/env python3
# File: Tests/helpers_test.py
import pytest
import helpers
import datetime
@pytest.mark.parametrize("date_obj, expected", [
(datetime.date(1945, 7, 3), 'Jul 03, 1945'),
(datetime.date(1943, 7, 15), 'Jul 15, 1943'),
])
def test_get_datestamp_w_valid_datetime_params():
assert helpers.get_datestamp(date_obj) == expected

$ ./helpers.py # behaves as I'd expect
$ pytest # complains that ...
"In test_get_datestamp_w_valid_datetime_params: function uses no argument 'date_obj' "

Above is the bare minimum to illustrate the problem.
In my code I have other instances of the @pytest.mark.parametrize() decorator that work just fine.
There must be something simple here that I'm just not seeing.
Thanks in advance.
Cheers,
Alex Kleider


More information about the Tutor mailing list