当前位置:   article > 正文

Pytest 使用简介及常用方法_pytest用法

pytest用法

目录

前言

一、安装

二、简单使用

1.创建 test_sample.py 文件,代码如下:

2.使用 pytest 执行测试需要遵行的规则:

3.pytest.ini 配置文件 

4.addopts: OPTS 命令行参数集

三、常用方法

1.pytest.mark.parametrize 装饰器

1.1一次传多个参数

1.2组合传参:

2、@pytest.fixture()

1.fixture scope 作用范围

1.获取被调用函数返回值

2.单个用例调用多个函数

 3.yield

 3.conftest

4、重试机制

1、安装 pytest-rerunfailures

2、重试的两种方法

5、skipif-跳过测试

1.pytest.skip (用于函数内,跳过测试用例)

1.1也可以用于模块级别,跳过当前模块里所有的测试用例。(注:参数allow_module_level的值设为True)

2.@pytest.mark.skip(用于函数外,跳过测试用例)

3.@pytest.mark.skipif(用于函数外,条件condition,跳过原因reason="xxx")

6、控制执行顺序

order-执行顺序

7、预期失败

1.xfail-预期失败


前言

  最近在听极客时间的课程,里面的讲师极力推崇 pytest 框架,鄙视 unittest 框架,哈哈!然后查了些资料,发现了一条 python 鄙视链:pytest 鄙视 > unittest 鄙视 >  robotframework 。

  pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以上的插件,同时兼容 unittest 框架。这就使得我们在 unittest 框架迁移到 pytest 框架的时候不需要重写代码。接下来我们在文中来对分析下 pytest 有哪些简洁、高效的用法。

一、安装

首先使用 pip 安装 pytest 

pip3 install pytest

 查看 pytest 是否安装成功

pip3 show pytest

二、简单使用

1.创建 test_sample.py 文件,代码如下:

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import pytest
  4. def inc(x):
  5. return x + 1
  6. def test_answer():
  7. assert inc(3) == 5
  8. if __name__ =="__main__":
  9. pytest.main()

执行结果: 

  1. test_sample.py F [100%]
  2. ================================== FAILURES ===================================
  3. _________________________________ test_answer _________________________________
  4. def test_answer():
  5. > assert inc(3) == 5
  6. E assert 4 == 5
  7. E + where 4 = inc(3)
  8. test_sample.py:19: AssertionError
  9. ============================== 1 failed in 0.41s ==============================

从上面的例子可以看出,pytest 中断言的用法直接使用 assert ,和 unittest 中断言 self.assert 用法有所区别。

2.使用 pytest 执行测试需要遵行的规则:

  • .py 测试文件必须以test_开头(或者以_test结尾) 

  • 测试类必须以Test开头,并且不能有 init 方法

  • 测试方法必须以test_开头

  • 断言必须使用 assert

3.pytest.ini 配置文件 

  1. [pytest]
  2. addopts = -v -s --html=py_test/scripts/report/report.html -p no:warnings --reruns=10
  3. testpaths = ./py_test/scripts
  4. python_files= test_rerun.py
  5. python_classes = Test*
  6. python_function = test*
  7. xfail_strict = true

4.addopts: OPTS 命令行参数

-s:表示输出调试信息,包括 print打印的信息
-v:显示更详细的信息
-vs:这两个参数一起用
-n :支持多线程或者分布式运行测试用例
     如:pytest -vs ./testcase/test_login.py -n 2
--html : 测试报告位置
--reruns :失败重跑
-p no:warnings  :取消警告
--ff :先执行上次失败的用例
--lf :只执行上次失败的用例
-x :遇到测试用例fail,就结束测试
--maxfail=num:遇到num条测试用例fail, 就结束测试 
-k :根据测试用例的部分字符串指定测试用例
如:pytest -vs ./testcase -k “ao”

三、常用方法

1.参数化

 pytest 内置装饰器 @pytest.mark.parametrize 可以让测试数据参数化,把测试数据单独管理,类似 ddt 数据驱动的作用,方便代码和测试数据分离。

1.1一次传多个参数

  1. import pytest
  2.  
  3. @pytest.mark.parametrize('x,y',[(1,2),(3,4)])
  4. def test_sum(x,y):
  5.     sum = x + y
  6.     print(sum)
  7.  
  8. if __name__ =="__main__":
  9.     pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py 
  2. 3
  3. .
  4. 7
  5. .
  6.  
  7. ============================== 2 passed in 0.06s ==============================

1.2组合传参:

注意:这种方式一共传递了4组参数  (1,3)、(1,4)、(2,3)、(2,4)。这种方式可以简化测试数据,不用手动再将参数组合。

  1. import pytest
  2.  
  3. @pytest.mark.parametrize('x',[1,2])
  4. @pytest.mark.parametrize('y',[3,4])
  5. def test_sum(x,y):
  6.     sum = x + y
  7.     print(sum)
  8.  
  9. if __name__ =="__main__":
  10.     pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py 
  2. 4
  3. .
  4. 5
  5. .
  6. 5
  7. .
  8. 6
  9. .
  10.  
  11. ============================== 4 passed in 0.14s ==============================

2、@pytest.fixture()

pytest 提供的 fixture 实现 unittest  中 setup/teardown 功能,可以在每次执行case之前初始化数据。不同点是,fixture 可以只在执行某几个特定 case 前运行,只需要在运行 case 前调用即可。比 setup/teardown 使用起来更灵活。

2.1fixture scope 作用范围

 先看下 fixture 函数的定义:

  1. def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
  2. """
  3. :arg scope: 可选四组参数:function(默认)、calss、module、package/session
  4. :arg params: 一个可选的参数列表,它将导致多个参数调用fixture函数和所有测试使用它。
  5. :arg autouse: 如果为True,则fixture func将为所有测试激活可以看到它。如果为False(默认值),则需要显式激活fixture。
  6. :arg ids: 每个参数对应的字符串id列表,因此它们是测试id的一部分。如果没有提供id,它们将从参数中自动生成。
  7. :arg name: fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名 “fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')”。
  8.   """

 重点说下 scope 四组参数的意义:

  • function:每个方法(函数)都会执行一次。

  • class:每个类都会执行一次。类中有多个方法调用,只在第一个方法调用时执行。

  • module:一个 .py 文件执行一次。一个.py 文件可能包含多个类和方法。

  • package/session:多个文件调用一次,可以跨 .py 文件。

2.2获取被调用函数返回值

  1. import pytest
  2.  
  3. @pytest.fixture(scope='function')
  4. def login():
  5.     accesstoken = '197ce8083c38467f'
  6.  
  7.     return accesstoken
  8.  
  9.  
  10. def test_sum(login):
  11.     token = login
  12.     print(token)
  13.  
  14. if __name__ =="__main__":
  15.     pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py 
  2. 197ce8083c38467f
  3. .
  4.  
  5. ============================== 1 passed in 0.04s ==============================

若被调用函数返回多个参数:

  1. import pytest
  2.  
  3. @pytest.fixture(scope='function')
  4. def login():
  5.     accesstoken = '197ce8083c38467f'
  6.     customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
  7.  
  8.     return accesstoken,customerguid
  9.  
  10.  
  11. def test_sum(login):
  12.     token = login[0]
  13.     guid = login[1]
  14.     print(token)
  15.     print(guid)
  16.  
  17. if __name__ =="__main__":
  18.     pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py 
  2. 197ce8083c38467f
  3. 096799f5-e040-11e9-8c01-0242ac11000d
  4. .
  5.  
  6. ============================== 1 passed in 0.07s ==============================

2.3单个用例调用多个函数

  1. import pytest
  2.  
  3. @pytest.fixture(scope='function')
  4. def login():
  5.     print('登录')
  6.  
  7. @pytest.fixture(scope='function')
  8. def conn():
  9.     print('连接数据库')
  10.  
  11. def test_1(login,conn):
  12.     print('测试用例1')
  13.  
  14. def test_2():
  15.     print('测试用例2')
  16.  
  17. if __name__ =="__main__":
  18.     pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py 
  2. 登录
  3. 连接数据库
  4. 测试用例1
  5. .
  6. 测试用例2
  7. .
  8.  
  9. ============================== 2 passed in 0.05s ==============================

 2.4yield

 我们刚刚实现了在每个用例之前执行初始化操作,那么用例执行完之后如需要 清除数据(或还原)操作,可以使用 yield 来实现。

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import pytest
  4. @pytest.fixture(scope='function')
  5. def login():
  6. print("登录")
  7. yield
  8. print("注销登录")
  9. def test_1():
  10. print('测试用例1')
  11. def test_2(login):
  12. print('测试用例2')
  13. if __name__ =="__main__":
  14. pytest.main(['test_sample.py','-s'])

 执行结果:

  1. test_sample.py
  2. 测试用例1
  3. .
  4. 登录
  5. 测试用例2
  6. .注销登录
  7. ============================== 2 passed in 0.08s ==============================

3.conftest

        上面的案例都是写在同一个.py 文件内的。倘若有多个.py 文件需要调用 login() 方法,就必须把 login() 方法写在外面,这里引用了conftest.py 配置文件。test_xxx.py 测试文件中无需 import  conftest,pytest 会自动搜索同级目录中的 conftest.py 文件。

 conftest.py 与测试文件目录层级关系

  1. # 新建conftest.py,和 test_sample.py 同级目录
  2. import pytest
  3. @pytest.fixture(scope='function')
  4. def login():
  5. print("登录")
  6. # test_sample.py 代码如下
  7. import pytest
  8. def test_1():
  9. print('测试用例1')
  10. def test_2(login):
  11. print('测试用例2')
  12. if __name__ =="__main__":
  13. pytest.main(['test_sample.py','-s'])

执行结果:

  1. test_sample.py
  2. 测试用例1
  3. .
  4. 登录
  5. 测试用例2
  6. .
  7. ============================== 2 passed in 0.01s ==============================

4、重试机制

有的时候用例执行失败了,然后排查发现不是代码问题,可能和环境或者网络不稳定有关系,这个时候可以引入重试机制,排除一些外在因素。

1、安装 pytest-rerunfailures

pip3 install pytest-rerunfailures

2、重试的两种方法

1)使用装饰器 @pytest.mark.flaky(reruns=5, reruns_delay=2) 

  • reruns :最大重试次数
  • reruns_delay :重试间隔时间,单位是秒
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import pytest
  4. @pytest.mark.flaky(reruns=5, reruns_delay=2)
  5. def test():
  6. assert 0==1
  7. if __name__ =="__main__":
  8. pytest.main(['test_sample.py','-s'])

 R表示用例失败后正在重试,尝试5次。

2)也可以使用命令行 pytest --reruns 5 --reruns-delay 2 -s ,参数与装饰器 @pytest.mark.flaky 一致,这个就不多说了。

5、测试用例分类@pytest.mark.smoke

        有时候我们只需执行部分测试用例,比如从用例集当中挑选 smoke 测试,要怎么做呢?通过装饰器 @pytest.mark.smoke,smoke 是可以自定义的,运行时加上命令‘-m=smoke’,pytest 就会挑选带有装饰器的类或函数运行。

  1. import pytest
  2.  
  3. @pytest.fixture(scope='function')
  4. def login():
  5.     accesstoken = '197ce8083c38467f'
  6.     customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
  7.  
  8.     return accesstoken,customerguid
  9.  
  10. @pytest.mark.smoke
  11. def test_sum(login):
  12.     token = login[0]
  13.     guid = login[1]
  14.     print(token)
  15.     print(guid)
  16.  
  17. def test_2():
  18.     print('测试用例')
  19.  
  20. if __name__ =="__main__":
  21.     pytest.main(['test_sample.py','-s','-m=smoke'])

 执行结果:

  1. test_sample.py 
  2. 197ce8083c38467f
  3. 096799f5-e040-11e9-8c01-0242ac11000d
  4. .
  5.  
  6. ======================= 1 passed, 1 deselected in 0.02s =======================


6、skipif-跳过测试


1.pytest.skip (用于函数内,跳过测试用例)

  1. def test_2():
  2.     if 1 < 2:
  3.         pytest.skip('1111111')
  4.     pass

1.1也可以用于模块级别,跳过当前模块里所有的测试用例。(注:参数allow_module_level的值设为True)

  1. if 1==1
  2.     pytest.skip('1111111',allow_model_level=True)
  3.  
  4. def test_1():
  5.     pass
  6.  
  7. def test_2():
  8.     pass

2.@pytest.mark.skip(用于函数外,跳过测试用例)

  1. @pytest.mark.skip(reason='feature not implemented')
  2. def test_1():
  3.     pass
  4.  
  5. # 模块级别跳过。(注:参数allow_module_level的值设为True)
  6. pytest.skip('skip all tests', allow_module_level=True)

3.@pytest.mark.skipif(用于函数外,条件condition,跳过原因reason="xxx")

  1. @pytest.mark.skipif(condition='1<2',reason='feature not implemented')
  2. def test_1():
  3.    pass

7、控制执行顺序

order-执行顺序

1、控制用例执行顺序的方法
2、在需要调整用例执行顺序的函数(或方法)前增加,如@pytest.mark.run(order=x),x表示数字
3、执行顺序,由小到大、由正到负、未标记的在正数后、负数前执行,顺序为:1,2,3,无标记,-3,-2,-1

4、提示:需要下载   pytest_ordering  依赖包,否则@pytest.mark.run(order=x)既不报错也不会生效

  1. class Testpytest(object):
  2.  
  3.   @pytest.mark.run(order=-1)
  4.   def test_two(self):
  5.     print("test_two, 测试用例")
  6.  
  7.   @pytest.mark.run(order=3)
  8.   def test_one(self):
  9.     print("test_one, 测试用例")
  10.  
  11.   @pytest.mark.run(order=1)
  12.   def test_three(self):
  13.     print("test_three, 测试用例")

8、预期失败

1.xfail-预期失败

xfail-预期失败的函数语法xfail(condition, reason):

--condition 预期失败的条件
--reason 预期失败的原因

  1. #  condition 条件相等判断失败
  2. @pytest.mark.xfail(condition='1==1', reason="The test case")
  3. def test_1():
  4.     print("\n-------")
  5.  
  6. #  condition 条件不等判断成功
  7. @pytest.mark.xfail(condition='1==2', reason="The test case")
  8. def test_2():
  9.     print("\n-------")
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/739858
推荐阅读
相关标签
  

闽ICP备14008679号