pytest(三十七)--fixture参数化params

前言

参数化是自动化测试里面必须掌握的一个知识点,用过unittest框架的小伙伴都知道使用ddt来实现测试用例的参数化。

pytest测试用例里面对应的参数可以用parametrize实现,随着用例的增多,我们的需求也会越来越多,那么如何在fixture中使用参数呢?

fixture源码

先看下fixture源码,有这几个参数:scope,params,autouse,ids,name

def fixture(
    callable_or_scope=None,
    *args,
    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 a
    fixture function.

    The name of the fixture function can later be referenced to cause its
    invocation ahead of running tests: test
    modules or classes can use the ``pytest.mark.usefixtures(fixturename)``
    marker.

    Test functions can directly use fixture names as input
    arguments in which case the fixture instance returned from the fixture
    function 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 executed
    as 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).

                This parameter may also be a callable which receives ``(fixture_name, config)``
                as parameters, and must return a ``str`` with one of the values mentioned above.

                See :ref:`dynamic scope` in the docs for more information.

    :arg params: an optional list of parameters which will cause multiple
                invocations of the fixture function and all of the tests
                using it.
                The current parameter is available in ``request.param``.

    :arg autouse: if True, the fixture func is activated for all tests that
                can see it.  If False (the default) then an explicit
                reference is needed to activate the fixture.

    :arg ids: list of string ids each corresponding to the params
                so that they are part of the test id. If no ids are provided
                they will be generated automatically from the params.

    :arg name: the name of the fixture. This defaults to the name of the
                decorated function. If a fixture is used in the same module in
                which it is defined, the function name of the fixture will be
                shadowed by the function arg that requests the fixture; one way
                to resolve this is to name the decorated function
                ``fixture_<fixturename>`` and then use
                ``@pytest.fixture(name='<fixturename>')``.
    """

 

重点看params参数:一个可选的参数列表,它将导致多次调用fixture函数和使用它的所有测试,获取当前参数可以使用 request.param  

    :arg params: an optional list of parameters which will cause multiple
                invocations of the fixture function and all of the tests
                using it.
                The current parameter is available in ``request.param``.

fixture值params使用示例

request是pytest的内置fixture,主要用于传递参数

#test_a.py
import pytest
data=["admin","test"]
@pytest.fixture(scope="function",params=data)
def user(request):
    return request.param
def test_register(user):
    print("注册用户:{}".format(user))

if __name__=="__main__":
    pytest.main(["-s","test_a.py"])

 运行结果

 

前置与后置

如果每次注册用户之前,需先在前置操作里面清理用户注册表的数据,可以执行SQL,传不同用户的参数。

#test_a.py
import pytest
def delete_sql(user):
    sql="delete from users where username = {}".format(user)
    print("执行的sql,删除用户:{}".format(user))

data=["admin","test"]
@pytest.fixture(scope="function",params=data)
def user(request):
    #前置操作
    delete_sql(request.param)
    yield request.param
    #后置操作
    print("===成功:{}===".format(request.param))
def test_register(user):
    print("注册用户:{}".format(user))

if __name__=="__main__":
    pytest.main(["-s","test_a.py"])

 运行结果

posted on 2020-08-02 12:30  星空6  阅读(458)  评论(0编辑  收藏  举报

导航