Skip to content

Instantly share code, notes, and snippets.

@oranja
Last active July 15, 2017 08:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oranja/c032d7f77becca18137d49251e791593 to your computer and use it in GitHub Desktop.
Save oranja/c032d7f77becca18137d49251e791593 to your computer and use it in GitHub Desktop.
Run and monitor AutoTests in QtCreator (QTestLib)

The Qt framework offers Qt Test / QTestLib as a simple unit-testing framework for Qt-based projects. Perhaps too simple, as it comes with an annoying limitation that requires a separate executable for each test class, or creating a single test runner with a manually added call to each test suite. A quick search shows easy and helpful workarounds that offer to replace the manual work with a short #include and a macro. Then the test-runner remains untouched and finds all your test suites with these hints alone.

1]: http://qtcreator.blogspot.co.il/2009/10/running-multiple-unit-tests.html

2]: https://marcoarena.wordpress.com/2012/06/23/increase-your-qtest-productivity/

3]: https://github.com/e-j/qt-multiple-tests

In version 4.0 of QtCreator, the AutoTest plugin is made available to all, through the Community Edition of QtCreator. It offers a decent “Test Results” output pane that allows you to run and monitor the tests defined in the project. Problem is, it doesn't acknowledge the workarounds mentioned earlier, and so if you don't go the laborious way of creating a new project for every test suite, you don't get to integrate with the IDE. It's a pretty exclusive club membership, you see... Luckily, I found it very easy to modify the source of the AutoTest plugin to make it work with the workaround methods.

All you need to do is edit src/plugins/autotest/qtest/qttest_utils.h such that the line:

static QByteArrayList valid = {“QTEST_MAIN”, “QTEST_APPLESS_MAIN”, “QTEST_GUILESS_MAIN”};

Will also include the DECLARE_TEST macro (or TEST_DECLARE, depending on your chosen method):

static QByteArrayList valid = {“DECLARE_TEST”, “QTEST_MAIN”, “QTEST_APPLESS_MAIN”, “QTEST_GUILESS_MAIN”};

Building QtCreator is relatively simple. I’m leaving it up to you. After that you launch bin/qtcreator, open your project, go to Tools > Tests and select Rescan Tests. Then open the Test Results output pane (Alt + 9) and you can use the green “play” buttons to run all/selected tests and monitor their results. (This is all assuming that you have the AutoTest plugin enabled, of course)

ps. I’d have shared the final shared library, but it seems to have to be compiled separately for each QtCreator version.

pps. I :love: open-source.

@martonb
Copy link

martonb commented Jun 1, 2017

I actually created an issue on the project, as it would be nice to get a solution directly with Qt Creator for the people who just download and install it (not building it from sources). See: pasccom/QTestLibPlugin#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment