pytest学习笔记二 fixtrue

前言

官方文档关于fixture功能的解释如下:

The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions:

  • fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects.
  • fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures.
  • fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across function, class, module or whole test session scopes.

fixtrue特点

  1. 和setup和teardown一样的功能,但相对更灵活一些
  2. 通过conftest.py文件可以实现数据共享,方便调用

使用fixtrue

通过fixtrue源码,可以看到fixtrue的几个参数作用如下:

def fixture(callable_or_scope=None, *args, scope="function", params=None, autouse=False, ids=None, name=None)
: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.
scope: scope 有四个级别参数 "function", "class", "module" or "session". 默认情况下为function
: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功能和所有测试使用它

: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. 如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活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.

每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从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>')``. fixture的名称。 默认装饰函数的名称。 如果fixture在定义它的同一模块中使用, 功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名 """

  


 

实际使用

在举例过程中可以使用 pytest --setup-show 文件名.py 执行测试用例,可以查看用例的执行细节

第一个例子:scope为默认值

import pytest


@pytest.fixture(scope="class")
def login():
   print("登录")



def test_1(login):
   print("登录后1")

def test_2(login):
   print("登录后2")

test_fix2.py 
SETUP C login
test_fix2.py::test_1 (fixtures used: login).
TEARDOWN C login
SETUP C login
test_fix2.py::test_2 (fixtures used: login).
TEARDOWN C login

  从执行结果看到,当fixtrue参数为空,默认为function时,两条测试用例各执行了一次login函数

  

 第二个例子:scope=class

@pytest.fixture(scope="class")
def login():
	print("登录")


class TestCase():
	def test_1(self, login):
		print("登录后1")

	def test_2(self, login):
		print("登录后2")


执行结果:
test_fix2.py 
SETUP C login
test_fix2.py::TestCase::test_1 (fixtures used: login).
test_fix2.py::TestCase::test_2 (fixtures used: login).
TEARDOWN C login

 从执行结果可以看到,在两条测试用例login在该类中只执行了一次 

 

第三个例子:scope="module"

import pytest


@pytest.fixture(scope="module")
def login():
print("登录")


def test_1(login):
print("登录后1")


def test_2(login):
print("登录后2")


class TestCase():
def test_case1(self, login):
print("类方法,登录1")

def test_case2(self, login):
print("类方法,登录2")


执行结果:

test_fix2.py
SETUP M login
test_fix2.py::test_1 (fixtures used: login).
test_fix2.py::test_2 (fixtures used: login).
test_fix2.py::TestCase::test_case1 (fixtures used: login).
test_fix2.py::TestCase::test_case2 (fixtures used: login).
TEARDOWN M login

 

第四个例子:scope="session",跨文件使用

文件一

import pytest


def test_1(login):
	print("登录后1")


def test_2(login):
	print("登录后2")


class TestCase():
	def test_case1(self, login):
		print("类方法,登录1")

	def test_case2(self, login):
		print("类方法,登录2")


文件二

import pytest


def test_1(login):
	print('第二个文件登录后的第一个用例')


conftest.py 文件
import pytest
@pytest.fixture(scope="session")
def login():
print("登录")



执行结果:

test_fix2.py
SETUP S login
test_fix2.py::test_1 (fixtures used: login).
test_fix2.py::test_2 (fixtures used: login).
test_fix2.py::TestCase::test_case1 (fixtures used: login).
test_fix2.py::TestCase::test_case2 (fixtures used: login).
test_fix1.py
test_fix1.py::test_1 (fixtures used: login).
TEARDOWN S login

  由上面的执行结果可知,当scope="session"的时候,多个测试文件,多个测试用例,添加fixtrue装饰器后都是执行一次

 

小结

由以上四个简单的例子,可以看到pytest具有的强大的fixtrue功能,可以使用例设计更加灵活,测试数据、测试依赖等配置更加方便,以后在书写测试脚本的时候,可以根据不同请款合理使用fixtrue功能 

 

posted @ 2019-11-17 21:44  zelatan  阅读(400)  评论(0编辑  收藏  举报
AmazingCounters.com