py.test session level fixtures in setup_method

前端 未结 3 1877
长发绾君心
长发绾君心 2021-02-19 19:05

Is there a way to somehow use pytest fixtures from conftest.py in a test class\'s setup? I need to initialize an object when the session starts and use it in so

3条回答
  •  遇见更好的自我
    2021-02-19 19:20

    I don't think you can do it directly. However, you can decorate the whole class with pytest.mark.usefixtures, if that helps:

    @pytest.mark.usefixtures(['myfixture'])
    class TestAAA(object):
        ...
    

    IIRC, setup_method will be called before any of the automatically applied fixtures.

    You can also utilize autouse for class-level fixtures like so:

    class TestAAA(object):
        @pytest.fixture(autouse=True)
        def init_aaa(self, myfixture):
            ...
    

提交回复
热议问题