400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

Pytest框架之fixture的详细使用教程-创新互联

前言

前面一篇讲了setup、teardown可以实现在执行用例前或结束后加入一些操作,但这种都是针对整个脚本全局生效的
如果有以下场景:用例 1 需要先登录,用例 2 不需要登录,用例 3 需要先登录。很显然无法用 setup 和 teardown 来实现了fixture可以让我们自定义测试用例的前置条件

创新互联建站专注于企业全网营销推广、网站重做改版、岱山网站定制设计、自适应品牌网站建设、HTML5商城开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为岱山等各大城市提供网站开发制作服务。fixture优势 fixture参数列表

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
    print("fixture初始化的参数列表")

参数列表

注意

session的作用域:是整个测试会话,即开始执行pytest到结束测试

测试用例如何调用fixture
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ =
__Time__ = 2020-04-06 15:50
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import pytest
# 调用方式一
@pytest.fixture
def login():
 print("输入账号,密码先登录")

def test_s1(login):
 print("用例 1:登录之后其它动作 111")

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

# 调用方式二
@pytest.fixture
def login2():
 print("please输入账号,密码先登录")

@pytest.mark.usefixtures("login2", "login")
def test_s11():
 print("用例 11:登录之后其它动作 111")

# 调用方式三
@pytest.fixture(autouse=True)
def login3():
 print("====auto===")

# 不是test开头,加了装饰器也不会执行fixture
@pytest.mark.usefixtures("login2")
def loginss():
 print(123)

网站名称:Pytest框架之fixture的详细使用教程-创新互联
链接地址:http://www.bluegullmedia.com/article/cccgio.html

其他资讯

让你的专属顾问为你服务

0.0407s