How do I correctly setup and teardown for my pytest class with tests?

后端 未结 6 1019
名媛妹妹
名媛妹妹 2020-11-30 21:50

I am using selenium for end to end testing and I can\'t get how to use setup_class and teardown_class methods.

I need to set up browser in <

6条回答
  •  情歌与酒
    2020-11-30 22:25

    According to Fixture finalization / executing teardown code, the current best practice for setup and teardown is to use yield instead of return:

    import pytest
    
    @pytest.fixture()
    def resource():
        print("setup")
        yield "resource"
        print("teardown")
    
    class TestResource:
        def test_that_depends_on_resource(self, resource):
            print("testing {}".format(resource))
    

    Running it results in

    $ py.test --capture=no pytest_yield.py
    === test session starts ===
    platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
    collected 1 items
    
    pytest_yield.py setup
    testing resource
    .teardown
    
    
    === 1 passed in 0.01 seconds ===
    

    Another way to write teardown code is by accepting a request-context object into your fixture function and calling its request.addfinalizer method with a function that performs the teardown one or multiple times:

    import pytest
    
    @pytest.fixture()
    def resource(request):
        print("setup")
    
        def teardown():
            print("teardown")
        request.addfinalizer(teardown)
        
        return "resource"
    
    class TestResource:
        def test_that_depends_on_resource(self, resource):
            print("testing {}".format(resource))
    

提交回复
热议问题