0
点赞
收藏
分享

微信扫一扫

Pytest单元测试框架

猎书客er 2022-04-29 阅读 65
单元测试

文章目录

一、Pytest简介

1.定义:
 pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以上的插件,同时兼容 unittest 框架。这就使得我们在 unittest 框架迁移到 pytest 框架的时候不需要重写代码。

2.作用:

  • 单元测试框架,比unittest测试框架更灵活;
  • 入门难度低,第三方库丰富;
  • 通用性;
  • 与allure生成的报告非常美观;
  • 定制性强;

二、Pytest代码编写

1.编写代码注意事项

  • 模块名称(即py文件名称)必须以test_开头或以_test结尾;
  • 测试的类名(即Class Testxxx)必须以Test开头,且在类中不能含有init;
  • 测试方法(即def testxxx)必须以test开头;

2.pytest测试用例运行方式

1)主函数方式:

  if __name__ == '__main__':
  	  pytest.main([ "-vs","test_1.py"])

 a).运行所有测试用例:
  方法:pytest.main(参数)
  参数:
  -s 打印测试用例中的print语句;
  -v 增加打印细节;
  -x 一旦发现失败用例,停止运行;
  -maxfail=2 出现两个用例失败就停止;
  -n 多线程:pytest.main([“-vs”,“./testcase”,“-n=2”])两个线程;
  -k 组合调用执行部分用例:pytest -vs ./testcase -k “ao"只执行含有ao的测试用例;
  -m “标签名” 给测试用例添加标签:
    pytest.main([”-s",“test_123.py”,“-m=webtest”])只运行用webtest标记的测试;

 b).指定模块运行:
  pytest.main([“-vs”,“TestAdd::test_add2”])
  pytest.main([“-vs”,“test_123.py”])

 c).指定目录运行:
  pytest.main([“-vs”,“./文件夹名称”])

2)命令行方式:

 a).运行所有:pytest test_add.py
 b).指定模块:pytest -vs test_add1.py
 a).运行所有:pytest -vs ./文件夹名称

E:\python_practice\test__123.py>pytest -vs  test_1.py
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-7.1.2, pluggy-0.13.1 -- e:\python3.7\pyth
cachedir: .pytest_cache
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=
metadata: {'Python': '3.7.0', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages
'forked': '1.4.0', 'html': '3.1.1', 'metadata': '2.0.1', 'ordering': '0.6', 'rer
rootdir: E:\python_practice\test__123.py
plugins: allure-pytest-2.9.45, benchmark-3.2.3, forked-1.4.0, html-3.1.1, metada
collected 3 items                                                              

test_1.py::test_add PASSED
test_1.py::TestAdd::test_add PASSED
test_1.py::TestAdd::test_add2 PASSED

3)配置文件pytest.ini方式:

位置:pytest.ini一般放置在项目根目录中,名字就为这个名字不要进行改变;
编码:ANSI编码;
作用:改变pytest默认的行为
使用方法:

[pytest]
addopts = -vs
testpaths = ./test_case
python_files = test_*.py
python_classes = Test*
python_functions = test

注:配置好文件后运行pytest -vs可能会有报错–>这个问题可以用notpad++改变编码格式为ANSI

UnicodeDecodeError: 'gbk' codec can't decode byte 0xb9 in position 46: illegal m

3.测试用例执行顺序

pytest默认的执行顺序是从上到下
注:如果想要改变默认的顺序需要使用mark标记
如:@pytest.mark.run(order=1)即顺序为第一个

# 测试脚本
class TestAdd:
    @pytest.mark.run(order=2)
    def test_add2(self):
        assert 3 == add(1, 2)

    @pytest.mark.run(order=1)
    def test_add3(self):
        assert 5 == add(1, 4)

运行结果:

test_case/test_1.py::TestAdd::test_add3 PASSED
test_case/test_1.py::TestAdd::test_add2 PASSED

4.断言的使用

  • assert “h” in "“hello”
  • assert 3==4
  • assert 3!=4
  • assert f()==4
  • assert not xx(即:判断xx不为真)

5.mark方法使用

1)跳过(skip)和失败(xfail)

跳过:pytest.skip(reason=‘原因’)
 无条件跳过:@pytest.mark.skip(reason=‘原因’)
 有条件跳过:@pytest.mark.skipif(条件,reason=‘原因’)
失败:pytest.xfail(reason=‘原因’)
 使用:@pytest.mark.xfail(reason=‘原因’)
注:如果在测试时希望看到跳过原因可以使用pytest -rs

2)分组执行用例

  • smoke:冒烟用例,分布在各个模块里面,使用:@pytest.mark.smoke
  • g1:为组一,在使用时可以任意定义名称,使用:@pytest.mark.g1
  • g2:为组二,在使用时可以任意定义名称,使用:@pytest.mark.g2
#此时的配置文件为:
[pytest]
addopts = -vs
testpaths = ./test_case
python_files = test_*.py
python_classes = Test*
python_functions = test
markers=
		smoke:冒烟用例
		g1:组一
		g2:组二

在执行用例时可使用
pytest -m “smoke”
pytest -m “smoke or g1 or g2”
pytest -m “smoke and g1”

举报

相关推荐

0 条评论