Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest fixtures inside or outside class?

What is the difference if the fixture is inside or outside of Class(bla0, bla1)?

@pytest.fixtures()
    def bla0()
    ...

class MyTest:
    @pytest.fixtures()
    def bla1()
        ...

    @pytest.mark.usefixtures("bla0", "bla1")
    def test ...
like image 814
user1456435 Avatar asked Mar 02 '15 13:03

user1456435


People also ask

Where do I put pytest fixtures?

Fixtures and their visibility are a bit odd in pytest. They don't require importing, but if you defined them in a test_*. py file, they'll only be available in that file. You can however put them in a (project- or subfolder-wide) conftest.py to use them in multiple files.

Can a class be a pytest fixture?

Like normal functions, fixtures also have scope and lifetime. The default scope of a pytest fixture is the function scope. Apart from the function scope, the other pytest fixture scopes are – module, class, and session.

How do you use pytest fixtures?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.

What is the point of pytest fixtures?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.


1 Answers

Just a matter of visibility... bla1 can only be used in test methods declared in MyTest.

like image 135
Bruno Oliveira Avatar answered Oct 11 '22 21:10

Bruno Oliveira