!REDIRECT “https://docs.px4.io/master/ja/test_and_ci/integration_testing_mavsdk.html

Integration Testing using MAVSDK

PX4 can be tested end to end to using integration tests based on MAVSDK.

The tests are primarily developed against SITL for now and run in continuous integration (CI). However, they are meant to generalize to real tests eventually.

Install the MAVSDK C++ Library

The tests need the MAVSDK C++ library installed system-wide (e.g. in /usr/lib or /usr/local/lib).

Install either from binaries or source:

Prepare PX4 Code

To build the PX4 code, use:

  1. DONT_RUN=1 make px4_sitl gazebo mavsdk_tests

Run All PX4 Tests

To run all SITL tests as defined in sitl.json, do:

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

To see all possible command line arguments, check out:

  1. test/mavsdk_tests/mavsdk_test_runner.py -h
  2. usage: mavsdk_test_runner.py [-h] [--log-dir LOG_DIR] [--speed-factor SPEED_FACTOR] [--iterations ITERATIONS] [--abort-early] [--gui] [--model MODEL]
  3. [--case CASE] [--debugger DEBUGGER] [--verbose]
  4. config_file
  5. positional arguments:
  6. config_file JSON config file to use
  7. optional arguments:
  8. -h, --help show this help message and exit
  9. --log-dir LOG_DIR Directory for log files
  10. --speed-factor SPEED_FACTOR
  11. how fast to run the simulation
  12. --iterations ITERATIONS
  13. how often to run all tests
  14. --abort-early abort on first unsuccessful test
  15. --gui display the visualization for a simulation
  16. --model MODEL only run tests for one model
  17. --case CASE only run tests for one case
  18. --debugger DEBUGGER choice from valgrind, callgrind, gdb, lldb
  19. --verbose enable more verbose output

Notes on implementation

  • The tests are invoked from the test runner script mavsdk_test_runner.py, which is written in Python. This runner also starts px4 as well as Gazebo for SITL tests, and collects the logs of these processes.
  • The test runner is a C++ binary It contains:
    • The main function to parse the arguments.
    • An abstraction around MAVSDK called autopilot_tester.
    • The actual tests using the abstraction around MAVSDK as e.g. test_multicopter_mission.cpp.
    • The tests use the catch2 unit testing framework. The reasons for using this framework are:
      • Asserts (REQUIRE) which are needed to abort a test can be inside of functions (and not just in the top level test as is the case with gtest).
      • Dependency management is easier because catch2 can just be included as a header-only library.
      • Catch2 supports tags, which allows for flexible composition of tests.

Terms used:

  • “model”: This is the selected Gazebo model, e.g. iris.
  • “test case”: This is a catch2 test case.