简体   繁体   中英

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 ...

只是可见性问题...... bla1只能用于MyTest中声明的测试方法。

The difference is the same as between functions and class methods. The first can be used within the scope of file and the other in the scope of class.

What you are probably looking for is conftest.py file which allows you to define fixtures that are available in all test files within project (ie Pyramid app). It would look like this:

@pytest.fixture(scope='session', autouse=True)
def method():
    return 'foobar'

Now, this fixture can be used within all test classes in your project that are covered by conftest.py file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM