百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

全网最全pytest大型攻略,单元测试学这就够了

yuyutoo 2024-10-12 01:26 10 浏览 0 评论

pytest 是一款以python为开发语言的第三方测试,主要特点如下:

  • 比自带的 unittest 更简洁高效,兼容 unittest框架
  • 支持参数化
  • 可以更精确的控制要测试的测试用例
  • 丰富的插件,已有300多个各种各样的插件,也可自定义扩展,如pytest-selenium、pytest-html、pytest-rerunfailures、pytes-xdish
  • 可很好的和CI工具结合

安装

pip install pytest

测试用例编写规则

  • 测试文件以test_开头 或者 _test结尾
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试文件以 test_开头
  • 断言使用基本的 assert 即可

pytest会递归查找当前目录及子目录下所有 以test_开始 或者 _test结尾的python脚本,执行其中符合规则的函数和方法,不需要显示调用

运行命令:(cmd进入用例所在目录)

pytest folder_name ======》直接运行文件夹内符合规则的所有用例

pytest test_file.py ======》执行某个py文件中的用例

pytest test_file.py::test_func ======》执行模块内的某个函数(节点运行)

pytest test_file.py::TestClass::test_method ======》执行模块内测试类的某个方法(节点运行)

pytest test_file.py::TestClass ======》执行模块内某个测试类(节点运行)

pytest test_file.py::TestClass test_file2.py::test_mothod ======》多节点运行,中间用空格隔开

pytest -k pass ======》匹配用例名称的表达式,含有“pass”的被执行,其他的deselected

pytest -k "pass or fail" ======》组合匹配,含有“pass” 和 “fail”的被执行

pytest -k "not pass" ======》排除运行,不含“pass”的被执行

pytest -m finished ======》标记表达式,运行用@pytest.mark.finished 标记的用例

pytest -m "finished and not merged" ======》多个标记逻辑匹配,运行含有finished 不含 merged标记的用例

pytest -v ======》运行时显示详细信息

pytest -s ======》显示打印消息

pytest -x ======》遇到错误就停止运行

pytest -x --maxfail=2 ======》遇到两个错误就停止运行

pytest --setup-show ======》跟踪固件运行

pytest -v --reruns 5 --reruns-delay 1 ======》运行失败的用例间隔1s重新运行5次 pip install pytest-rerunfailures

pytest ======》多条断言,报错后,后面的依然执行, pip install pytest-assume,断言 pytest.assume(2==4)

pytest -n 3 ======》3个cpu并行执行测试用例,需保证测试用例可随机执行, pip install pytest-xdist分布式执行插件,多个cpu或主机执行

pytest -v -n auto ======》自动侦测系统里cpu的数目

pytest --count=2 ======》重复运行测试 pip install pytest-repeat

pytest --html=./report/report.html ======》生成报告,此报告中css是独立的,分享时会丢失样式,pip install pytest-html

pytest --html=report.html --self-containd-html ======》合并css到html报告中,除了passed所有行都被展开

pytest --durations=10 ======》获取最慢的10个用例的执行耗时


用例执行顺序控制

pytest 用例执行顺序默认是按字母顺序去执行,要控制执行顺序,需要安装插件 pytest-ordering:pip install pytest-ordering

在测试方法上加上装饰器:

@pytest.mark.last 最后一个执行

@pytest.mark.run(order=n) n=1则是第一个执行

Mark

标签的使用方法:

注册标签名 / 内置标签—> 在测试用例 / 测试类 / 模块文件 前面加上 @pytest.mark.标签名

注册方法:

1.在conftest.py 文件中添加代码

# 单个标签文件内容

def pytest_configure(config):

config.addinivalue_line("markers", "demo:demo标签名称")

# 多个标签文件内容

def pytest_configure(config):

marker_list = ["p0:p0级别用例", "p1:p1级别用例", "p2:p2级别用例"] # 标签名称

for markers in marker_list:

config.addinivalue_line("markers", markers)

2.项目中添加pytest.ini配置文件

[pytest]

markers =

p0:p0级别用例

p1:p1级别用例

p2:p2级别用例


使用方法:

import pytest

@pytest.mark.p0

def test_mark01():

print("函数级别的mark_p0")

@pytest.mark.p1

def test_mark02():

print("函数级别的mark_p1")

@pytest.mark.P2

class TestDemo:

def test_mark03(self):

print("mark_p2")

def test_mark04(self):

print("mark_p2")


运行方式:

命令行运行

pytest -m "p0 and p1"


文件运行

pytest.main(["-m", "P0", "--html=report.html"])


内置标签:

参数化:@pytest.mark.parametrize(argnames, argvalues)

无条件跳过用例:@pytest.mark.skip(reason=“xxx”)

有条件跳过用例:@pytest.mark.skipif(version < 0.3, reason = “not supported until 0.3”)

预测执行失败进行提示标记:@pytest.mark.xfail(version < 0.3, reason = “not supported until 0.3”),运行结果为X(通过xpassed,失败xfailed)

# 参数化

import hashlib

@pytest.mark.parametrize("x", list(range(10)))

def test_somethins(x):

time.sleep(1)

@pytest.mark.parametrize("passwd",["123456", "abcdefgfs", "as52345fasdf4"])

def test_passwd_length(passwd):

assert len(passwd) >= 8

@pytest.mark.parametrize('user, passwd',[('jack', 'abcdefgh'),('tom', 'a123456a')])

def test_passwd_md5(user, passwd):

db = {

'jack': 'e8dc4081b13434b45189a720b77b6818',

'tom': '1702a132e769a623c1adb78353fc9503'

}

assert hashlib.md5(passwd.encode()).hexdigest() == db[user]

# 如果觉得每组测试的默认参数显示不清晰,可以使用 pytest.param 的 id 参数进行自定义

@pytest.mark.parametrize("user, passwd",

[pytest.param("jack", "abcdefgh", id = "User<Jack>"),

pytest.param("tom", "a123456a", id = "User<Tom>")])

def test_passwd_md5_id(user, passwd):

db = {

'jack': 'e8dc4081b13434b45189a720b77b6818',

'tom': '1702a132e769a623c1adb78353fc9503'

}

assert hashlib.md5(passwd.encode()).hexdigest() == db[user]


Fixture

固件:是一些函数,pytest会在执行函数之前或者之后加载运行它们,相当于预处理和后处理。

fixture的目的是提供一个固定基线,在该基线上测试可以可靠地、重复的执行。

名称:默认为定义时的函数名,可以通过 @pytest.fixture(name="demo") 给fixture重命名

定义:在固件函数定义前加上@pytest.fixture();fixture是有返回值的,没return则返回None

使用:作为参数、使用usefixtures、自动执行(定义时指定autouse参数)

def test_demo(fixture_func_name)

@pytest.mark.usefixtures("fixture_func_name1", "fixture_func_name2") 标记函数或者类

预处理和后处理:用yield关键词,yield之前的代码是预处理,之后的是后处理

作用域:通过scope参数控制作用域

function:函数级,每个测试函数都会执行一次(默认)

class:类级别,每个测试类执行一次,所有方法都共享这个fixture

module:模块级别,每个模块.py执行一次,模块中所有测试函数、类方法 或者 其他fixture 都共享这个fixture

session:会话级别,每次会话只执行一次,一次会话中所有的函数、方法都共享这个fixture

集中管理:使用文件conftest.py 集中管理,在不同层级定义,作用于在其所在的目录和子目录,pytest会自动调用


scope、yield、auto的使用

# scope、yield、auto使用

@pytest.fixture(scope = "function", autouse=True)

def function_scope():

pass

@pytest.fixture(scope = "module", autouse=True)

def module_scope():

pass

@pytest.fixture(scope = "session")

def session_scope():

pass

@pytest.fixture(scope = "class", autouse=True)

def class_scope():

pass

import time

DATE_FORMAT = '%Y-%m-%d %H:%M:%S'

@pytest.fixture(scope='session', autouse=True)

def timer_session_scope():

start = time.time()

print('\nsession start: {}'.format(time.strftime(DATE_FORMAT, time.localtime(start))))

yield

finished = time.time()

print('\nsession finished: {}'.format(time.strftime(DATE_FORMAT, time.localtime(finished))))

print('session Total time cost: {:.3f}s'.format(finished - start))

def test_1():

time.sleep(1)

def test_2():

time.sleep(2)

'''

执行命令:pytest --setup-show -s

固件执行结果:

test_pytest_study.py

session start: 2020-04-16 17:29:02

SETUP S timer_session_scope

SETUP M module_scope

SETUP C class_scope

SETUP F function_scope

test_pytest_study.py::test_3 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope).

TEARDOWN F function_scope

TEARDOWN C class_scope

SETUP C class_scope

SETUP F function_scope

test_pytest_study.py::test_4 (fixtures used: class_scope, function_scope, module_scope, timer_session_scope).

TEARDOWN F function_scope

TEARDOWN C class_scope

TEARDOWN M module_scope

session finished: 2020-04-16 17:29:05

session Total time cost: 3.087s

TEARDOWN S timer_session_scope

'''


使用文件conftest.py 集中管理

# conftest.py

# conding=utf-8

import pytest

@pytest.fixture()

def postcode():

print("执行postcode fixture")

return "010"


# test_demo.py

# coding=utf-8

import pytest

class TestDemo():

def test_postcode(self, postcode):

assert postcode == "010"

if __name__=="__main__":

pytest.main(["--setup-show", "-s", "test_demo.py"])


python test_demo.py

执行过程:

test_demo.py 执行postcode fixture

SETUP F postcode

test_demo.py::TestDemo::test_postcode (fixtures used: postcode).

TEARDOWN F postcode


# 如果整个文件都用一个fixture,可以用pytestmark标记

pytestmark = pytest.mark.usefixtures("login")


fixture参数化

固件参数化需要使用pytest内置的固件request,并通过 request.param 获取参数。

# test_demo.py

@pytest.fixture(params=[

("user1", "passwd1"),

("user2", "passwd2")

])

def param(request):

return request.param

@pytest.fixture(autouse=True)

def login(param):

print("\n登录成功 %s %s" %param)

yield

print("\n退出成功 %s %s" %param)

def test_api():

assert 1 == 1

'''

pytest -s -v test_demo.py

运行结果:

test_demo.py::test_api[param0]

登录成功 user1 passwd1

PASSED

退出成功 user1 passwd1

test_demo.py::test_api[param1]

登录成功 user2 passwd2

PASSED

退出成功 user2 passwd2

'''


assert

assert "h" in "hello"

assert 3==4

assert 3!=4

assert f()==4

assert 5>6

assert not xx

assert {"0", "1", "2"} == {"0", "1", "2"}


相关推荐

墨尔本一华裔男子与亚裔男子分别失踪数日 警方寻人

中新网5月15日电据澳洲新快网报道,据澳大利亚维州警察局网站消息,22岁的华裔男子邓跃(Yue‘Peter’Deng,音译)失踪已6天,维州警方于当地时间13日发布寻人通告,寻求公众协助寻找邓跃。华...

网络交友须谨慎!美国犹他州一男子因涉嫌杀害女网友被捕

伊森·洪克斯克(图源网络,侵删)据美国广播公司(ABC)25日报道,美国犹他州一名男子于24日因涉嫌谋杀被捕。警方表示,这名男子主动告知警局,称其杀害了一名在网络交友软件上认识的25岁女子。雷顿警...

一课译词:来龙去脉(来龙去脉 的意思解释)

Mountainranges[Photo/SIPA]“来龙去脉”,汉语成语,本指山脉的走势和去向,现比喻一件事的前因后果(causeandeffectofanevent),可以翻译为“i...

高考重要考点:range(range高考用法)

range可以用作动词,也可以用作名词,含义特别多,在阅读理解中出现的频率很高,还经常作为完形填空的选项,而且在作文中使用是非常好的高级词汇。...

C++20 Ranges:现代范围操作(现代c++白皮书)

1.引言:C++20Ranges库简介C++20引入的Ranges库是C++标准库的重要更新,旨在提供更现代化、表达力更强的方式来处理数据序列(范围,range)。Ranges库基于...

学习VBA,报表做到飞 第二章 数组 2.4 Filter函数

第二章数组2.4Filter函数Filter函数功能与autofilter函数类似,它对一个一维数组进行筛选,返回一个从0开始的数组。...

VBA学习笔记:数组:数组相关函数—Split,Join

Split拆分字符串函数,语法Split(expression,字符,Limit,compare),第1参数为必写,后面3个参数都是可选项。Expression为需要拆分的数据,“字符”就是以哪个字...

VBA如何自定义序列,学会这些方法,让你工作更轻松

No.1在Excel中,自定义序列是一种快速填表机制,如何有效地利用这个方法,可以大大增加工作效率。通常在操作工作表的时候,可能会输入一些很有序的序列,如果一一录入就显得十分笨拙。Excel给出了一种...

Excel VBA入门教程1.3 数组基础(vba数组详解)

1.3数组使用数组和对象时,也要声明,这里说下数组的声明:'确定范围的数组,可以存储b-a+1个数,a、b为整数Dim数组名称(aTob)As数据类型Dimarr...

远程网络调试工具百宝箱-MobaXterm

MobaXterm是一个功能强大的远程网络工具百宝箱,它将所有重要的远程网络工具(SSH、Telnet、X11、RDP、VNC、FTP、MOSH、Serial等)和Unix命令(bash、ls、cat...

AREX:携程新一代自动化回归测试工具的设计与实现

一、背景随着携程机票BU业务规模的不断提高,业务系统日趋复杂,各种问题和挑战也随之而来。对于研发测试团队,面临着各种效能困境,包括业务复杂度高、数据构造工作量大、回归测试全量回归、沟通成本高、测试用例...

Windows、Android、IOS、Web自动化工具选择策略

Windows平台中应用UI自动化测试解决方案AutoIT是开源工具,该工具识别windows的标准控件效果不错,但是当它遇到应用中非标准控件定义的UI元素时往往就无能为力了,这个时候选择silkte...

python自动化工具:pywinauto(python快速上手 自动化)

简介Pywinauto是完全由Python构建的一个模块,可以用于自动化Windows上的GUI应用程序。同时,它支持鼠标、键盘操作,在元素控件树较复杂的界面,可以辅助我们完成自动化操作。我在...

时下最火的 Airtest 如何测试手机 APP?

引言Airtest是网易出品的一款基于图像识别的自动化测试工具,主要应用在手机APP和游戏的测试。一旦使用了这个工具进行APP的自动化,你就会发现自动化测试原来是如此简单!!连接手机要进行...

【推荐】7个最强Appium替代工具,移动App自动化测试必备!

在移动应用开发日益火爆的今天,自动化测试成为了确保应用质量和用户体验的关键环节。Appium作为一款广泛应用的移动应用自动化测试工具,为测试人员所熟知。然而,在不同的测试场景和需求下,还有许多其他优...

取消回复欢迎 发表评论: