Use pytest’s tmpdir fixture in a class

Asked

Viewed 22 times

0

Hello, I have a question that is slowing me down in the use of TDD in my study projects. I’m trying to create a temporary directory with a json file using fixture tmpdir, but find the following error when executing:

request = <Response [200]>

    @pytest.fixture(scope="session")
    def tmpdir_factory(request):
        """Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
        """
>       return request.config._tmpdirhandler
E       AttributeError: 'Response' object has no attribute 'config'

.venv/lib/python3.7/site-packages/_pytest/tmpdir.py:152: AttributeError

I own the following fixture in my file conftest.py:

@pytest.fixture(scope='session')
def json_file(tmpdir_factory, request):
    fp = tmpdir_factory.mktemp('data').join('answer.json')
    create_json(fp, request.txt)
    return fp

The request is another fixture I created to download the contents of an API, and in my file test_case.py this way:

class TestCase:
    def test_create_file(self, json_file):
        print(json_file)
        assert 0

The assert 0 is just there to generate the error and show me the result of the directory created as in the examples of pytest. All my other tests and fixtures run smoothly, just this to create a temporary directory using the tmpdir_factory, the same happens if only use tmpdir within the class.

1 answer

0

Solved, the problem lies in the fixture request, it is used by the scope session directly. To solve just change the name of the fixture request for request_json. Another point is that the others fixture should be in scope session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.