Getting started with unit tests in Qt Creator and Catch
2017-06-20 11:00
525 查看
转自
https://dragly.org/2015/11/24/getting-started-with-unit-tests-in-qt-creator-with-catch/
I have written about unit testing in Qt Creator on multiple occasions earlier.
Since then, a new testing framework seems to have become a new leader in the field, and that is Catch. It is similar to UnitTest++ and Google's
gtest and brings the best of both worlds. It is header-only and appears to reduce the amount of code that must be written in comparison
to the other two.
This is an adopted version my previous posts that shows how to use Catch in a Qt Creator project. It doesn't any longer integrate the testing output into Qt Creator's issue pane. Instead I've started to rely on Travis
CI or Jenkins to run the tests and notify me about any newly introduced errors.
An example project using this code structure has been posted on Github by Filip Sund (thanks, Filip!) and further adapted by us
with the move to Catch.
The structure of the project is as follows:
The main project file, MyProject.pro will now be based on a subdirs template, and may look like this:
The app.depends and tests.depends statements makes sure that the src project is compiled before the application and tests. This is because the src directory contains the library that will be used by both the app and the tests.
CONFIG+=ordered is needed because depends doesn't affect the order during make install.
Each of the other .pro files will include defaults.pri to have all the headers available. defaults.pri contains the following:
If the library, main program and tests use common libraries, it is very useful to have the defaults.pri define these dependencies too.
In the src folder, I have myclass.cpp, which is the class that I want to use and test. The src.pro needs to compile to a library, so that it may be used both by app and tests, and could look something like this:
What this class does is not so interesting, it could be anything. A simple example could be this header file:
With this accompaning source file:
I only have a main.cpp file in app, because the app is basically just something that uses everything in the src folder. It will depend on the shared compiled library from src, and app.pro would look something like this:
The main.cpp file could be a simple program that uses MyClass:
In the tests folder I have simply added a main.cpp which will run the tests. Then tests.pro has the following contents:
Which now links to the myapp library in addition to the unit tests.
The main.cpp in tests file which could contain the following, if we were to use UnitTest++ as our testing library:
This test will fail because my implementation of MyClass::addition is completely wrong:
Note that I'm including MyClass by through <myclass.h> which is possible because of the INCLUDEPATH variable in defaults.pri.
This should help you define a project that compiles a library, as well as tests and an application using the library.
https://dragly.org/2015/11/24/getting-started-with-unit-tests-in-qt-creator-with-catch/
I have written about unit testing in Qt Creator on multiple occasions earlier.
Since then, a new testing framework seems to have become a new leader in the field, and that is Catch. It is similar to UnitTest++ and Google's
gtest and brings the best of both worlds. It is header-only and appears to reduce the amount of code that must be written in comparison
to the other two.
This is an adopted version my previous posts that shows how to use Catch in a Qt Creator project. It doesn't any longer integrate the testing output into Qt Creator's issue pane. Instead I've started to rely on Travis
CI or Jenkins to run the tests and notify me about any newly introduced errors.
An example project using this code structure has been posted on Github by Filip Sund (thanks, Filip!) and further adapted by us
with the move to Catch.
The structure of the project is as follows:
MyProject ├─ MyProject.pro ├─ defaults.pri ├─ app/ │ ├─ app.pro │ └─ main.cpp ├─ src/ │ ├─ src.pro │ └─ myclass.cpp └─ tests/ ├─ tests.pro └─ main.cpp
The main project file, MyProject.pro will now be based on a subdirs template, and may look like this:
TEMPLATE = subdirs CONFIG+=ordered SUBDIRS = src app tests app.depends = src tests.depends = src
The app.depends and tests.depends statements makes sure that the src project is compiled before the application and tests. This is because the src directory contains the library that will be used by both the app and the tests.
CONFIG+=ordered is needed because depends doesn't affect the order during make install.
defaults.pri
Each of the other .pro files will include defaults.pri to have all the headers available. defaults.pri contains the following:INCLUDEPATH += $$PWD/src SRC_DIR = $$PWD
If the library, main program and tests use common libraries, it is very useful to have the defaults.pri define these dependencies too.
./src
In the src folder, I have myclass.cpp, which is the class that I want to use and test. The src.pro needs to compile to a library, so that it may be used both by app and tests, and could look something like this:include(../defaults.pri) CONFIG -= qt TARGET = myapp TEMPLATE = lib SOURCES += myclass.cpp HEADERS += myclass.h
What this class does is not so interesting, it could be anything. A simple example could be this header file:
#ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: double addition(double a, double b); }; #endif // MYCLASS_H
With this accompaning source file:
#include "myclass.h" double MyClass::addition(double a, double b) { return a * b; }
./app
I only have a main.cpp file in app, because the app is basically just something that uses everything in the src folder. It will depend on the shared compiled library from src, and app.pro would look something like this:include(../defaults.pri) TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp LIBS += -L../src -lmyapp
The main.cpp file could be a simple program that uses MyClass:
#include <myclass.h> #include <iostream> using namespace std; int main() { MyClass adder; cout << adder.addition(10, 20) << endl; return 0; }
./tests
In the tests folder I have simply added a main.cpp which will run the tests. Then tests.pro has the following contents:include(../defaults.pri) TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp LIBS += -L../src -lmyapp
Which now links to the myapp library in addition to the unit tests.
The main.cpp in tests file which could contain the following, if we were to use UnitTest++ as our testing library:
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file #include "catch.hpp" #include <myclass.h> TEST_CASE( "MyMath", "[mymath]" ) { SECTION("Addition") { MyClass my; REQUIRE(my.addition(3,4) == 7); } }
This test will fail because my implementation of MyClass::addition is completely wrong:
class MyClass { public: double addition(double a, double b) { return a * b; } };
Note that I'm including MyClass by through <myclass.h> which is possible because of the INCLUDEPATH variable in defaults.pri.
This should help you define a project that compiles a library, as well as tests and an application using the library.
相关文章推荐
- Lesson1:Getting Started with TinyOS and nesC
- Getting Started with DFC and DQL
- Getting started with JVM Memory model and GC
- how to deal with "no such file error or diretory" error for a new programmer in QT creator
- Getting started with ASP.NET AJAX in Visual Web Developer
- Getting Started with Kinect and Processing
- Getting Started With Ruby and Rails
- VTK Installation and Configuration with Qt in Ubuntu
- Getting Started with Eclipse and Counterclockwise
- Getting Started with NHibernate and ASP.NET MVC- CRUD Operations
- Getting Started With Ruby and Rails
- Getting Started with Java Management Extensions (JMX): Developing Management and Monitoring Solutions
- Getting Started with Java and Bluetooth (From www.java.net)
- Getting Started With IronRuby And RSpec
- Getting Started With Node.js and mongoDB
- Getting Started With Ruby and Rails
- Essential Silverlight: Getting started now with Microsofts new cross-platform plug-in for rich inte
- Getting Started with Java Management Extensions (JMX): Developing Management and Monitoring Solution
- Getting Started with Eclipse and the SWT
- Getting started with SQLite in C#