pytest三:fixture_conftest.py 自定义测试用例的预置条件(setup)

用例加 setup 和 teardown 可以实现在测试用例之前或之后加入一些操作,但返种是整个脚本全局生效的,
如果我想实现以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录。很显然这就无法用 setup 和 teardown 来实现了。

fixture 优势
firture 相对于 setup 和 teardown 来说应该有以下几点优势
  命名方式灵活,不局限于 setup 和 teardown 返几个命名
  conftest.py 配置里可以实现数据共享,丌需要 import 就能自动找到一些配置
  scope="module" 可以实现多个.py 跨文件共享前置
  scope="session" 以实现多个.py 跨文件使用一个 session 来完成多个用例

fixture(scope="function", params=None, autouse=False, ids=None, name=None):

可以使用此装饰器(带或不带参数)来定义 fixture 功能。 fixture功能的名称可以在以后使用,引用它会在运行测试之前调用它:test 模块或类可以使用pytest.mark.usefixtures(fixturename) 标记。
测试功能可以直接使用 fixture 名称作为输入参数,在返种情况下,夹具实例从 fixture 返回功能将被注入。
scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
  params: 一个可选的参数列表,它将导致多个参数调用fixture 功能和所有测试使用它
  autouse: 如果为 True,则为所有测试激活 fixture func 可以看到它。 如果为 False(默认值)则显式需要参考来激活 fixture
  ids: 每个字符串 id 的列表,每个字符串对应于 params 这样他们就是测试 ID 的一部分。 如果没有提供 ID 它们将从 params 自动生成
  name: fixture 的名称。 返默认为装饰函数的名称。 如果fixture 在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能 arg 遮蔽; 解决这个问题的一种方法是将装饰函数命名fixture_ <fixturename>”然后使用”@ pytest.fixture(name='<fixturename>')

Fixtures 可以选择使用 yield 语句为测试函数提供它们的值,而不是 return。 在这种情况下,yield 语句之后的代码块作为拆卸代码执行,而不管测试结果如何。fixture 功能必须只产生一次fixture 参数传入(scope=”function”)

如果@pytest.fixture()里面没有参数,那么默认 scope=”function”,也就是此时的级别的 function,针对函数有效

import pytest

@pytest.fixture()
def login():
print('输入账号密码,先登录')

def test_s1(login): # 传login
print('用例1:登录之后其他动作111')

def test_s2(): # 不传login
print('用例2:不需要登录,操作222')

def test_s3(login): # 传login
print('用例3:登录之后其他动作333')

if __name__=='__main__':
pytest.main()



conftest.py 配置:
上面一个案例是在同一个.py 文件中,多个用例调用一个登录功能,如果有多个.py 的文件都需要调用这个登录功能的话,那就不能把登录写到用例里面去了。
此时应该要有一个配置文件,单独管理一些预置的操作场景,pytest里面默认读取 conftest.py 里面的配置
conftest.py 配置需要注意以下点:
  conftest.py 配置脚本名称是固定的,不能改名称,即配置脚本的名称必须为:conftest.py
  conftest.py 与运行的用例要在同一个 pakage 下,并且有__init__.py 文件
  不需要 import 导入 conftest.py,pytest 用例会自动查找

新建一个.py文件,名为conftest.py,里面写一个login

再在另外一个脚本中调用conftest.py下的login

import pytest

@pytest.fixture()
def login():
print('输入账号、密码,登录')
import pytest

def test_s1(login): # 传login
print('用例1:登录之后其他动作111')

def test_s2(): # 不传login
print('用例2:不需要登录,操作222')

def test_s3(login): # 传login
print('用例3:登录之后其他动作333')

if __name__=='__main__':
pytest.main()


这样其他测试用例都能调用到 login()方法,这样就能实现一些公共的操作可以单独拿出来了



scope=”module”
fixture 参数 scope=”module”,module 作用是整个.py 文件都会生效,用例调用时,参数写上函数名称就行

从结果看出,虽然 test_s1,test_s2,test_s3 三个地方都调用了 open函数,但是它只会在第一个用例前执行一次

import pytest

@pytest.fixture(scope='module')
def open():
print('打开浏览器=============')

def test_s1(open):
print('用例1111111')

def test_s2(open):
print('用例22222222')

def test_s3(open):
print('用例33333333')

if __name__=='__main__':
pytest.main()

如果 test_s1 不调用,test_s2(调用 open),test_s3 不调用,运行顺序会是怎样的?

从结果看出,module 级别的 fixture 在当前.py 模块里,只会在用例(test_s2)第一次调用前执行一次

import pytest

@pytest.fixture(scope='module')
def open():
print('打开浏览器=============')

def test_s1():
print('用例1111111')

def test_s2(open):
print('用例22222222')

def test_s3():
print('用例33333333')

if __name__=='__main__':
pytest.main()



原文地址:https://www.cnblogs.com/zhongyehai/p/9679761.html