简体   繁体   English

如何减少执行pytest固定装置的时间

[英]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. 我测试的重点是登录Web应用程序,创建表条目,批准它,检查条目状态,将其删除并注销。 So as there are few similar test cases with common pre-conditions/post-conditions I decided to create fixtures ( scope=function ). 因此,由于几乎没有类似的具有常见前置条件/​​后置条件的测试用例,因此我决定创建固定装置( 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? 所以我想知道: pytest固定装置需要这么多时间来执行是正常的,还是有一种减少这种时间的方法?

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. 我认为pytest不会花费很多时间来渲染作用域,除非您有其他服务(例如mysql和真实的API请求),这在您的情况下似乎是这样。

Perhaps you may mock your requests sessions or use the vrc lib. 也许您可以嘲笑您的请求会话或使用vrc库。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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