Writing Unit Tests for vtol_att_control

Hello everyone :wave:,

I am working with the vtol flight stack and I would like to have some unit tests and sitl tests for my CI. I followed the instructions in the docs to set up the files but I am getting stuck with initializing a simple tiltrotor so I could test the functions, e.g. update_transiton_state(). I started with a real simple Test:

#include <gtest/gtest.h>
#include <wico_att_control/tiltrotor.h>

TEST(VtolAttitudeControlTest, AllZeroCase)
{
	VtolAttitudeControl *vtol_att_controller{};
	Tiltrotor tiltrotor(vtol_att_controller);
	EXPECT_EQ(0.0f, 0.0f);
}

The thing is, that the terminal spits out a lot of undefined refrences like ‘PX4_TICKS_PER_SEC’. Is it even possible to write a unit test for vtol_att_control? Or do I have to use sitl tests because it highly depends on other modules?

Sorry in case its a real dumb question :see_no_evil: I am trying to dive into the depths of PX4 and try to learn as much as I can.

Kind regards,

1 Like

That’s a good question.

Have a look at how other unit tests like that are made:

Or this:

@JulianOes Thanks for pointing out these tests. I already looked at the Test for the Commander and for the MC PositionControl before. I think the Tiltrotor class is somehow similar to the ObstacleAvoidance class, but I can still not manage to initiate my class I can not see why :frowning:
Here is the simple vtol_att_control_test.cpp:

#include <gtest/gtest.h>
#include <vtol_att_control/tiltrotor.h>

TEST(VtolAttitudeControlTest, instantiation)
{
    Tiltrotor tiltrotor(nullptr);
    EXPECT_EQ(0.0f, 0.0f);
} 

And this is the terminal output after trying to run my classes:

Also tried to replace nullptr with a pointer to an instance of VtolAttitudeControl, but it didn’t work either. Do I need to include some dependencies in order for the tiltrotor instance to work?

Thanks,

Ok, are you adding it in cmake like this?

1 Like

:see_no_evil: I was still using px4_add_unit_gtest() instead of the functional version… Thanks for your help to find my stupid errors :roll_eyes:

But there is still one issue left: If the module, which I would like to write a test for, is out-of-tree, the function px4_add_functional_gtest() is not found. Do you also know how to configure those tests if they are out of tree? Maybe I have to adjust the px4_add_functional_gtest() function?

Oh ok. I didn’t realize there were different cmake functions!

Maybe you need something like this your out-of-tree CMakeLists.txt:

I tried it but sadly it doesn’t work. The function px4_add_functional_gtest() is found, but the test is not added to the test cases when running make tests.

I also checked how the EXTERNAL parameter works in the px4_add_module() function, but I didn’t get much smarter out of it either.

Ok, that’s odd. I think you have to look into the px4_add_gtest function works and debug the content of the list of unit tests which it creates.