ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

So I figured out what was wrong, and I suppose that most of you out there would consider this a "beginner's mistake" however due to some reasons I will explain below, I think that this can be considered an "answer":

The answer to my issue was sort-of provided in this answer and later sort-of-but-closer-to provided in this other answer however in my lacking of a thorough understanding of CMake, gtest, catkin, etc, I missed a crucial detail which lead me to a lot of frustration. I say "sort-of" in these cases because, although they are correct, they do not help users who, say, don't simply need a reminder that they already knew the answer. Since I can't imagine that I am the only one out there who will ever find themselves in such a position, and since this ROS documentation for gtest is practically useless for anyone who doesn't already know everything they need to know about linking, I feel it necessary to explain why I had the above-described issue and how I solved it. (in my defense, I am a self-taught Mech E pretending to be a software engineer, which is to be read: if it's something I ought to know, I probably don't know it yet).

Leading up to writing the code that caused me so much pain, I had very little experience in proper unit testing save for some tutorials that I had followed which went smoothly and were simple. As a result I overlooked the fact that the test code for the tutorials was all in the form of libraries, and my above-posted snippets show that in the form of me trying to test a library that did not exist. My lack of understanding lead me to believe that all I had to do was include the header for the code that I wanted to test and everything would be hunky dory, I was wrong.

If you are having similar problems as me, check if the code you want to test is even available as a library to the test code.

I remedied this by adding the following to my CMakelists after the catkin_package() section and before the include_directories() section:

add_library(scatter_phidgets
    src/scatter_phidgets.cpp)
target_link_libraries(scatter_phidgets ${catkin_LIBRARIES})

Furthermore, and as mentioned in this answer and this other answer, you have to link your test executable against the library you create above. Although I admit I am still not 100% on what the background mechanics of the linking are, for me this consisted of changing this:

if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(utest test/utest.cpp)
  target_link_libraries(utest ${catkin_LIBRARIES})
endif()

And modifying it so that it looks like this:

if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(utest test/utest.cpp)
  target_link_libraries(utest ${catkin_LIBRARIES} scatter_phidgets)
endif()

After doing so catkin_make run_tests and catkin run_tests (now I am using catkin_tools I guess) not only successfully built my tests, but showed me that I had placed some unnecessary code in the function I was trying to test by failing the test! (which I considered a huge success, here's to TDD!)

Additional reasons why this should be allowed as an answer:

  1. Because the gtest ROS page completely leaves this out, assumes that you already know much of this, and fails to provide a simplest working example.
  2. Because you can write an entire ROS package to do whatever you want, and it will be successful and build and solve world hunger and tie your shoes for you but it won't warn you that if you want to test the slew of ROS-agnostic functions you are bound to write, that you need them in a library.