簡體   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