当前位置:网站首页>Pytest study notes
Pytest study notes
2022-08-02 19:03:00 【RalohaD】
建议有unittestLearn the basics laterpytest
一.用例规范
pytestDefault use case specification:
1.用例文件:以test开头
2.以testThe function at the beginning will be treated as a test case
3.以TestClasses starting with class are considered test case classes
测试类中,以testThe method at the beginning will be treated as a test case
例:
def test_login():
assert 99 == 99
class TestLogin01:
def test_01(self):
assert 99 == 99
但是pytestThe use case specification of can be passedpytest.ini去配置
pytest.ini文件
[pytest]
python_files = test*.py
python_functions =
test*
*test
python_classes =
Test*
Check*
这里就配置了:
1.以testThe file at the beginning is the test file
2.函数名以test开头或testThe function at the end will be treated as a test case
3.以Test开头或者以CheckClasses that start with are considered test classes
二.The pre and post of the test class
pytestThere are two methods of pre and post
第一种:
use case level setup 和 teardown
def setup(self):
print('use case prepended')
def teardown(self):
print('用例后置')
类级别的 setup_class 和 teardown_class
def setup_class(self):
print('类前置')
def teardown_class(self):
print('类后置')
第二种:
pytestVery classic test fixture–pytest.fixture
@pytest.fixture()
def fix_01():
print('Use case level prepended')
yield
print('Use case level post')
@pytest.fixture(scope='class')
def fix_02():
print('类级别前置')
yield
print('类级别后置')
@pytest.fixture(autouse=True)
def fix_03():
print("Use case level prepended to be performed automatically")
yield
print("Use case level postfix for automatic execution")
调用时,in the parameters of the use case,Write the name of the method before and after it
def test_04(self, fix_01):
print('用例1')
assert 99 == 99
pytestYou can put all pre and post methods in conftest.py文件中(Use it directly in the use case file,不用导入)
三.pytest用例执行顺序
同一个文件按照用例文件中代码的前后顺序
多个文件根据文件名的ASCII码排序
四.Use case labeling
pytest.mark
1.在pytest.ini文件中markers这个配置项中注册标签
2.通过@pytest.mark.标签名,给用例加上标签
import pytest
class TestLogin:
@pytest.mark.opo
def test_01(self):
assert 100 == 100
def test_02(self):
assert 10 == 100
3.It can be passed when the use case is executedpytest -m 标签名 to filter the use case execution
4.When filtering use cases,可以使用 and or not
pytest -m"a and b":Execute concurrentlya和b标签的用例
pytest -m"a or b":执行有a或者b标签的用例
pytest -m"not a":执行没有a标签的用例
5.跳过
@pytest.mark.skip 直接跳过
@pytest.mark.skipif (表达式) Skip by condition
五.The way the use case runs
命令行:pytest 参数
pytest.main运行:pytest.main([‘参数列表’])
Filter executed use case files 或者 测试类 或者 用例方法
1.执行测试文件:pytest 文件名.py
2.执行测试类:pytest 文件名.py::类名
3.执行测试方法:pytest 文件名.py::类名::用例方法名
只执行test_Demo1的话
命令行进入test_Case目录,输入pytest test_Demo1.py
just executetest_Demo1文件了
只执行test_Demo1中的TestLogin方法的话,就输入pytest test_Demo1.py::TestLogin
六.断言
unittestThere are many assertion methods built in
pytestThe assertion is used directlyassert关键字
七.参数化
Parameterization isunittest中ddt
import pytest
class Test99:
cases = [11, 2, 23, 456, 7, 5, 44, 66, 77, 88, 99]
@pytest.mark.parametrize('item', cases)
def test_999(self, item):
assert item > 30
八.测试报告
allure报告
具体安装步骤:
1.在https://github.com/allure-framework/allure2/releases下载allure进行解压
2.将解压后的binThe path is placed in the system variablepath中
3.安装依赖
边栏推荐
猜你喜欢
随机推荐
关于我用iVX沉浸式体验了一把0代码项目创建
小心 transmittable-thread-local 的这个坑
接入网学习笔记
实时数仓架构演进及选型
Switch 块、Switch 表达式、Switch 模式匹配,越来越好用的 Switch
解析并执行 shell 命令
编写一个油猴脚本
工信部电子五所张志强:中国数据库行业发展趋势分析
Limit实现分页
研发了 5 年的时序数据库,到底要解决什么问题?
julia系列1:介绍与安装
MYSQL一站式学习,看完即学完
融云「 IM 进阶实战高手课」系列直播上线
内网渗透之kerberos认证(三)
【C语言刷题】指针入门三题|字符串长度、字符串复制、两数交换
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
尚硅谷尚品项目汇笔记(三)
亲戚3.5W入职华为后,我也选择了转行……
MySQL——慢查询日志分析
ActiveMQ漫谈(一)