Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ntest for unit testing on NuttX #12106

Closed
wants to merge 4 commits into from
Closed

Conversation

julianoes
Copy link
Contributor

@julianoes julianoes commented May 29, 2019

This brings the capability to run gtest tests on NuttX.

I looked at various ways to get the gtest tests onto NuttX/Pixhawks:

  • Get the the C++ STL libcxx to run on NuttX and build gtest. I got this working but it's quite messy and will need further work and discussions.
  • Use another C++ testing framework such as cpputest or [catch2(https://github.com/catchorg/Catch2/blob/master/single_include/catch2/catch.hpp), however both seem to use the C++ STL like gtest.
  • Use some C unit testing harness. I didn't find this too useful or exciting, so I skipped that.
  • Replace the minimal functionality of gtest that we actually require. That's what I tried and called ntest.

Currently we support ASSERT_EQ, EXPECT_EQ, ASSERT_TRUE, EXPECT_TRUE, ASSERT_FALSE, EXPECT_FALSE.

The output looks like this for failures:

nsh> ntest all
[==========] Running 9 tests from 3 test cases.
[----------] 1 test from TakeoffTest
[ RUN      ] TakeoffTest.Initialization
/home/julianoes/src/Firmware/src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp:47 Failure
Expected equality of these values:
 foo
   Which is: 42
 99
   Which is: 99
[  FAILED  ] TakeoffTest.Initialization
[----------] 1 test from TakeoffTest
[----------] 1 test from AttitudeControlTest
[ RUN      ] AttitudeControlTest.AllZeroCase
[       OK ] AttitudeControlTest.AllZeroCase
[----------] 1 test from AttitudeControlTest
[----------] 7 tests from Hysteresis
[ RUN      ] Hysteresis.TakeChangeBack
[       OK ] Hysteresis.TakeChangeBack
[ RUN      ] Hysteresis.ChangeAfterMultipleSets
[       OK ] Hysteresis.ChangeAfterMultipleSets
[ RUN      ] Hysteresis.HysteresisChanged
[       OK ] Hysteresis.HysteresisChanged
[ RUN      ] Hysteresis.ChangeAfterTime
[       OK ] Hysteresis.ChangeAfterTime
[ RUN      ] Hysteresis.Zero
[       OK ] Hysteresis.Zero
[ RUN      ] Hysteresis.InitTrue
[       OK ] Hysteresis.InitTrue
[ RUN      ] Hysteresis.InitFalse
/home/julianoes/src/Firmware/src/lib/hysteresis/HysteresisTest.cpp:54 Failure
Value of: hysteresis.get_state()
 Actual: false
Expected: true
[  FAILED  ] Hysteresis.InitFalse
[----------] 7 tests from Hysteresis
[==========] 9 tests from 3 test cases ran.
[  PASSED  ] 7 tests.
[  FAILED  ] 2 tests, listed below:
[  FAILED  ] TakeoffTest.Initialization
[  FAILED  ] Hysteresis.InitFalse

And if successful:

nsh> ntest all
[==========] Running 9 tests from 3 test cases.
[----------] 1 test from TakeoffTest
[ RUN      ] TakeoffTest.Initialization
[       OK ] TakeoffTest.Initialization
[----------] 1 test from TakeoffTest
[----------] 1 test from AttitudeControlTest
[ RUN      ] AttitudeControlTest.AllZeroCase
[       OK ] AttitudeControlTest.AllZeroCase
[----------] 1 test from AttitudeControlTest
[----------] 7 tests from Hysteresis
[ RUN      ] Hysteresis.TakeChangeBack
[       OK ] Hysteresis.TakeChangeBack
[ RUN      ] Hysteresis.ChangeAfterMultipleSets
[       OK ] Hysteresis.ChangeAfterMultipleSets
[ RUN      ] Hysteresis.HysteresisChanged
[       OK ] Hysteresis.HysteresisChanged
[ RUN      ] Hysteresis.ChangeAfterTime
[       OK ] Hysteresis.ChangeAfterTime
[ RUN      ] Hysteresis.Zero
[       OK ] Hysteresis.Zero
[ RUN      ] Hysteresis.InitTrue
[       OK ] Hysteresis.InitTrue
[ RUN      ] Hysteresis.InitFalse
[       OK ] Hysteresis.InitFalse
[----------] 7 tests from Hysteresis
[==========] 9 tests from 3 test cases ran.
[  PASSED  ] 9 tests.

@julianoes
Copy link
Contributor Author

Please review @dagar @bkueng.

@dagar
Copy link
Member

dagar commented Jun 12, 2019

This looks promising, I'd just like to see if we can do it without the linking and module ordering hacks.

@julianoes
Copy link
Contributor Author

This looks promising, I'd just like to see if we can do it without the linking and module ordering hacks.

Do you have an idea how? Otherwise this will probably rot here.

@dagar
Copy link
Member

dagar commented Jun 14, 2019

FYI @LukasWoodtli

@julianoes
Copy link
Contributor Author

Note to myself: I should change this to have separate binaries and then list them at build time for the test script to run them using serial:
https://github.com/PX4/Firmware/blob/master/Tools/HIL/run_tests.py

julianoes added 3 commits July 2, 2019 16:04
This adds a small testing library which mimicks gtest to be used on
NuttX where we currently can't use the C++ STL.
This adds the ntest runner using the ntestlib. It's a simple harness
mimicking the gtest test runner for NuttX.
This adds the ntest systemcmd using ntestlib to the build. This is the
equivalent of gtest testing on Posix for NuttX.

This works as follows:
1. At cmake configure time all tests are added to global cmake lists.
2. Then when ntest is configured (last), the collected test sources and
   dependencies are added to the ntest build.

When compiling for NuttX we need to include ntestlib.h, when building
for Posix we need to include gtest.h (see px4_unittesting.h).
@stale
Copy link

stale bot commented Oct 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

@julianoes
Copy link
Contributor Author

As discussed on the dev call we would rather want to use gtest using STL on NuttX in the future, closing.

@julianoes julianoes closed this Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants