I want to test a class method with py.test
and I've followed this post Testing class methods with pytest.
I've got a file with my method (calgo.py
) and another file with my test (test_calgo.py
), and both files are in the same folder (learning_ut
).
When I run pytest I get an error message saying fixture 'self' not found
.
Where am I going wrong?
Thanks!
# calgo.py
class MyClass():
def func(self, x):
return x+1
# test_calgo.py
import calgo
def test_func(self):
mc = MyClass()
assert mc.func(3) == 4
# Command line
(pyenv) C:\Users\Jimmy\Desktop\learning_ut>pytest
# Error message
(pyenv) C:\Users\Jimmy\Desktop\learning_ut>pytest
================================================= test session starts =================================================
platform win32 -- Python 3.6.13, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\Jimmy\Desktop\learning_ut
collected 1 item
test_calgo.py E [100%]
======================================================= ERRORS ========================================================
_____________________________________________ ERROR at setup of test_func _____________________________________________
file C:\Users\Jimmy\Desktop\learning_ut\test_calgo.py, line 11
def test_func(self):
E fixture 'self' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary,
doctest_namespace, monkeypatch, 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.
C:\Users\Jimmy\Desktop\learning_ut\test_calgo.py:11
=============================================== short test summary info ===============================================
ERROR test_calgo.py::test_func