QtTest Extension for VSCode Introducing QtTest Runner
Qt Test is a framework designed for unit testing applications and libraries built with Qt. It provides all the standard features commonly found in unit testing frameworks, making it easier to write unit tests for Qt-based projects.
We’re happy to introduce the QtTest Runner – a Visual Studio Code extension for running Qt Tests
via the test explorer pane.
The main difference from existing test extensions is that you can now run a single test slot instead of the whole executable. Also, unlike the ctest extension, we only build the target owning the test. There is no need for big rebuilds when debugging.
Other nice-to-haves include Go To Test, which opens the corresponding .cpp file directly from the test explorer:
And you can now execute a slot directly from the .cpp file editor, via context menu, by selecting the slot name:
Features
- Listing and running Qt tests
- Listing and running individual QTest test slots
- Context-menu entry to run selected slot in the text editor
- Build automatically before running
.cpp
file with the test’s implementation is automatically opened when there’s a failure- “Go to Test” available in the test explorer
Requirements
Only CMake is supported at this time, as test discovery is done via CTest.
You’ll need the official CMake extension. See our VSCode setup for Qt blog or just grab our template to get started quickly.
You’ll also need a C++ debugger extension. This is usually either ms-vscode.cpptools
or vadimcn.vscode-lldb
aka CodeLLDB
.
You’re expected to be using CMake’s add_test(). Confirm by running ctest
in your build directory.
Settings
KDAB.QtTest.debugger
By default the extension will try to guess what the best debugger is. But you can and probably should explicitly choose which debugger to use. The best setting might be Existing Launch, which will use an existing launch config, this way you can reuse your source maps, pretty printers and launch env variables. When reusing an existing launch, its program and args are replaced by the test you’re running.
KDAB.QtTest.CheckTestLinksToQtTestLib
Only available on Linux. Turn it on in case you have non-Qt tests executables that
you want to exclude from the list. It will invoke ldd
and see if libQtTest.so
is present. Patches are accepted for Windows and macOS support.
How it works
Test discovery is done by calling ctest --show-only=json-v1
, which lists all executables in a friendly JSON format. Then, for each
executable, we call -functions
which lists the available slots.
If you have a weird build system to support and you know of a nice test discovery approach, file an issue request and we’ll try to guide you for a contribution.
Known Issues
- Slots are not known at configure time, only after a build
- Not nice if you mix non-QtTests with QtTests, as there’s no ideal way to exclude tests,
however, on Linux we can call
ldd
and see if a test links toQtTest
.
Troubleshooting
In the output pane, choose KDAB-QtTests and see if there are any errors. Before reporting a bug, clear the output pane, reproduce the bug, and copy all output, paste it in the bug report.
Try pressing the reload button if the listing seems stale.
If no tests are reported, try running ctest -N inside the build directory. If that doesn’t work either then it’s a cmake
problem. Either there’s really no tests added with add_test()
or the enable_testing()
call is missing in the root CMakeLists.txt
.
If you like this article and want to read similar material, consider subscribing via our RSS feed.
Subscribe to KDAB TV for similar informative short video content.
KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.
That’s a great improvement for running tests in VSCode, I’m already curious to give it a try!