繁体   English   中英

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

[英]How to reduce time to execute pytest fixtures

我测试的重点是登录Web应用程序,创建表条目,批准它,检查条目状态,将其删除并注销。 因此,由于几乎没有类似的具有常见前置条件/​​后置条件的测试用例,因此我决定创建固定装置( scope=function )。 我的代码(由于有几十行而已大大简化了)看起来像这样:

@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

代码可以工作,但是由于某种原因,与仅在测试中直接使用相同的代码(来自固定装置)相比,执行测试要花几分钟的时间:

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)

所以我想知道: pytest固定装置需要这么多时间来执行是正常的,还是有一种减少这种时间的方法?

好吧,这实际上取决于测试代码的运行方式,我的意思是它具有多少个操作和集成。 我认为pytest不会花费很多时间来渲染作用域,除非您有其他服务(例如mysql和真实的API请求),这在您的情况下似乎是这样。

也许您可以嘲笑您的请求会话或使用vrc库。

希望能帮助到你。

暂无
暂无

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

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