pytest

学习视频

https://www.bilibili.com/video/BV1py4y1t7bJ/?spm_id_from=333.337.search-card.all.click&vd_source=5d1d6a8d64d590dd81191ca4c12bf2f2

pytest插件

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

使用规范

1、模块名必须以test_开头或者_test结尾
2、测试必须以Test开头,并且不能有init方法
3、测试方法必须以test开头

运行

指定运行目录: pytest -vs testcase
指定运行方法: pytest -vs testcase/test_interface.py::test_04_func
多线程运行: pytest -vs testcase -n 2
失败重跑: pytest -vs testcase --reruns 2
跳过用例: pytest -vs testcase -k “ao” 只执行包含ao字符串的测试用例

执行顺序: 默认执行顺序从上往下,@pytest.mark.run(order=1) 指定执行顺序

配置运行规则,pytest.ini放在根目录下

[pytest]
addopts = -vs --html ./report/report.html   # 命令行的参数,用空格分隔
testpaths = ./testcase    # 测试用例执行的路径
python_files = test_*.py    # 模块名的规则
python_classes = Test*      # 配置执行的测试类名
python_functions = test     # 配置执行的函数名
markers = 
	smoke: smoke
	usermanage: usermanage

如果报unicodeDecodeError: ‘gbk’ … 需要把注释删掉,然后用notepad++将文件编码转为ANSI

分组执行:在对应函数上加装饰器@pytest.mark.smoke smoke自定义的,可以随意编写,跟配置文件中一致
执行命令为:`pytest -m “smoke or usermanage” # 运行smoke和usermanage装饰器的函数

跳过用例:
有条件跳过:@pytest.mark.skip(age<18, reason=“ad”)
无条件跳过:@pytest.mark.skip(reason=“ad”)

生成报告:加载配置文件addopts中

前后置和固件

setup() 每条用例执行之前都会执行一遍
setup_class() 所有用例执行之前只执行一次,如创建日志对象,创建数据库连接,创建接口的请求对象
teardown() 每条用例执行之后都会执行一遍
setup_class() 所有用例执行之后只执行一次,如销毁日志对象,销毁数据库连接,销毁接口的请求对象

class Test():
	def setup(self):
		print("aaaa")	
	def test_01(self):
		print("111")
	def test_02(self):
		print("333")
	def teardown(self):
		print("bbbb")

# 执行结果为  aaaa  111 bbbb   aaaa 333 bbbb

针对部分用例的前后置:@pytest.fixture(scope=‘fuction’, params=[“”], autouse=Fasle, ids=“”, name=“”)

参数详解:
scope:表示作用域,默认时fuction,可选class,module,package/session
params:参数化,支持list,tulp, dict
autouse:自动使用,全部方法使用,默认时False
ids:当使用params参数化时,给每一个值设置一个变量名,意义不大
name:给装饰的方法取别名

实例:

@pytest.fixture(scope="function")
def my_fixture():
	 print("这是前置")
	 yield 
	 print("这是后置")

class TestA:
	def test_01(self):
		print("222")
	def test_01(self, my_fixture):
	 	print("3333")
## 传参
@pytest.fixture(scope="function" params=["1", "2"])
def my_fixture(request):
	 print("这是前置")
	 yield request.param
	 print("这是后置")

class TestA:
	def test_01(self):
		print("222")
	# 会被执行两次
	def test_01(self, my_fixture):
	 	print("3333")

通过conftest.py和@pytest.fixture() 实现全局前置应用,如全局登录

conftest.py文件时单独存放的一个家具配置文件,名称不能更改。且要与运行的测试用例放到同一层级,不同层级都可以创建一个conftest.py

# conftest.py
@pytest.fixture(scope="function" params=["1", "2"])
def my_fixture(request):
	 print("这是前置")
	 yield request.param
	 print("这是后置")

添加好后,同一层级和下层级的测试用例就都可以使用该fixture了

断言 assert

assert 1==2

pytest+allure

下载: https://github.com/allure-framework/allure2/releases

[pytest]
addopts = -vs --alluredir ./temp   # 命令行的参数,用空格分隔
testpaths = ./testcase    # 测试用例执行的路径
python_files = test_*.py    # 模块名的规则
python_classes = Test*      # 配置执行的测试类名
python_functions = test     # 配置执行的函数名
markers = 
	smoke: smoke
	usermanage: usermanage
import os
import pytest

if __name__ == '__main__':
	pytest.main()
	os.system('allure generate ./temp -o ./report --clean')

数据驱动

import pytest

data_set = [(1, 1), (2, 1), (3, 1)]
 
@pytest.mark.parametrize("test_input,expected", data_set)
def test_is_equal(test_input, expected):
    assert is_equal(test_input, expected)

或者用方法返回参数

import pytest

def data_set():
	return [(1, 1), (2, 1), (3, 1)]
 
@pytest.mark.parametrize("test_input,expected", data_set)
def test_is_equal(test_input, expected):
    assert is_equal(test_input, expected)
  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值