Pytest missing 1 required positional argument with fixture

Question:

I’m using vscode as IDE

I have code a very simple usage of pytest fixture but it doesn’t working when basic example fixture found in the pytest documentation are working well :

@pytest.fixture
def declare_hexidict():
    hd = hexidict()
    rvc = ReferenceValueCluster()
    rv = ReferenceValue(init=3)
    hd_var = (hd, rvc, rv)
    return hd_var

def setitem_getitem(declare_hexidict):
    print('start')
    # hd = hexidict()
    # rvc = ReferenceValueCluster()
    # rv = ReferenceValue(init=3)
    hd, rvc, rv = declare_hexidict
    print('datastruct defined')
    hd[rvc("key1").reflink] = rv[0].reflink
    hd[rvc["key1"]] == rv[0]
    assert rvc["key1"] in hd.keys(), "key :{} is not int this hexidict".format(
        rvc("key1")
    )
    assert hd[rvc["key1"]] == rv[0], "key :{}  return {} instead of {}".format(
        rvc["key1"], hd[rvc["key1"]], rv[0]
    )
    #set non value item (on set une liste)
    hd[rvc("key2").reflink] = [rv[1].reflink]
    hd[rvc["key2"]]
    assert type(hd[rvc["key2"]]) == list

    #on verifie que l'item dans la list est bien celui qui provient de rv
    assert hd[rvc["key2"]][0] in rv

I get in the test summary info :

ERROR test/process/hexidict/test_hd_basic_function.py - TypeError: setitem_getitem() missing 1 required positional argument: 'declare_hexidict'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Asked By: Chrys Bltr

||

Answers:

pytest does not recognize setitem_getitem like test, so you should rename it to test_setitem_getitem and try it out:

def test_setitem_getitem(declare_hexidict):
Answered By: Vova

The problem is that your test is not detected by Pytest’s test discovery.

Depending on how you execute your tests (whether you provide a full path to your test file, provide path with sub directories and multiple test files or want to execute all tests matching a specific mark in the entire project) you will want to make sure all test modules, classes and functions are discovered properly. By default test files need to match test_*.py or *_test.py, classes – Test* and functions – test*.
https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#conventions-for-python-test-discovery

Test discovery can also be configured to match your needs in pytest.ini.

Example pytest.ini:

[pytest]
python_files = *_pytest.py
python_functions = mytest_*
python_classes = *Tests
Answered By: Override12