Skip to content

集成测试

本主题介绍如何运行(以及扩展)基于 ROS 的 PX4 集成测试。

INFO

编写新测试时,建议使用 MAVSDK 集成测试。仅在 需要 ROS 的用例(例如避障)中使用基于 ROS 的集成测试框架。

所有 PX4 集成测试都由我们的持续集成系统自动执行。

ROS / MAVROS 测试

进行这些测试需要以下组件:

执行测试

要运行 MAVROS 测试,请执行以下步骤:

sh
source <catkin_ws>/devel/setup.bash
cd <PX4-Autopilot_clone>
make px4_sitl_default sitl_gazebo
make <test_target>

test_target 是以下 makefile 目标之一:tests_missiontests_mission_coveragetests_offboardtests_avoidance

也可以直接运行位于 test/ 目录下的测试脚本来执行测试:

sh
source <catkin_ws>/devel/setup.bash
cd <PX4-Autopilot_clone>
make px4_sitl_default sitl_gazebo
./test/<test_bash_script> <test_launch_file>

例如:

sh
./test/rostest_px4_run.sh mavros_posix_tests_offboard_posctl.test

也可以按照以下方式运行完整的测试套件:

sh
# 开始仿真
cd <Firmware_clone>
source integrationtests/setup_gazebo_ros.bash $(pwd)
roslaunch px4 mavros_posix_sitl.launch

# 运行测试(在新的 shell 中):
cd <Firmware_clone>
source integrationtests/setup_gazebo_ros.bash $(pwd)
rosrun px4 mavros_new_test.py

.test 文件会启动 integrationtests/python_src/px4_it/mavros/ 中定义的相应 Python 测试。

编写新的 MAVROS 测试(Python)

本节介绍如何使用 ROS 1/MAVROS 编写新的 Python 测试、进行测试并将其添加到 PX4 测试套件中。

我们建议你参考现有的测试作为示例和灵感来源(integrationtests/python_src/px4_it/mavros/)。官方 ROS 文档也包含了如何使用 unittest 的信息(本测试套件基于此)。

要编写新的测试,请按以下步骤操作:

  1. 通过复制以下空测试框架来创建一个新的测试脚本:
python
#!/usr/bin/env python
# [... 许可证信息 ...]

#
# @author 示例作者 <author@example.com>
#
PKG = 'px4'

import unittest
import rospy
import rosbag

from sensor_msgs.msg import NavSatFix

class MavrosNewTest(unittest.TestCase):
    """
    测试描述
    """

    def setUp(self):
        rospy.init_node('test_node', anonymous=True)
        rospy.wait_for_service('mavros/cmd/arming', 30)

        rospy.Subscriber("mavros/global_position/global", NavSatFix, self.global_position_callback)
        self.rate = rospy.Rate(10) # 10hz
        self.has_global_pos = False

    def tearDown(self):
        pass

    #
    # 测试中使用的通用回调函数
    #
    def global_position_callback(self, data):
        self.has_global_pos = True

    def test_method(self):
        """测试方法描述"""

        # FIXME: 临时解决方法,等待仿真准备好
        while not self.has_global_pos:
            self.rate.sleep()

        # TODO: 执行测试

if __name__ == '__main__':
    import rostest
    rostest.rosrun(PKG, 'mavros_new_test', MavrosNewTest)
  1. 仅运行新测试
    • 启动仿真器:
sh
cd <PX4-Autopilot_clone>
source Tools/simulation/gazebo/setup_gazebo.bash
roslaunch launch/mavros_posix_sitl.launch
- 运行测试(在新的 shell 中):
sh
cd <PX4-Autopilot_clone>
source Tools/simulation/gazebo/setup_gazebo.bash
rosrun px4 mavros_new_test.py
  1. 将新的测试节点添加到启动文件中
    • test/ 目录下创建一个新的 <test_name>.test ROS 启动文件。
    • 使用基础脚本 rostest_px4_run.shrostest_avoidance_run.sh 调用测试文件。
  2. (可选)在 Makefile 中创建一个新目标
    • 打开 Makefile。
    • 搜索 Testing 部分。
    • 添加一个新的目标名称并调用测试。 例如:
sh
tests_<new_test_target_name>: rostest
    @"$(SRC_DIR)"/test/rostest_px4_run.sh mavros_posix_tests_<new_test>.test

按照上述说明运行测试。