0

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
Jimmy
  • 69
  • 1
  • 10

1 Answers1

0

I don't know about pytest, in unittest you create a test class, which would have method belonging to this class and have self as parameter. However, your error seems to be causes by the function in calgo.py, which does not belong to a class, thus does not have the self reference. Try the following:


import calgo 
def test_func():
    mc = MyClass()
    assert mc.func(3) == 4

marcel h
  • 742
  • 1
  • 7
  • 20
  • The problem is not related nor to pytest neither to unittest. That is only python syntax – azro Sep 27 '21 at 17:51