Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing in QTestLib - running single test / tests in class / all tests

I'm just starting to use QTestLib. I have gone through the manual and tutorial. Although I understand how to create tests, I'm just not getting how to make those tests convenient to run. My unit test background is NUnit and MSTest. In those environments, it was trivial (using a GUI, at least) to alternate between running a single test, or all tests in a single test class, or all tests in the entire project, just by clicking the right button.

All I'm seeing in QTestLib is either you use the QTEST_MAIN macro to run the tests in a single class, then compile and test each file separately; or use QTest::qExec() in main() to define which objects to test, and then manually change that and recompile when you want to add/remove test classes.

I'm sure I'm missing something. I'd like to be able to easily:

  • Run a single test method
  • Run the tests in an entire class
  • Run all tests

Any of those would call the appropriate setup / teardown functions.

EDIT: Bounty now available. There's got to be a better way, or a GUI test runner that handles it for you or something. If you are using QtTest in a test-driven environment, let me know what is working for you. (Scripts, test runners, etc.)

like image 400
Dave Mateer Avatar asked Apr 21 '10 20:04

Dave Mateer


People also ask

Should unit tests only test one class?

Good practices for writing testsUnit tests should test one method only. This allows you to easily identify what failed if the test fails. Unit tests should not be coupled together, therefore one unit test CANNOT rely on another unit test having completed first.

What is single unit testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.

Should unit tests be run in parallel?

Running unit tests in parallel can significantly improve the speed at which they run. However, you have to make sure that one test does not affect another in any way. Else your tests are green most of the time, but sometimes one or more tests will fail.


2 Answers

You can run only selected test cases (test methods) by passing test names as command line arguments :

myTests.exe myCaseOne myCaseTwo

It will run all inits/cleanups too. Unfortunately there is no support for wildcards/pattern matching, so to run all cases beginning with given string (I assume that's what you mean by "running the tests in an entire class"), you'd have to create script (windows batch/bash/perl/whatever) that calls:

myTests.exe -functions

parses the results and runs selected tests using first syntax.

To run all cases, just don't pass any parameter:

myTests.exe
like image 143
chalup Avatar answered Oct 01 '22 13:10

chalup


The three features requested by the OP, are nowadays integrated in to the Qt Creator.

The project will be automatically scanned for tests and they apear on the Test pane. Bottom left in the screenshot:

Qt Creator screenshot

Each test and corresponding data can be enabled by clicking the checkbox. The context menu allows to run all tests, all tests of a class, only the selected or only one test. As requested.

The test results will be available from the Qt Creator too. A color indicator will show pass/fail for each test, along additional information like debug messages.

In combination with the Qt Creator, the use of the QTEST_MAIN macro for each test case will work well, as each compiled executable is invoked by the Qt Creator automatically.

For a more detailed overview, refer to the Running Autotests section of the Qt Creator Manual.

like image 33
Jan L. Avatar answered Oct 01 '22 11:10

Jan L.