簡體   English   中英

在 setup_method 上使用 pytest.fixture

[英]using pytest.fixture on setup_method

是否可以在 setup_method 上使用 pytest.fixture 以便在每個測試用例之間始終可以完成某些操作? 我嘗試使用如下夾具,結構看起來還可以。 我能夠在 funcA 完成之前執行每個測試用例。 但是,我不想包含 ``` @pytest.mark.usefixtures('funcA')


    class TestSuite(TestBase):
        @classmethod
        def setup_class(cls):
            super(TestSuite, cls).setup_class()
            'do something for setup class'

        @classmethod
        def teardown_class(cls):
            super(TestSuite, cls).teardown_class()
            'do something for teardown class'


        def setup_method(self):
            'do log in'
            'do a,b c for setup_method'

        @pytest.fixture(scope="function")
        def funcA(self):
            print('do function A')

        @pytest.mark.usefixtures('funcA')
        def test_caseA(self):
            'check a'

        @pytest.mark.usefixtures('funcA')
        def test_caseB(self):
            'check b'

        @pytest.mark.usefixtures('funcA')
        def test_caseC(self):
            'check c'

您可以將夾具作為參數傳遞給測試:

def test_caseA(self, funcA):
    'check a'

def test_caseB(self, funcA):
    'check b'

def test_caseC(self, funcA):
    'check c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM