Cotest is an extension of Google Test with fully integrated support for coroutines:
- as contexts within which to run checking code and actions,
- and to execute the code under test.
Cotest allows for a linear test case layout, in which the test coroutine launches the code under test, and then accepts and returns mock calls in a single sequence of C++ statements. Cotest can do this without needing a data structure populated with expected future events. The user directly implements the test coroutine and can insert arbitrary logic to decide how to respond to mock calls and results from code under test, without needing to split the code into multiple lambda actions.
Cotest supports matching on both watches (similar to GMock expectations) and inside the coroutine body (a test coroutine can drop a mock call and GMock's matching search will continue). Cotest actions are coded directly into the coroutine body by the user. Cardinality is inferred from the execution state of the coroutine.
Cotest supports multiple test coroutines: these will see mock calls with priority established by their watches, which may be interleaved with GMock expectations. Cotest permits multiple overlapping launches of code under test: in the coroutine model there is no freely concurrent execution, but a test coroutine can control the order in which mock calls are allowed to return.
This project is a fork of Google Test, and changes to gtest/gmock source code have been kept minimal. C++14.
Cotest supports test coroutines, mocking integrated with Google Mock, and launch coroutines. This completes development phases 1 and 2, bringing Cotest to concept demo status. Three further phases remain:
- Test failure reporting: non-fatal failures integrated into Google Mock, more file/line info, and fixes to cardinality messages.
- Also exceptions to/from code-under-test.
- Alternate coroutine backends, to include Boost coroutines and maybe C++20. Cotest currently uses coroutines built on threads.
- Thread safety - focussing on the case where launched code-under-test starts a thread which makes mock calls.
User documentation comes in three parts:
For background on the idea of coroutine-based testing, see
- Discussion: Testing with Two Computers for background on coroutine-based testing
In lieu of a formal API specification, see
- the public API header file cotest.h
- A code sample that (mis-)uses a mutex: cotest-mutex.cc.
- Examples of serverised testing style: cotest-serverised.cc.
- Using Cotest with GMock's
MockFunction<>: cotest-mockfunction.cc. - Complex matching example with commentry: cotest-wild.cc (see
StackedCoros).
There are many more examples in test/
Cotest currently only supports CMake. From repo top level:
mkdir build
cd build
cmake -Dcoro_build_tests=ON -Dgtest_build_tests=ON -Dgtest_build_samples=ON -Dgmock_build_tests=ON -DCMAKE_BUILD_TYPE=Debug -DGOOGLETEST_VERSION=0 -DCOROUTINES_VERSION=0 ../
make
ctest
This will run the googletest tests as well as the cotest tests. Use -R ^co for just the cotest tests.