Forums

How to fix a "fixture 'tmp_path' not found" error?

Hello,

Can anyone tell me why I am getting this error message and how I can fix it? The pytest function test_isbn_checker has been provided to me as part of a course I am doing and none of the other students report a problem with it, but then they are probably not be using Python Anywhere. I have looked up the error messages, fixtures, etc but I could not find the solution.

Many thanks

-----------------------------------------------------------------------

I have the following pytest function defined

import pytest

def test_isbn_checker(tmp_path):
    test_directory = tmp_path / 'testfiles'
    test_directory.mkdir()
    filename = test_directory / 'outfile.txt'

    test_isbns = {'': 'bad length of 0',          # empty; invalid
              '12345': 'bad length of 5',
              '123456789012345' : 'bad length of 15',
              '9780143127796': 'True',
              '9780415700108': 'True',
              '9780525533184': 'True',
              '9780143127793': 'False',
              '9780415700103': 'False',
              '9780525533183': 'False' }

    validate_isbns(filename, *test_isbns)

    for one_line in open(filename):
        print(one_line)
        isbn, assessment = one_line.rstrip().split('\t')

        assert assessment == test_isbns[isbn]

When I call it using the command

pytest -vv solution.py

where solution.py is the program I am testing, I get the the following error message:

========================= test session starts =============
platform linux -- Python 3.7.0, pytest-3.7.1, py-1.5.4, pluggy-0.7.1 -- /usr/local/bin/python3.7
cachedir: .pytest_cache
rootdir: /home/moonbrain/WPE_Jan_2019, inifile:
collected 1 item                                                                                                                                                                                                
solution.py::test_isbn_checker ERROR [100%]
========================= ERRORS =========================================
_________________________ ERROR at setup of test_isbn_checker ____________
file /home/moonbrain/WPE_Jan_2019/solution.py, line 61
  def test_isbn_checker(tmp_path):
E       fixture 'tmp_path' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, 
        capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, 
        record_xml_attribute, record_xml_property, recwarn, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

Do you have a file that defines the tmp_path fixture anywhere? It should be in file called conftest.py, or in something that is imported into the module containing the test.

No that was all the code I was given. I have managed to work around it but I would like to find out how to fix this.

I think I found the problem: tmp_path was introduced in version 3.9 of pytest; 3.7.1 is installed on the earltgrey image.