Versions :
pytest version: 4.5.0
OS version : 4.13.0-43-generic #48~16.04.1-Ubuntu
Hello, there! I just started to use pytest, and wonder how it is amazingly useful to test python code. The below is my sample code, and ERROR message. I don't know why this simple code get this type of ERROR message. Is there anyone who can explain fixture to me? Or rather refer the material to read?
Sample code:
def test_leap_year(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return 1;
else:
return 0;
else:
return 1;
else:
return 0;
def test_run():
assert test_leap_year(2000) == 1
assert test_leap_year(2001) == 1
assert test_leap_year(2002) == 1
ERROR at setup of test_leap_year
---------------------------------------------------------------------------------------------------------
file /home/learn/Development/Code/2019-1-OSS-L1/test_jy/test_4_leap_year_function.py, line 1
def test_leap_year(year):
E fixture 'year' not found
> available fixtures: LineMatcher, _config_for_test, _pytest, _sys_snapshot, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, linecomp, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, testdir, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/home/learn/Development/Code/2019-1-OSS-L1/test_jy/test_4_leap_year_function.py:1
In pytest parameters to test functions are usually fixtures. You declared test_leap_year(year) so pytest is expecting year to be a function declared somewhere.
pytest will run functions with the test prefix as test functions, but it seems here that you did not intend for test_leap_year to be a test function.
The simplest solution is to rename test_leap_year to something else similar, like check_leap_year; this way pytest won't try to run it as a test.
Also, this is a perfect example where you can use parametrization:
import pytest
def check_leap_year(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return 1;
else:
return 0;
else:
return 1;
else:
return 0;
@pytest.mark.parametrize('year', [2000, 2001, 2002])
def test_run(year):
assert check_leap_year(year) == 1
I'm closing this for now, but feel free to follow up with further questions. 馃憤
@nicoddemus Awesome!! Very clear explanation! I appreciate your answer.
It definitely help me to understand better about pytest :). In addition, Thanks for editing my ugly markdown post. That was your hidden care in this issue page. You were my first commentator in my first open source project (not just personal log repository). Sure, I would be back with more questions to explore pytest!! :)
Sure thing, glad I could help you! 馃榿
@nicoddemus Hello, I tested the parametrization code which you posted.
As I realized now, I successfully tested it with 'import pytest'.
I tried to edit your code in the answer, but could not do that. Can you add 'import pytest' in the 1st line?
Can you add 'import pytest' in the 1st line?
Sure, done, thanks for the pointer.