Pytest fixture参数化params
生活随笔
收集整理的這篇文章主要介紹了
Pytest fixture参数化params
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
unittest使用ddt來實現測試用例參數化、或parameterized實現測試用例參數化,pytest測試用例里面對應的參數可以用 parametrize 實現參數化,今天我們來了解下fixture參數化params
fixture的參數可以解決大量重復代碼工作,比如數據庫的連接、查詢、關閉等.同樣可以使用參數化來測試多條數據用例。
fixture源碼:
傳入參數scope,params,autouse,ids,name
def fixture(scope="function", params=None, autouse=False, ids=None, name=None):"""Decorator to mark a fixture factory function.This decorator can be used, with or without parameters, to define afixture function.The name of the fixture function can later be referenced to cause itsinvocation ahead of running tests: testmodules or classes can use the ``pytest.mark.usefixtures(fixturename)``marker.Test functions can directly use fixture names as inputarguments in which case the fixture instance returned from the fixturefunction will be injected.Fixtures can provide their values to test functions using ``return`` or ``yield``statements. When using ``yield`` the code block after the ``yield`` statement is executedas teardown code regardless of the test outcome, and must yield exactly once.:arg scope: the scope for which this fixture is shared, one of``"function"`` (default), ``"class"``, ``"module"``,``"package"`` or ``"session"``.``"package"`` is considered **experimental** at this time.:arg params: an optional list of parameters which will cause multipleinvocations of the fixture function and all of the testsusing it.The current parameter is available in ``request.param``.:arg autouse: if True, the fixture func is activated for all tests thatcan see it. If False (the default) then an explicitreference is needed to activate the fixture.:arg ids: list of string ids each corresponding to the paramsso that they are part of the test id. If no ids are providedthey will be generated automatically from the params.:arg name: the name of the fixture. This defaults to the name of thedecorated function. If a fixture is used in the same module inwhich it is defined, the function name of the fixture will beshadowed by the function arg that requests the fixture; one wayto resolve this is to name the decorated function``fixture_<fixturename>`` and then use``@pytest.fixture(name='<fixturename>')``."""if callable(scope) and params is None and autouse is False:# direct decorationreturn FixtureFunctionMarker("function", params, autouse, name=name)(scope)if params is not None and not isinstance(params, (list, tuple)):params = list(params)return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)params 參數:一個可選的參數列表,它將導致多次調用fixture函數和使用它的所有測試,獲取當前參數可以使用request.param,request 是pytest的內置 fixture ,主要用于傳遞參數
1、獲取賬號密碼案例:
import pytestdata = [("username1", "password1"), ("username2", "password2")] # data = (("username1", "password1"), ("username2", "password2")) # data = [["username1", "password1"], ["username2", "password2"]]@pytest.fixture(scope = "function", params = data) def get_data(request):print(request.param)return request.paramdef test_login(get_data):print("賬號:%s"%get_data[0],"密碼:%s"%get_data[1])if __name__ == '__main__':pytest.main(["-s",?"test_C_01.py"])test_C_01.py ('username1', 'password1') 賬號:username1 密碼:password1 .('username2', 'password2') 賬號:username2 密碼:password2 .============================== 2 passed in 0.08s ==============================Process finished with exit code 02、前置準備后置清理案例:
import pytest # 封裝刪除用戶sql def delete_user(user):sql = "delete from user where mobile = '%s'"%userprint("刪除用戶sql:%s"%sql) # 測試數據 mobile_data = ["18200000000", "18300000000"]@pytest.fixture(scope="function", params=mobile_data) def users(request):'''注冊用戶參數化'''# 前置操作delete_user(request.param)yield request.param# 后置操作delete_user(request.param)def test_register(users):print("注冊用戶:%s"%users)if __name__ == '__main__':pytest.main(["-s", "test_C_01.py"])test_C_01.py 刪除用戶sql:delete from user where mobile = '18200000000' 注冊用戶:18200000000 .刪除用戶sql:delete from user where mobile = '18200000000' 刪除用戶sql:delete from user where mobile = '18300000000' 注冊用戶:18300000000 .刪除用戶sql:delete from user where mobile = '18300000000'============================== 2 passed in 0.12s ==============================Process finished with exit code 0總結
以上是生活随笔為你收集整理的Pytest fixture参数化params的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 测试人员做到这几点,线上80%的BUG将
- 下一篇: mitmproxy抓包 | Python