pytest介绍

概述

pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:

  • 简单灵活,容易上手,文档丰富;
  • 支持参数化,可以细粒度地控制要测试的测试用例;
  • 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
  • pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
  • 测试用例的skip和xfail处理;
  • 可以很好的和CI工具结合,例如jenkins

install package

pip3 install pytest pytest-html pytest-xdist pytest-rerunfailures pytest-ordering pytest-dependency --index-url https://pypi.douban.com/simple Image

验证:pytest --version

pytest框架约束

1所有的单测文件名都需要满足test_*.py格式或*_test.py格式。 2在单测文件中,测试类以Test开头,并且不能带有 init 方法(注意:定义class时,需要以Test开头,不然pytest是不会去运行该class) 3在单测类中,可以包含一个或多个test_开头的函数。 4此时,在执行pytest命令时,会自动从当前目录及子目录中寻找符合上述约束的测试函数来执行。

pytest运行方式

1# file_name: test_case.py 2 import pytest # 引入pytest包 3 def test_a(): # test开头的测试函数 4 print("------->test_a") 5 assert 1 == 1 # 断言成功 6 def test_b(): 7 print("------->test_b") 8 assert 1 == 2 # 断言失败 9 if __name__ == '__main__': 10 pytest.main("-s test_case.py") # 调用pytest的main函数执行测试

如何运行

  1. pytest # run all tests below current dir
  2. pytest test_mod.py # run tests in module file test_mod.py
  3. pytest somepath # run all tests below somepath like ./tests/
  4. pytest -k stringexpr # only run tests with names that match the the "string expression", e.g. "MyClass and not method" will select TestMyClass.test_something but not TestMyClass.test_method_simple
  5. pytest test_mod.py::test_func # only run tests that match the "node ID", e.g "test_mod.py::test_func" will be selected only run test_func in test_mod.py

scope

fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

  • @pytest.fixture(scope="session", autouse="true")
  • @pytest.fixture(scope="module", autouse="true")
  • @pytest.fixture(scope="class", autouse="true")
  • @pytest.fixture(scope="function", autouse="true")

测试用例执行顺序

  • pytest-ordering 通过装饰器@pytest.mark.run(order=1)来进行控制,数字越小,越前执行
  • 安装pytest-dependency 在对应的方法A上添加@pytest.mark.dependency()对所依赖的方法进行标记设置为被依赖方法,在依赖方法使用@pytest.mark.dependency(depends=["被依赖方法名"])引用依赖 可添加name=参数
  • @pytest.fixture装饰,包括session、module、class、function
  • @pytest.mark.skip() 可以装饰方法与类,用于跳过该用例

三方插件

  • pytest-selenium(集成selenium)
  • pytest-html(完美html测试报告生成)
  • pytest-rerunfailures(失败case重复执行)
  • pytest-xdist(多CPU分发)
  • pytest-ordering(控制用例执行顺序)
  • pytest-dependency(用例直接依赖调用)
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

4cast

4castpackageloadcsv.KumarAwanish发布:2020122117:43:04.501348作者:KumarAwanish作者邮箱:awanish00@gmail.com首页:

Python之time模块的时间戳、时间字符串格式化与转换

Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y

pytest介绍 - HelloWorld