• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest笔记

武飞扬头像
慕长秋
帮助1

目录

一、命名原则

二、命令参数

三、指定测试

1. 在模块中运行测试

2. 在目录中运行测试

3. 按关键字表达式运行测试

4. 通过节点 id 来进行测试

5. 通过标记来执行 (只能运行有相应标识的测试用例)

四、pytest fixture

1. fixture scope的范围参数

2. 调用fixture的三种方法

2.1 函数或类里面方法直接传fixture的函数参数名称

2.2 使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

2.3 叠加usefixtures

3. fixture和usefixture区别

五、conftest全局处理器

六、参数化


一、命名原则

  1. 测试用例文件名必须以test_*.py*_test.py开头或结尾
  2. 测试类名必须以Test开头,并且不能有__init__方法,测试方法必须以 test_ 开头的函数
  3. 如果没有类名,那么以函数为单位的函数名必须以 test_ 开头
  4. 文件夹(包名)名可以随意命名

pytest的main方法运行方式

  1.  
    if __name__ == '__main__':
  2.  
     
  3.  
            pytest.main(['参数''文件名.py'])

二、命令参数

常用的命令参数:

pytest ‐x                          # 如果出现一条测试用例失败,则退出测试

pytest ‐‐maxfail=num      # 用例失败总数等于num 时停止运行

-q, --quiet                         # 极简结果显示

-V                                     # 可以输出用例更加详细的执行信息,比如用例所在的文件及用例名称等

-s                                      # 输出用例中调试信息,如print的打印信息

-v --verbose                      # 详细结果

--junit-xml=path                # 输出xml格式文件,与jenkins做集成时使用

--result-log=path              # 将最后的结果保存到本地文件中

ps:橙色为出现频率较高的命令

三、指定测试

1. 在模块中运行测试

pytest test_mod.py

2. 在目录中运行测试

pytest testing/

3. 按关键字表达式运行测试

pytest -k "01" test_02.py

会执行 test_02.py 下 01 的方法,用于匹配"文件名、类名、方法名"符合表达式的的测试用例

4. 通过节点 id 来进行测试

参数化的类名、函数名和参数,用::分隔。

规则        pytest 目录 / py文件 :: 类名 : 方法名

例子        pytest testcase/test_02.py::TestDemo02:test_01

5. 通过标记来执行 (只能运行有相应标识的测试用例)

  • pytest -m "tag01 or tag02" # 运行tag01和tag02的标记用例

这种方式会运行所有通过装饰器 @pytest.mark.mark进行装饰的测试用例

  1.  
    @pytest.mark.tag01
  2.  
    def test_01(self):
  3.  
    assert self.func(3) == 4
  4.  
     
  5.  
    @pytest.mark.tag02
  6.  
    def test_02(self):
  7.  
    assert self.func(3) == 4
  • pytest -m tag # 执行test_01.py内所有@pytest.mark.tag的标记用例

四、pytest fixture

  • 模块形式----使用setup_module/teardown_module
  • 函数形式----使用setup_function/teardown_function
  • 类形式----使用setup_class/teardown_class
  • 方法形式---使用setup_method/teardown_method

1. fixture scope的范围参数

@pytest.fixture(scope='module')来定义框架,scope的参数有以下几种

  • function 每一个用例都执行
  • class 每个类执行
  • module 每个模块执行(函数形式的用例)
  • session 每个session只运行一次,在自动化测试时,登录步骤可以使用该session

2. 调用fixture的三种方法

2.1 函数或类里面方法直接传fixture的函数参数名称

  1.  
    from __future__ import print_function
  2.  
    import pytest
  3.  
     
  4.  
    @pytest.fixture(scope='module') #范围是module
  5.  
    def resource_a_setup(request): #前置器
  6.  
    print('\n resources_a_setup()')
  7.  
    def resource_a_teardown(): #后置器
  8.  
    print('\n resources_a_teardown()')
  9.  
    request.addfinalizer(resource_a_teardown)
  10.  
     
  11.  
    def test_1(resource_a_setup):
  12.  
    print('test_1()')
  13.  
     
  14.  
    def test_2():
  15.  
    print('\ntest_2()')
  16.  
     
  17.  
    def test_3(resource_a_setup):
  18.  
    print('\ntest_3()')
学新通

2.2 使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

  1.  
    @pytest.fixture()
  2.  
    def test1(): #套件名test1
  3.  
    print('\n开始执行function')
  4.  
    @pytest.mark.usefixtures('test1') #方法使用套件test1
  5.  
    def test_a():
  6.  
    print('---用例a执行---')
  7.  
    @pytest.mark.usefixtures('test1') #类使用套件test1
  8.  
    class TestCase:
  9.  
    def test_b(self):
  10.  
    print('---用例b执行---')
  11.  
    def test_c(self):
  12.  
    print('---用例c执行---')
  13.  
    if __name__ == '__main__':
  14.  
    pytest.main(['-s', 'test_fixture1.py'])

2.3 叠加usefixtures

如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层

  1.  
    @pytest.fixture()
  2.  
    def test1(): #套件test1
  3.  
    print('\n开始执行function1')
  4.  
    @pytest.fixture()
  5.  
    def test2(): #套件test2
  6.  
    print('\n开始执行function2')
  7.  
     
  8.  
    @pytest.mark.usefixtures('test1')
  9.  
    @pytest.mark.usefixtures('test2')
  10.  
    def test_a(): #方法使用套件test1和test2
  11.  
    print('---用例a执行---')
  12.  
    #或
  13.  
     
  14.  
     
  15.  
    @pytest.mark.usefixtures('test2')
  16.  
    @pytest.mark.usefixtures('test1')
  17.  
    class Testcase: #类使用套件test1和test2
  18.  
    def test_c(self):
  19.  
    print('---用例c执行---')
  20.  
    if __name__ == '__main__':
  21.  
    pytest.main(['-s', 'test_fixture1.py'])
学新通

3. fixture和usefixture区别

如果fixture有返回值,那么usefixture就无法获取到返回值

五、conftest全局处理器

conftest.py通常可以用来做2个事情:

  1. 存放你的fixture函数
  2. 在里面写自己的本地插件

conftest.py作用全局,不可跨模块调用,作用范围只能在该层级以及以下目录生效

另外,可以根据conftest.py的文件存放位置,来决定它的适用范围,实际应用需要结合fixture来使用。

  1.  
    # /test_module_01/conftest.py
  2.  
    # 作用于test_module_01下的py文件
  3.  
     
  4.  
    import pytest
  5.  
     
  6.  
    @pytest.fixture
  7.  
    def demo_fixture():
  8.  
    print("这是fixture函数的输出")

六、参数化

@pytest.mark.parametrize("参数名",列表数据)

参数名:用来接收每一项数据,并作为测试用例的参数。

列表数据:测试数据,支持列表、元组、字典列表、字典元组,有多少值用例就执行多少次

# 和unittest的ddt里面@unpack解包相同
  1.  
    方式一:直接写入
  2.  
    class Testapi
  3.  
    @pytest.mark.parametrize('name,arg',[['赵四','24'],['张三','25']])
  4.  
    def test_01(self,name,arg):
  5.  
    print(name,arg)
  6.  
    if __name__ == '__main__':
  7.  
    pytest.main
  1.  
    方式二:参数为列表中嵌套元组或字典
  2.  
    class Testapi
  3.  
    date_dicts = [
  4.  
    {'user':01,'pwd':123},
  5.  
    {'user':02,'pwd':123},
  6.  
    {'user':03,'pwd':123}
  7.  
    ]
  8.  
     
  9.  
    @pytest.mark.parametrize("data",date_dicts)
  10.  
    def test_01(data):
  11.  
    print(data)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeakib
系列文章
更多 icon
同类精品
更多 icon
继续加载