菜单

测试

相关源文件

本文档介绍了 face_recognition 库的测试框架和流程。它涵盖了测试套件的结构、如何运行测试以及正在测试的功能。本页面向希望为代码库做贡献或验证其更改没有破坏现有功能的开发者。

测试框架概述

face_recognition 库使用 Python 内置的 unittest 框架进行测试。所有测试都组织在一个测试文件中,该文件验证了 API 和命令行接口的各个方面。

测试结构

测试套件遵循标准结构,包含一个测试类,该类包含用于库不同组件的多个测试方法。

来源: tests/test_face_recognition.py22-344

运行测试

有两种两种运行测试套件的方法

使用 tox

Tox 用于在多个 Python 版本(2.7、3.5、3.6、3.7、3.8)上运行测试,并使用 flake8 进行代码质量检查。这在 tox.ini 文件中配置。

使用 tox 运行所有测试

tox

这将为每个 Python 版本创建虚拟环境,并在每个环境中运行测试。

环境Python 版本目的
py27Python 2.7核心功能测试
py35Python 3.5核心功能测试
py36Python 3.6核心功能测试
py37Python 3.7核心功能测试
py38Python 3.8核心功能测试
flake8不适用代码质量检查

来源: tox.ini1-29

直接使用 unittest

您也可以直接使用 Python 的 unittest 框架运行测试

python -m unittest discover

正如 tox.ini 配置中所指定的那样,tox 内部就是这样做的。

来源: tox.ini19-21

测试覆盖

测试套件涵盖了 face_recognition 库的所有主要组件。以下是测试方法与其所测试的 API 函数的对应关系

来源: tests/test_face_recognition.py24-344

API 测试

下表总结了 API 函数测试

测试方法API 功能测试内容
test_load_image_fileapi.load_image_file()加载 JPG 图像
test_load_image_file_32bitapi.load_image_file()加载 32 位 PNG 图像
test_raw_face_locationsapi._raw_face_locations()基于 HOG 的人脸检测
test_cnn_raw_face_locationsapi._raw_face_locations()基于 CNN 的人脸检测
test_face_locationsapi.face_locations()公共人脸检测 API
test_partial_face_locationsapi.face_locations()部分人脸检测
test_raw_face_locations_batchedapi._raw_face_locations_batched()批量人脸检测
test_batched_face_locationsapi.batch_face_locations()公共批量检测 API
test_raw_face_landmarksapi._raw_face_landmarks()底层特征点检测
test_face_landmarksapi.face_landmarks()带默认模型的公共特征点 API
test_face_landmarks_small_modelapi.face_landmarks()使用小型模型的特征点检测
test_face_encodingsapi.face_encodings()生成人脸编码
test_face_encodings_large_modelapi.face_encodings()使用大型模型的编码
test_face_distanceapi.face_distance()计算人脸距离
test_compare_facesapi.compare_faces()比较人脸编码

来源: tests/test_face_recognition.py24-246

CLI 测试

下表总结了命令行接口测试

测试方法CLI 组件测试内容
test_command_line_interface_optionsface_recognition_cli.mainCLI 帮助选项
test_command_line_interfaceface_recognition_cli.main基本识别功能
test_command_line_interface_big_imageface_recognition_cli.main处理大型图像
test_command_line_interface_toleranceface_recognition_cli.main容差参数
test_command_line_interface_show_distanceface_recognition_cli.main显示距离参数
test_fd_command_line_interface_optionsface_detection_cli.main检测 CLI 帮助选项
test_fd_command_line_interfaceface_detection_cli.main基本检测功能
test_fd_command_line_interface_folderface_detection_cli.main处理图像文件夹
test_fd_command_line_interface_hog_modelface_detection_cli.mainHOG 模型选择
test_fd_command_line_interface_cnn_modelface_detection_cli.mainCNN 模型选择

来源: 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

添加新测试

在为库添加新功能时,您还应该添加相应的测试。请遵循以下步骤

  1. 确定您正在测试的功能的哪个方面
  2. Test_face_recognition 类中添加一个新的测试方法
  3. 使用描述性的方法名称,以 test_ 开头
  4. 使用适当的断言来验证预期行为
  5. 运行测试以确保您的新测试通过

示例测试结构

来源: tests/test_face_recognition.py22-345

持续集成

测试套件已集成到 GitHub Actions 中,如 tox.ini 文件中所定义。这确保了在创建拉取请求或将代码推送到存储库时,所有测试都会自动运行。

来源: tox.ini11-16

CI 设置在多个 Python 版本上运行所有测试,并通过 flake8 检查确保代码质量。这有助于在不同环境中维护兼容性和代码标准。