Best practice for unit testing mixed Swift/C++ package

Hello. I have Swift package that is mostly built in C++ with a Swift wrapper.

What is the best way to unit test the internal C++ classes? I see that that the swift-lang/swift project itself uses gtest. But I would rather not have to introduce CMake or change the build system as I would like to stick with SwiftPM and keep it as simple as possible.

My current thought it to vendor some header-only C++ unit testing framework. Would this integrate with SwiftPM test targets?

A related question - is there a contact that SwiftPM expects test targets to conform to? I imagine at a minimum the exit code should be non-zero on test failure. Are there other expectations regarding stdout/stderr for example?

Any insight appreciated!

Imo you should test only the public facing Swift API.

Tests for it have to be written anyway and when calling the Swift API, you call the underlying C++ code, if you were to test the C++ code separately, you would double-test it. Once directly, once via the public API.

1 Like

The Swift API tests would be akin to integration tests. I think it's normal to have unit tests for individual classes too.

The surface area of the Swift API is quite small compared to the actual functionality of the library. There are dozens of internal classes that are not exposed through Swift, so it's not one to one.