当前位置:网站首页>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.安装依赖
边栏推荐
猜你喜欢
随机推荐
【一】TS安装编译配置自动生成.js文件
Numpy那些事
数字孪生园区场景中的坐标知识
JZ9 用两个栈实现队列
One article to understand DI dependency injection in php
编写一个油猴脚本
Antd-ProComponents中的EditableProTable无法在子行继续新增子子行的临时解决方案
8大软件供应链攻击事件概述
持续集成(三)Jenkins新增节点
QACTION_QA百科
持续集成(四)Jenkins配置报警机制
使用 LaunchPad 上的 TAO 工具包体验轻松创建 AI 模型
Summary of CNN classic models [easy to understand]
Oracle分析归档日志内容时,发现很多null?
Oracle 11g rac打完补丁,dbca新建数据库还需要执行应用补丁的sql吗?
es6 map使用场景
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
【Redis】连接报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused
uniapp引入vantUI库
更舒适更安全更时尚 凯翼2023款炫界正式上市