本文档介绍了 face_recognition 库的测试框架和流程。它涵盖了测试套件的结构、如何运行测试以及正在测试的功能。本页面向希望为代码库做贡献或验证其更改没有破坏现有功能的开发者。
face_recognition 库使用 Python 内置的 unittest 框架进行测试。所有测试都组织在一个测试文件中,该文件验证了 API 和命令行接口的各个方面。
测试套件遵循标准结构,包含一个测试类,该类包含用于库不同组件的多个测试方法。
来源: tests/test_face_recognition.py22-344
有两种两种运行测试套件的方法
Tox 用于在多个 Python 版本(2.7、3.5、3.6、3.7、3.8)上运行测试,并使用 flake8 进行代码质量检查。这在 tox.ini 文件中配置。
使用 tox 运行所有测试
tox
这将为每个 Python 版本创建虚拟环境,并在每个环境中运行测试。
| 环境 | Python 版本 | 目的 |
|---|---|---|
| py27 | Python 2.7 | 核心功能测试 |
| py35 | Python 3.5 | 核心功能测试 |
| py36 | Python 3.6 | 核心功能测试 |
| py37 | Python 3.7 | 核心功能测试 |
| py38 | Python 3.8 | 核心功能测试 |
| flake8 | 不适用 | 代码质量检查 |
来源: tox.ini1-29
您也可以直接使用 Python 的 unittest 框架运行测试
python -m unittest discover
正如 tox.ini 配置中所指定的那样,tox 内部就是这样做的。
来源: tox.ini19-21
测试套件涵盖了 face_recognition 库的所有主要组件。以下是测试方法与其所测试的 API 函数的对应关系
来源: tests/test_face_recognition.py24-344
下表总结了 API 函数测试
| 测试方法 | API 功能 | 测试内容 |
|---|---|---|
test_load_image_file | api.load_image_file() | 加载 JPG 图像 |
test_load_image_file_32bit | api.load_image_file() | 加载 32 位 PNG 图像 |
test_raw_face_locations | api._raw_face_locations() | 基于 HOG 的人脸检测 |
test_cnn_raw_face_locations | api._raw_face_locations() | 基于 CNN 的人脸检测 |
test_face_locations | api.face_locations() | 公共人脸检测 API |
test_partial_face_locations | api.face_locations() | 部分人脸检测 |
test_raw_face_locations_batched | api._raw_face_locations_batched() | 批量人脸检测 |
test_batched_face_locations | api.batch_face_locations() | 公共批量检测 API |
test_raw_face_landmarks | api._raw_face_landmarks() | 底层特征点检测 |
test_face_landmarks | api.face_landmarks() | 带默认模型的公共特征点 API |
test_face_landmarks_small_model | api.face_landmarks() | 使用小型模型的特征点检测 |
test_face_encodings | api.face_encodings() | 生成人脸编码 |
test_face_encodings_large_model | api.face_encodings() | 使用大型模型的编码 |
test_face_distance | api.face_distance() | 计算人脸距离 |
test_compare_faces | api.compare_faces() | 比较人脸编码 |
来源: tests/test_face_recognition.py24-246
下表总结了命令行接口测试
| 测试方法 | CLI 组件 | 测试内容 |
|---|---|---|
test_command_line_interface_options | face_recognition_cli.main | CLI 帮助选项 |
test_command_line_interface | face_recognition_cli.main | 基本识别功能 |
test_command_line_interface_big_image | face_recognition_cli.main | 处理大型图像 |
test_command_line_interface_tolerance | face_recognition_cli.main | 容差参数 |
test_command_line_interface_show_distance | face_recognition_cli.main | 显示距离参数 |
test_fd_command_line_interface_options | face_detection_cli.main | 检测 CLI 帮助选项 |
test_fd_command_line_interface | face_detection_cli.main | 基本检测功能 |
test_fd_command_line_interface_folder | face_detection_cli.main | 处理图像文件夹 |
test_fd_command_line_interface_hog_model | face_detection_cli.main | HOG 模型选择 |
test_fd_command_line_interface_cnn_model | face_detection_cli.main | CNN 模型选择 |
来源: tests/test_face_recognition.py248-344
测试套件使用了位于 tests/test_images/ 目录中的一组测试图像。这些图像包括:
obama.jpg, obama2.jpg, obama3.jpg - 巴拉克·奥巴马的不同图像,用于测试识别功能biden.jpg - 乔·拜登的图像,用于测试区分不同人物obama_partial_face.jpg, obama_partial_face2.jpg - 部分人脸图像,用于边缘情况测试32bit.png - 32 位 PNG 图像,用于测试不同的图像格式来源: tests/test_face_recognition.py25-326
在为库添加新功能时,您还应该添加相应的测试。请遵循以下步骤
Test_face_recognition 类中添加一个新的测试方法test_ 开头示例测试结构
来源: tests/test_face_recognition.py22-345
测试套件已集成到 GitHub Actions 中,如 tox.ini 文件中所定义。这确保了在创建拉取请求或将代码推送到存储库时,所有测试都会自动运行。
来源: tox.ini11-16
CI 设置在多个 Python 版本上运行所有测试,并通过 flake8 检查确保代码质量。这有助于在不同环境中维护兼容性和代码标准。