简体   繁体   中英

How to reduce time to execute pytest fixtures

The point of my test is to login to web-app, create table entry, approve it, check entry status, remove it and logout. So as there are few similar test cases with common pre-conditions/post-conditions I decided to create fixtures ( scope=function ). My code (pretty much simplified as there are dozens of lines) looks like this:

@pytest.fixture
def create_new_entry():
    # code for new entry creation using POST-request with python-requests(pre-condition)

@pytest.fixture
def login():
    # get to URL and complete authorization with Selenium

@pytest.fixture
def tear_down(request)
    def logout_remove():
        # logout from web-app with Selenium. Remove entry from table using POST-request with python-requests (post-condition)
    request.addfinalizer(logout_remove)

def test_1(create_new_entry, login, tear_down):
    # code to approve entry
    assert # whether entry approved or not

Code works, but for some reason test execution takes for few minutes more time than if just to use same code (from fixtures) directly in test:

def test_1():
    # code for new entry creation using POST-request with python-requests(pre-condition)
    # get to URL and complete authorization with Selenium
    # code to approve entry
    assert # whether entry approved or not
    # logout from web-app with Selenium. Remove entry from table using POST-request with python-requests (post-condition)

So I wonder: is it normal that pytest fixtures requires so much time to execute or there is a way to reduce this time?

Well it really depends on how your tests code is running, I mean how many operations and integrations it has. I think that pytest does not expand much time to renderize the scopes unless you have another service such as mysql and real API requests which seems to your case.

Perhaps you may mock your requests sessions or use the vrc lib.

Hope it helps.

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