12

I want to be able to return a value from a fixture to multiple tests/test classes, but the value that gets passed is a function.

Here's my code:

import pytest

@pytest.fixture()
def user_setup():
    user = {
        'name': 'chad',
        'id': 1
    }
    return user

@pytest.mark.usefixtures('user_setup')
class TestThings:
    def test_user(self):
        assert user_setup['name'] == 'chad'

The output is:

=================================== FAILURES ===================================
_____________________________ TestThings.test_user _____________________________

self = <tests.test_again.TestThings instance at 0x10aed6998>

    def test_user(self):
>       assert user_setup['name'] == 'chad'
E       TypeError: 'function' object has no attribute '__getitem__'

tests/test_again.py:14: TypeError
=========================== 1 failed in 0.02 seconds ===========================

But if I rewrite my test so that it doesn't use the usefixtures decorator, it works as expected:

def test_user(user_setup):
    assert user_setup['name'] == 'chad'

Any ideas why it's not working when I try to use the decorator method?

2 Answers 2

20

When you use the @pytest.mark.usefixtures marker you still need to provide a similarly named input argument if you want that fixture to be injected in to your test function.

As described in the py.test docs for fixtures:

The name of the fixture function can later be referenced to cause its invocation ahead of running tests... Test functions can directly use fixture names as input arguments in which case the fixture instance returned from the fixture function will be injected.

So just using the @pytest.mark.usefixtures decorator will only invoke the function. Providing an input argument will give you the result of that function.

You only really need to use @pytest.mark.usefixtures when you want to invoke a fixture but don't want to have it as an input argument to your test. As described in the py.test docs.

The reason you are getting an exception that talks about user_setup being a function is because inside your test_user function the name user_setup actually refers to the function you defined earlier in the file. To get your code to work as you expect you would need to add an argument to the test_user function:

@pytest.mark.usefixtures('user_setup')
class TestThings:
    def test_user(self, user_setup):
        assert user_setup['name'] == 'chad'

Now from the perspective of the test_user function the name user_setup will refer to the function argument which will be the returned value of the fixture as injected by py.test.

But really you just don't need to use the @pytest.mark.usefixtures decorator at all.

5
  • 2
    Thanks for the clear explanation on how it works. I was thinking that I could accomplish this without having to add the argument to each test functions that are within my test class.
    – Cass
    Aug 29, 2014 at 1:26
  • I was just searching for the difference and/or use cases for these two variants and when to use which, thanks for this explanation. Sep 8, 2015 at 18:25
  • According to the official docs, you shouldn’t have to add user_setup to each test function when they’re grouped under a class decorated with @pytest.mark.usefixtures: “Due to the usefixtures marker, the cleandir fixture will be required for the execution of each test method, just as if you specified a “cleandir” function argument to each of them.” (Source: pytest.org/latest/…) After all, the whole point of grouping tests like this is to manipulate them as a batch.) …Perhaps this is a pytest bug?
    – Zearin
    Feb 8, 2016 at 19:48
  • I have just faced this too, in pytest 3.6.2
    – George ZP
    Jul 19, 2018 at 16:53
  • Came here with the same confusion, but it's worth pointing out that the example in the documentation is not one where the function returns an object that should be used in the test. In the example, the function is "cleandir" which creates and changes into a new dir, then yields, then removes the temp dir.
    – sixty4bit
    Nov 19, 2020 at 17:43
-3

In both cases, in the global scope user_setup refers to the function. The difference is, in your nonfixture version, you are creating a parameter with the same name, which is a classic recipe for confusion.

In that nonfixture version, within in the scope of test_user, your user_setup identifier refers to whatever it is you are passing it, NOT the function in the global scope.

I think you probably mean to be calling user_setup and subscripting the result like

assert user_setup()['name'] == 'chad'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.