当前位置: 首页 > news >正文

做网站绿标外贸网站电子建设

做网站绿标,外贸网站电子建设,宁波网站推广软件,seo推广的优缺点以下是对 ​pytest 官方 Tutorial​ 的详细解析,结合代码示例和关键概念说明,帮助你逐步掌握 pytest 的核心功能。​1. 安装与运行第一个测试​安装pip install -U pytest验证安装:pytest --version第一个测试文件创建文件 test_sample.py&am…

以下是对 ​pytest 官方 Tutorial​ 的详细解析,结合代码示例和关键概念说明,帮助你逐步掌握 pytest 的核心功能。


1. 安装与运行第一个测试

安装

pip install -U pytest

验证安装:

pytest --version

第一个测试文件

创建文件 test_sample.py

# test_sample.py
def func(x):return x + 1def test_func():assert func(3) == 4  # 断言:验证 func(3) 的结果是否为 4

运行测试

pytest test_sample.py

输出解析​:

  • pytest 会自动发现以 test_ 开头的文件(如 test_sample.py)和函数(如 test_func)。
  • 若断言失败,会显示详细的错误信息(如实际值和期望值的差异)。

2. 断言(Assertions)​

pytest 直接使用 Python 的 assert 语句,无需复杂的断言方法。例如:

def test_integers():a = 5b = 6assert a + 1 == b  # 验证 a+1 是否等于 bdef test_strings():s = "hello"assert s.upper() == "HELLO"  # 验证字符串大写转换

关键点​:

  • 断言失败时,pytest 会显示失败的具体表达式(如 assert 5 + 1 == 7 会明确提示 6 != 7)。

3. 测试类(Test Classes)​

将相关测试组织到类中,类名需以 Test 开头,方法名以 test_ 开头:

# test_class.py
class TestClass:def test_one(self):x = "this"assert "h" in x  # 验证字符串包含字符 'h'def test_two(self):x = "hello"assert hasattr(x, "check")  # 验证对象是否有 'check' 属性(预期失败)

运行方式​:

pytest test_class.py -v  # -v 显示详细输出

输出示例​:

test_class.py::TestClass::test_one PASSED
test_class.py::TestClass::test_two FAILED

4. 跳过测试(Skipping Tests)​

使用 @pytest.mark.skip 跳过特定测试:

# test_skip.py
import pytest@pytest.mark.skip(reason="暂时跳过此测试")
def test_skip_example():assert False  # 此测试会被跳过,不会执行

运行结果​:

test_skip.py::test_skip_example SKIPPED (暂时跳过此测试)

5. 参数化测试(Parametrization)​

通过 @pytest.mark.parametrize 对同一测试函数传入多组参数:

# test_parametrize.py
import pytest@pytest.mark.parametrize("a, b, expected", [(1, 2, 3),(4, 5, 9),(10, 20, 30),
])
def test_add(a, b, expected):assert a + b == expected

运行结果​:

test_parametrize.py::test_add[1-2-3] PASSED
test_parametrize.py::test_add[4-5-9] PASSED
test_parametrize.py::test_add[10-20-30] PASSED

优势​:避免重复代码,覆盖多组输入场景。


6. Fixture(测试夹具)​

Fixture 用于提供测试所需的资源(如数据库连接、临时文件等),通过 @pytest.fixture 定义:

# test_fixture.py
import pytest@pytest.fixture
def sample_data():return [1, 2, 3]  # 提供测试数据def test_sum(sample_data):assert sum(sample_data) == 6  # 使用 fixture 提供的数据

运行方式​:

pytest test_fixture.py -v

关键点​:

  • Fixture 可以跨多个测试函数复用。
  • 支持作用域(如 scope="module" 表示整个模块共享一个 Fixture 实例)。

7. 临时目录与文件(tmp_path Fixture)​

pytest 内置 tmp_path Fixture,用于创建临时目录和文件:

# test_tmp_path.py
def test_create_file(tmp_path):d = tmp_path / "subdir"  # 创建子目录d.mkdir()p = d / "hello.txt"      # 创建文件p.write_text("Hello, pytest!")assert p.read_text() == "Hello, pytest!"assert len(list(tmp_path.iterdir())) == 1  # 验证目录内容

用途​:测试文件操作时无需清理临时文件。


8. 捕获异常(Exception Testing)​

使用 pytest.raises 验证代码是否抛出预期异常:

# test_exception.py
import pytestdef test_zero_division():with pytest.raises(ZeroDivisionError):  # 验证是否抛出 ZeroDivisionError1 / 0

扩展​:还可检查异常的具体信息:

with pytest.raises(ValueError) as excinfo:int("xyz")
assert "invalid literal" in str(excinfo.value)

9. 标记(Markers)​

通过标记(如 @pytest.mark.slow)分类测试,选择性运行:

# test_markers.py
import pytest@pytest.mark.slow
def test_slow_function():import timetime.sleep(2)assert True# 运行时指定标记
# pytest -m slow  # 仅运行标记为 slow 的测试
# pytest -m "not slow"  # 排除 slow 测试

10. 配置文件(pytest.ini)​

通过配置文件 pytest.ini 自定义 pytest 行为(如设置默认命令行选项):

# pytest.ini
[pytest]
addopts = -v -ra  # 默认启用详细输出 (-v) 和失败重试 (-ra)
testpaths = tests  # 指定测试目录
http://www.dtcms.com/a/448395.html

相关文章:

  • 免费试用网站怎么做如何做营销
  • 做电脑壁纸的网站app 开发软件
  • 买了个服务器 怎么做网站多网站建设
  • 做网站的主要内容政务微网站建设方案
  • 网站建设未来发展的趋势济南网络优化
  • 如何自己创造一个网站平台公司网站做门户备案
  • 服装网站模板免费下载用dw制作个人网页
  • 东莞做网站一般多少钱手机百度搜索
  • 东阳建设公司网站电子商务网站建设与维护 试卷
  • 广州做一个网站多少钱国外哪个网站做c 挣钱
  • 网站要素的优化设计出售网站建设群
  • 几个做ppt的网站知乎给我推荐一个网站
  • asp电影网站源码python做视频点播网站
  • 用超轻粘土做网站宣传片制作费用
  • 网站开发公司基本业务流程图永州网站建设效果
  • 网站变慢的原因软件开发公司网站设计
  • 呼和浩特网站制作公司计算机作业做网站
  • 网站的结构布局建设网站需要花费多少钱
  • 创客贴网页设计网站中国旅游预订网站的建设始于哪一年
  • 有了域名如何做网站深圳网站开发平台
  • 网站吸引力办公空间设计说明范文
  • 看设计作品的网站软件福州网站建设培训
  • 镇江专业建网站官方网站建设 磐石网络知名
  • 做民宿需要和多家网站合作吗佛山企业推广哪家好
  • wap网站开发价格wordpress 首页模版
  • 在线网页游戏网站wordpress 数据库账号
  • 个人网站设计成品下载帮人做网站在徐州被敲诈五万
  • 厦门网站制作套餐做网站后付款
  • 萧山做网站的企业企联网登录
  • 电子商务网站建设与管理期末考试试卷a深圳网站设计公司哪家工艺好