Pytest入门

单元测试框架

java:junit 和 testing
python:unittest和pytest

单元框架主要做了什么

1.测试发现:从多个文件找到我们的测试用例
2.测试执行:按照一定的顺序和规则去执行,并生成结果
3.测试判断:通过断言判断预期结果和实际结果的差异
4.测试报告:统计测试进度,耗时,通过率,生成测试报告

Pytest单元测试框架和自动化测试框架的关系

单元测试框架:只是自动化测试框架中的组成部分之一
pom设计模式…
数据驱动…
关键字驱动…
日志监控…
selenium,requests二次封装…
断言…
报告邮件…
更多…

Pytest简介

1.pytest是一个非常成熟的单元测试框架,比unittest更灵活,更容易上手。
2.pytest可以和selenium,requests,appium结合实现web自动化,接口自动化,app自动化
3.pytest可以实现测试用例的跳过以及reruns失败用例重试
4.pytest可以和allure生成美观的测试报告
5.pytest可以和Jenkins持续集成
6.pytest有非常强大的插件,并且这些插件可以实现很多实用的操作。

Pytest
pytest-html:生成html格式的自动化测试报告
pytest-xdist:测试用例分布式执行,多CPU分发
pytest-ordering:用于改变测试用例的执行顺序
pytest-rerunfailures:用例失败后重跑
allure-pytest:用于生成美观的测试报告

安装插件可通过将依赖包加入requirements.txt,通过pip install -r requirements.txt安装

使用Pytest,默认的测试用例规则以及基础应用

1.python文件必须以test_开头或者_test结尾
2.测试类必须以Test开头,并且不能用_init_方法
3.测试方法必须以test开头

Pytest测试用例的运行方式

1.主函数模式
(1)运行所有:pytest.main()
(2)指定模块:pytest.main(['-vs','test_login.py'])
(3)指定目录:pytest.main(['-vs','./interface_testcase'])
(4)通过nodeid指定用例运行:nodeid由文件路径、文件名、类名、方法名、函数名组成。
pytest.main(['-vs','./interface_testcase/test_interface.py::test_04_func'])
pytest.main(['-vs','./interface_testcase/test_interface.py:TestInterface:test_03_func'])
2.命令行运行模式
(1)运行所有:pytest
(2)指定模块:pytest -vs test_login.py
(3)指定目录:pytest -vs ``'./interface_testcase/test_interface.py:TestInterface:test_03_func'
参数详解:
-s:表示输出调优信息,包括print打印的信息
-v:显示更详细的信息
-vs:这两个参数一起用
-n:pytest-xdist多线程运行(需要先安装pytest-xdist)。如pytest -vs ./testcase/test_login.py -n 2
--reruns NUM:重试运行测试用例(需要先安装pytest --rerunfailures)
-x:表示只要有一个用例报错,那么测试停止
--maxfail 2:出现2个用例失败就停止
-k:根据测试用例的部分字符串指定测试用例。如:pytest -vs ./testcase -k "ao"--html ./report/report.html:生成html的测试报告。(需要先安装pytest -xdist)。
3.通过读取pytest.ini配置文件运行
pytest.ini这个文件它是pytest单元测试框架的核心配置文件
1.位置:一般放在项目的根目录
2.编码:必须是ANSI,可以使用Notepad++修改编码格式
3.作用:改变pytet默认的行为
4.运行的规则:不管是主函数的模式运行,命令行模式运行,都会去读取这个配置文件
pytest.ini文件

[pytest]
addopts=-vs					# 命令行的参数,用空格分隔
testpaths=./testcase		# 测试用例的路径
python_files=test_*.py		# 模块名的规则
python_classes=Test*		# 类名的规则
python_functions=test		# 方法名的规则
markers=					# 标记
    smoke:冒烟用例
    usermanage:用户管理模块
    productmanage:商品管理模块

Pytest执行测试用例的顺序是怎样的呢?

unittest:使用ascII的大小来决定顺序的执行
pytest:默认从上到下

改变默认的执行顺序:使用mark标记。首先安装pytest-ordering插件
@pytest.mark.run(order=1)

# -*- coding: UTF-8 -*-
import pytest
class TestPytestDemo:
    @pytest.mark.run(order=2)
    def test_01_interface(self):
        print('测试test_01_interface')

    @pytest.mark.run(order=1)
    def test_02_interface(self):
        print('测试test_02_interface')
if __name__ == '__main__':
    pytest.main(['-vs', 'test_01_demo.py'])

如何分组执行(冒烟、分模块执行、分接口和web执行)

配置文件增加markers标签

[pytest]
addopts = -vs
testpaths = testcase/
python_files = test_*.py
python_classes = Test*
python_functions = test
markers =
    smoke:冒烟用例
    usermanage:用户管理模块
    productmanage:商品管理模块

测试用例:

# -*- coding: UTF-8 -*-
import pytest
class TestPytestDemo:
    @pytest.mark.smoke
    def test_01_interface(self):
        print('测试test_01_interface')
    @pytest.mark.usermanage
    def test_02_interface(self):
        print('测试test_02_interface')
	@pytest.mark.productmanage
    def test_03_interface(self):
        print('测试test_03_interface')

同时执行smoke和usermanage命令行调用:

pytest -m "smoke or usermanage"

控制台输出:

============================================================================= test session starts =============================================================================
platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python
cachedir: .pytest_cache
rootdir: /Users/xxx/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/
plugins: xdist-2.3.0, ordering-0.6, rerunfailures-10.1, forked-1.3.0
collected 4 items / 2 deselected / 2 selected                                                                                                                                 

testcase/test_01_demo.py::TestPytestDemo::test_01_interface 测试test_01_interface
PASSED
testcase/test_01_demo.py::TestPytestDemo::test_02_interface 测试test_02_interface
PASSED
======================================================================= 2 passed, 2 deselected in 0.01s =======================================================================

pytest跳过测试用例

(1)无条件跳过
使用@pytest.mark.skip(reason='xxx')
(2) 有条件跳过
使用@pytest.mark.skipif(condition,reason='xxx')
用例:

# -*- coding: UTF-8 -*-
import pytest

class TestPytestDemo:
    age = 17

    @pytest.mark.smoke
    @pytest.mark.skipif(age <= 18, reason='年龄不满18岁')
    def test_01_interface(self):
        print('测试test_01_interface')

    @pytest.mark.productmanage
    @pytest.mark.skip(reason='无条件跳过')
    def test_02_interface(self):
        print('测试test_03_interface')

命令行:

% pytest
============================================================================= test session starts =============================================================================
platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python
cachedir: .pytest_cache
rootdir: /Users/xxx/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/
plugins: xdist-2.3.0, ordering-0.6, rerunfailures-10.1, forked-1.3.0
collected 3 items                                                                                                                                                             

testcase/test_01_demo.py::TestPytestDemo::test_01_interface SKIPPED (年龄不满18岁)
testcase/test_01_demo.py::TestPytestDemo::test_02_interface SKIPPED (无条件跳过)

======================================================================== 0 passed, 2 skipped in 0.01s =========================================================================

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值