当前位置:网站首页>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.安装依赖
边栏推荐
猜你喜欢
随机推荐
numpy的学习笔记
解析并执行 shell 命令
【Redis】连接报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused
接入网学习笔记
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
一文搞懂│php 中的 DI 依赖注入
锁定和并发控制(四)
周末看点回顾|亚马逊将于2023年底关闭Amazon Drive网盘服务;千寻位置发布时空智能六大底层自研技术…
【二】通过props进行传值,子页面多种方式接收
exness:欧元区经济意外向好,欧元震荡蓄势等待突破
JWT原理详解_电磁感应现象原理
Mysql 查询语句中where字段= '' 作用是什么 ?如何实现多条件查询
低光数据集
Arduino 硬件编程语言基础学习入门
nacos集群配置详解
总结:不同语言比较总结
亲戚3.5W入职华为后,我也选择了转行……
提高测试覆盖率的四大步骤
从Oracle日志解析学习数据库内核原理
每日练习------定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出;









