C++ Unit Testing?

,

I’m looking for guidance on how to use unit tests in the C++ GradleRIO build environment.

I get the impression that there may be support for building and running native (ie on the host platform) unit tests. I’ve found some mention of JUnit, but I’ve also seen references to Google Test.

How can we add our own (non-robot) unit tests that would build and run on the host?

Thanks!

Boy do I feel silly. I’m so focused on the src folder, I totally skipped over the test folder.

Guess, I know where to start now.

:thinking:

I guess I do need some direction.

$ ./gradlew test

Task 'test' not found in root project 'myProject'.

$ ./gradlew build

succeeds without building or running the test.

Thanks for some help.

The build command does actually run the tests, but there must be actual tests for it to run. The check command will do it as well. test is something specific to Java.

Thanks,

The build command does actually run the tests, but there must be actual tests for it to run.

I guess this is where I’m confused. I changed main.cpp in test/cpp in the project.

First, I added an test that will fail. No failure. So I added a #error to the file. Clearly the file is not compiled.

I’d like to add tests. How do I do that? (I don’t even see where gtest has been downloaded as part of the project.).

Thanks for the help!

When you created the project, did you enable desktop support? It’s a checkbox, and without it checked testing won’t work. To enable it, you can go into your build.gradle and around the middle there is a line saying enableDesktopSupport = false (or something like that). Set that to true, it will enable that. And Google test is added as one of the useLibrary parameters in the testing binary. You would have to follow Google tests instructions how to write tests, we don’t really have a guide to do that.

def includeDesktopSupport = true

Now set to true!

That seems to have done it!

Now I have compiler errors.

I’m on Mac OS X. Is there a specific compiler that is recommended? There’s nothing in the log that says which compiler is being used.

Looks like the default g++ is Apple LLVM version 10.0.0 (clang-1000.11.45.5)

So close …

You need xcode (the latest version). Also know most vendors do not support desktop, so you’ll need to use conditional compilation to remove any vendor lib usage on desktop. You can use the define __FRC_ROBORIO__ to detect if you’re compiling for the Rio or not. What other compiling errors are you getting?

That’s the xcode compiler (I’m pretty sure).

The compiler errors are in the CANSpark code base. Looks like we’ll have to work around that.

Looks like I’m on the right track now!