场景:测试用例执行时,有的用例需要登陆才能执行,有些用例 不需要登陆。setup和teardown无法满足。fixture可以。默认 scope(范围)function

• 步骤:
1. 导入pytest
2. 在登陆的函数上面加@pytest.fixture()
3. 在要使用的测试方法中传入(登陆函数名称),就先登陆
4. 不传入的就不登陆直接执行测试方法

import pytest
@pytest.fixture(params=[1,2,3,'linda'])------可以是元组,也可以是列表
def prepara_data(request):------reuest是固定写法
return request.param
------reuest.param是固定写法


def test_one(prepara_data):
print('testdata:%s'%prepara_data)

案例2:

import pytest
@pytest.fixture(params=[1,2,3,'linda'])
def prepara_data(request):
return request.param def test_one(prepara_data):
print('testdata:%s'%prepara_data) def test_two(prepara_data):
if type(prepara_data)is str:      -----test2只执行传递参数为str类型的
print('testdata2:%s'%prepara_data) if __name__ == '__main__':
pytest.main()

pytest--fixture之参数化的更多相关文章

  1. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  2. 11、pytest -- 测试的参数化

    目录 1. @pytest.mark.parametrize标记 1.1. empty_parameter_set_mark选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_g ...

  3. pytest -- 测试的参数化

    目录 1. @pytest.mark.parametrize标记 1.1. empty_parameter_set_mark选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_g ...

  4. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  5. Pytest fixture及conftest详解

    前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  6. pytest.fixture和普通函数调用

    普通函数嗲用def one(): a="aaaaaaaaaaa" return a def test_one(): s=one() print (s) test_one() pyt ...

  7. pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能

    conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...

  8. pytest fixture 利用 params参数实现用例集合

    @pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...

  9. pytest fixture中scope试验,包含function、module、class、session、package

    上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...

  10. pytest - 测试函数的传参:fixture,参数化。必须传入实参

    测试函数的参数只有2个来源:fixture返回,参数化(ddt) 传入的参数必须是实参 pytest - 参数化 在测试用例的前面加上: @pytest.mark.parametrize(" ...

随机推荐

  1. Linux折腾

    安装了一圈发行版,最后发现还是Fedora最稳定 debian安装后无法启动 openSUSE源不完善 manjaro重启就进不去

  2. 导出CSV格式

    import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype. ...

  3. Jsoup爬虫任务总结

    这两周由于公司需要大量数据爬取进数据库给用户展示素材,在不停的做爬虫工作,现在总算基本完成就剩清理数据的工作: 公司有一个采集器管理后台的项目,可以直接把爬虫代码打包成jar导入进去设置定时参数即可: ...

  4. zabbix--Simple checks 基本检测

    开始      Simple checks 通常用来检查远程未安装代理或者客户端的服务. 使用 simple checks,被监控客户端无需安装 zabbixagent 客户端, zabbix ser ...

  5. window 下搭建流媒体服务器ffmpeg nginx-rmtp-module

    媒体介绍和需要下载需要软件 1.FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.在这里我只用到了它的视屏格式转换功能,将rtsp协议的视频流转成rtmp 2.ng ...

  6. js 禁止右击保存图片,禁止拖拽图片

    禁止鼠标右键保存图片 <img src="" oncontextmenu="return false;"> 禁止鼠标拖动图片 <img src ...

  7. 【记录】eclipse / STS 设置注释模版格式/导入注释模版格式

    设置注释模板的入口:Window->Preference->Java->Code Style->Code Template 将如下保存在新创建的xml文件中,导入进去 < ...

  8. icomoon字体图标引用代码

    1.第一步在样式里声明字体:告诉别人我们自己定义的字体. @font-face{ /*声明字体 引用字体*/ font-family:'icomoon'; src:url('fonts/icomoon ...

  9. 36.两个链表的第一个公共结点(python)

    题目描述 输入两个链表,找出它们的第一个公共结点. class Solution: def FindFirstCommonNode(self, pHead1, pHead2): # write cod ...

  10. String的static方法

    //String concat(String str) 拼接字符串 String concat_str0 = "abc"; String concat_str1 = "b ...