Writing tests#
AUI uses GTest as the tests framework and provides a simple way to use it in your application.
It also includes GMock library.
GoogleTest is Google's C++ testing and mocking framework. This user's guide has the following contents:
- GoogleTest Primer - Teaches you how to write simple tests using GoogleTest. Read this first if you are new to GoogleTest.
- GoogleTest Advanced - Read this when you've finished the Primer and want to utilize GoogleTest to its full potential.
- GoogleTest Samples - Describes some GoogleTest samples.
- GoogleTest FAQ - Have a question? Want some tips? Check here first.
- Mocking for Dummies - Teaches you how to create mock objects and use them in tests.
- Mocking Cookbook - Includes tips and approaches to common mocking use cases.
- Mocking Cheat Sheet - A handy reference for matchers, actions, invariants, and more.
- Mocking FAQ - Contains answers to some mocking-specific questions.
In AUI#
In order to enable tests for your project, call aui_enable_tests(<target-name>)
in the end of your CMakeLists.txt
and create tests/
dir along with src/
and CMakeLists.txt
. Place your test suites inside the tests/
directory.
Usage Example#
Suppose we want to test the sum
function of the following program:
main.cpp | |
---|---|
Possible output:
Project structure#
To write a test, enable tests for your projects first. Add aui_enable_tests(<target-name>)
to the end of your
CMakeLists.txt
:
CMakeLists.txt | |
---|---|
|
and create the tests/
dir along with CMakeLists.txt
and src/
. Inside tests/
, create MyTest.cpp
:
Note the main.h
file. Since we would like to access the sum
function of main.cpp
, we should put sum
declaration to the main.h
file:
Put the basic test case to MyTest.cpp
:
MyTest.cpp | |
---|---|
Build Tests
and run bin/Tests
. Possible output:
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN ] MyTest.Sum
[ OK ] MyTest.Sum (0 ms)
[----------] 1 test from MyTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
AUI does the following fundamental things for you:
- Collect all tests from your
tests/
dir - Provide an entry point for the
Tests
program (testing::InitGoogleTest) - Mirror all linked libraries and compile options to the
Tests
program - If you are testing a shared library, the shared library is linked to the
Tests
program - If you are testing an executable, the executable's sources are automatically compiled with the
Tests
program and theinclude
scope of the executable is copied to theTests
program (this is why we have easily accessed themain.h
header)
Writing UI Tests#
Since AUI is a UI framework it is also capable of UI tests.
In addition to the previous example, we'd create some window:
UITest.cpp | |
---|---|
|