Skip to content

使用 MAVSDK 进行集成测试

PX4 可以使用基于 MAVSDK 的集成测试进行端到端测试。

目前主要针对软件在环(SITL)开发测试,并在持续集成(CI)中运行。不过,它们最终旨在推广到实际测试。

测试需要将 MAVSDK C++ 库安装到系统目录(例如: /usr/lib/usr/local/lib)。

系统必备组件

运行所有 PX4 测试

可进行预安装或源码安装:

  • 安装 LinuxmacOS 的开发工具链(不支持 Windows)。需要安装 Gazebo Classic,且它应默认安装。
  • 获取 PX4 源代码
    sh
    git clone https://github.com/PX4/PX4-Autopilot.git --recursive
    cd PX4-Autopilot

为测试构建 PX4

使用以下命令构建 PX4 源码:

sh
DONT_RUN=1 make px4_sitl gazebo-classic mavsdk_tests

安装 MAVSDK C++ 库

测试需要将 MAVSDK C++ 库系统级安装(例如安装到 /usr/lib/usr/local/lib)。

要查看所有可用的命令行参数,运行:

准备 PX4 源码

要运行 sitl.json 中定义的所有 SITL 测试,执行以下命令:

sh
test/mavsdk_tests/mavsdk_test_runner.py test/mavsdk_tests/configs/sitl.json --speed-factor 10

这将列出所有测试,然后按顺序运行它们。

要查看所有可能的命令行参数,使用 -h 参数:

sh
test/mavsdk_tests/mavsdk_test_runner.py -h

用法:mavsdk_test_runner.py [-h] [--log-dir LOG_DIR] [--speed-factor SPEED_FACTOR] [--iterations ITERATIONS] [--abort-early] [--gui] [--model MODEL]
                             [--case CASE] [--debugger DEBUGGER] [--verbose]
                             config_file

位置参数:
  config_file         使用的 JSON 配置文件

可选参数:
  -h, --help          显示此帮助信息并退出
  --log-dir LOG_DIR   日志文件目录
  --speed-factor SPEED_FACTOR
                      模拟运行的速度因子
  --iterations ITERATIONS
                      在首次失败的测试中运行所有测试的频率
  --abort-early       提前中止
  --gui               显示模拟的可视化界面
  --model MODEL       只为一个模型运行测试
  --case CASE         只运行一个测试用例
  --debugger DEBUGGER 调试器:callgrind, gdb, lldb
  --verbose           启用更详细的输出

运行单个测试

通过在命令行选项中指定 model 和测试 case 来运行单个测试。例如,要测试倾转翼飞行器执行任务飞行,可以运行:

sh
test/mavsdk_tests/mavsdk_test_runner.py test/mavsdk_tests/configs/sitl.json --speed-factor 10 --model tailsitter --case 'Fly VTOL mission'

找出当前模型集及其相关测试用例的最简单方法是如上所示运行所有 PX4 测试(注意,如果只想测试一个,可以随后取消构建)。

在撰写本文时,运行所有测试生成的列表如下:

sh
即将为 3 个选定模型运行 39 个测试用例(1 次迭代):
  - iris:
    - 'Land on GPS lost during mission (baro height mode)'
    - 'Land on GPS lost during mission (GPS height mode)'
    - 'Continue on mag lost during mission'
    - 'Continue on baro lost during mission (baro height mode)'
    - 'Continue on baro lost during mission (GPS height mode)'
    - 'Continue on baro stuck during mission (baro height mode)'
    - 'Continue on baro stuck during mission (GPS height mode)'
    - 'Takeoff and Land'
    - 'Fly square Multicopter Missions including RTL'
    - 'Fly square Multicopter Missions with manual RTL'
    - 'Fly straight Multicopter Mission'
    - 'Offboard takeoff and land'
    - 'Offboard position control'
    - 'Fly forward in position control'
    - 'Fly forward in altitude control'
  - standard_vtol:
    - 'Land on GPS lost during mission (baro height mode)'
    - 'Land on GPS lost during mission (GPS height mode)'
    - 'Continue on mag lost during mission'
    - 'Continue on baro lost during mission (baro height mode)'
    - 'Continue on baro lost during mission (GPS height mode)'
    - 'Continue on baro stuck during mission (baro height mode)'
    - 'Continue on baro stuck during mission (GPS height mode)'
    - 'Takeoff and Land'
    - 'Fly square Multicopter Missions including RTL'
    - 'Fly square Multicopter Missions with manual RTL'
    - 'Fly forward in position control'
    - 'Fly forward in altitude control'
  - tailsitter:
    - 'Land on GPS lost during mission (baro height mode)'
    - 'Land on GPS lost during mission (GPS height mode)'
    - 'Continue on mag lost during mission'
    - 'Continue on baro lost during mission (baro height mode)'
    - 'Continue on baro lost during mission (GPS height mode)'
    - 'Continue on baro stuck during mission (baro height mode)'
    - 'Continue on baro stuck during mission (GPS height mode)'
    - 'Takeoff and Land'
    - 'Fly square Multicopter Missions including RTL'
    - 'Fly square Multicopter Missions with manual RTL'
    - 'Fly forward in position control'
    - 'Fly forward in altitude control'

实现说明

  • 测试由测试运行脚本 mavsdk_test_runner.py 调用,该脚本用 Python 编写。除了 MAVSDK,此运行器还会为 SITL 测试启动 px4 以及 Gazebo,并收集这些进程的日志。
  • 测试运行器是一个 C++ 二进制文件,包含:
    • main 函数用于解析参数。
    • 围绕 MAVSDK 的抽象层,称为 autopilot_tester
    • 实际的测试使用围绕 MAVSDK 的抽象层,例如 test_multicopter_mission.cpp
    • 测试使用 catch2 单元测试框架。使用此框架的原因如下:
      • 断言 (REQUIRE) 可用于函数内部,需要中止测试时可以使用(而不像 gtest 那样只能在顶级测试中使用)。
      • 依赖管理更简单,因为 catch2 可以作为仅头文件库包含。
      • Catch2 支持 标签,这允许灵活组合测试。

相关术语解释:

  • “model”:这是选定的 Gazebo 模型,例如 iris
  • “test case”:这是一个 catch2 测试用例