三十八、Python(pytest框架-上)

一、介绍

框架(framework):框架是为解决一类事情的功能集合。

pytest框架:pytest框架是单元测试框架,这是第三方框架想要使用必须要安装,可以使用pytest来作为自动化测试执行框架,用来管理测试用例、执行测试用例、生成测试报告。

使用框架的好处是:能够组织多个用例去执行,方便实现参数化,能够生成测试报告。

二、安装

在python中安装包,使用的工具是pip,只要安装了python,就会自动安装,在cmd中执行(注意使用管理员权限安装,否则终端可能不能直接使用)

python 复制代码
pip install pytest # 安装

# 默认下载的地址是国外的地址, 下载比较慢, 可以指定使用国内镜像下载
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
pip install -i 下载源 包名
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pytest

pip uninstall 包名 # 卸载
pip show 包名 # 查看
pip list # 查看所有安装的内容

三、pytest书写用例

pytest可以用来管理测试用例,使用pytest要注意几个点。

1.测试模块要以test命名开头,且测试类中不能有 init 方法,例如:test_login.py

2.测试类必须要以Test开头命名,例如:TestLogin

3.测试方法/测试函数必须以test开头,如:test_01_login()

python 复制代码
class TestDemo:

    def test_01_demo(self):
        print('测试1')

    def test_02_demo(self):
        print('测试2')

运行结果:

相关推荐
geovindu1 小时前
python: Face Recognition
开发语言·后端·python·人脸识别
2601_954811821 小时前
AI科学实验室MHS标准解读:AI智能体如何统一控制实验室设备接口
人工智能·python
一位正在转型AI全栈的前端工程师1 小时前
AI 全栈学习之旅 -Week 9:什么是AI Agent?从Function Calling到LangGraph
前端·python
用户0332126663672 小时前
使用 Python 为 PowerPoint 设置背景色和背景图【附代码示例】
python
ever_up9732 小时前
LangChain基础知识概述1
人工智能·python·langchain
asdzx672 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
zander2582 小时前
LeetCode 1143. 最长公共子序列
开发语言·python·算法
11路没有终点2 小时前
pytest Fixture 进阶:conftest 与依赖注入
pytest
轩情吖3 小时前
Python基础知识点完整总结
python·字符串·编程语言·函数·列表·元组·字典
青 春 记 忆3 小时前
零基础入门python69:为 FastAPI 项目构建可复现 Docker 镜像
python·fastapi·后端开发