CppUTest unit testing and mocking framework for C/C++
More information on the project page
Slack channel: Join if link not expired
You'll need to do the following to get started:
Building from source (Unix-based, Cygwin, MacOS):
git clone https://github.com/cpputest/cpputest.git
cd cpputest_build
autoreconf .. -i
../configure
make
You can use make install
if you want to install CppUTest system-wide.
You can also use CMake, which also works for Windows Visual Studio.
git clone https://github.com/cpputest/cpputest.git
cmake -B cpputest_build
cmake --build cpputest_build
Then to get started, you'll need to do the following:
CPPFLAGS += -I$(CPPUTEST_HOME)/include
CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
LD_LIBRARIES = -L$(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt
After this, you can write your first test:
TEST_GROUP(FirstTestGroup)
{
};
TEST(FirstTestGroup, FirstTest)
{
FAIL("Fail me!");
}
You can build and install cpputest using vcpkg dependency manager:
$ vcpkg install cpputest (More information: https://github.com/microsoft/vcpkg)
-h
help, shows the latest help, including the parameters we've implemented after updating this README page.-v
verbose, print each test name as it runs-r#
repeat the tests some number of times, default is one, default if # is not specified is 2. This is handy if you are experiencing memory leaks related to statics and caches.-s#
random shuffle the test execution order. # is an integer used for seeding the random number generator. # is optional, and if omitted, the seed value is chosen automatically, which results in a different order every time. The seed value is printed to console to make it possible to reproduce a previously generated execution order. Handy for detecting problems related to dependencies between tests.-g
group only run test whose group contains the substring group-n
name only run test whose name contains the substring name-f
crash on fail, run the tests as normal but, when a test fails, crash rather than report the failure in the normal wayTEST(group, name)
- define a testIGNORE_TEST(group, name)
- turn off the execution of a testTEST_GROUP(group)
- Declare a test group to which certain tests belong. This will also create the link needed from another library.TEST_GROUP_BASE(group, base)
- Same as TEST_GROUP
, just use a different base class than UtestTEST_SETUP()
- Declare a void setup method in a TEST_GROUP
- this is the same as declaring void setup()
TEST_TEARDOWN()
- Declare a void setup method in a TEST_GROUP
IMPORT_TEST_GROUP(group)
- Export the name of a test group so it can be linked in from a library. Needs to be done in main
.TEST_GROUP
may contain a setup
and/or a teardown
method.setup()
is called prior to each TEST
body and teardown()
is called after the test body.The failure of one of these macros causes the current test to immediately exit
CHECK(boolean condition)
- checks any boolean resultCHECK_TRUE(boolean condition)
- checks for trueCHECK_FALSE(boolean condition)
- checks for falseCHECK_EQUAL(expected, actual)
- checks for equality between entities using ==
. So if you have a class that supports operator==()
you can use this macro to compare two instances.STRCMP_EQUAL(expected, actual)
- check const char*
strings for equality using strcmp
LONGS_EQUAL(expected, actual)
- Compares two numbersBYTES_EQUAL(expected, actual)
- Compares two numbers, eight bits widePOINTERS_EQUAL(expected, actual)
- Compares two const void *
DOUBLES_EQUAL(expected, actual, tolerance)
- Compares two doubles within some toleranceENUMS_EQUAL_INT(excepted, actual)
- Compares two enums which their underlying type is int
ENUMS_EQUAL_TYPE(underlying_type, excepted, actual)
- Compares two enums which they have the same underlying typeFAIL(text)
- always failsTEST_EXIT
- Exit the test without failure - useful for contract testing (implementing an assert fake)Customize CHECK_EQUAL
to work with your types that support operator==()
SimpleString StringFrom(const yourType&)
The Extensions directory has a few of these.
Example of a main with a TestPlugin:
int main(int ac, char** av)
{
LogPlugin logPlugin;
TestRegistry::getCurrentRegistry()->installPlugin(&logPlugin);
int result = CommandLineTestRunner::RunAllTests(ac, av);
TestRegistry::getCurrentRegistry()->resetPlugins();
return result;
}
Memory leak detection
main
(or never).
To find out if a memory leak is due to lazy initialization set the -r
switch to run tests twice. The signature of this situation is that the first run shows leaks and the second run shows no leaks. When both runs show leaks, you have a leak to find.setup()
a memory usage checkpoint is recordedteardown()
another checkpoint is taken and compared to the original checkpointnew
, new[]
, delete
and delete[]
If you use some leaky code that you can't or won't fix you can tell a TEST to ignore a certain number of leaks as in this example:
TEST(MemoryLeakWarningTest, Ignore1)
{
EXPECT_N_LEAKS(1);
char* arrayToLeak1 = new char[100];
}
#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, char** av)
{
return RUN_ALL_TESTS(ac, av);
}
#include "CppUTest/TestHarness.h"
#include "ClassName.h"
TEST_GROUP(ClassName)
{
ClassName* className;
void setup()
{
className = new ClassName();
}
void teardown()
{
delete className;
}
};
TEST(ClassName, Create)
{
CHECK(0 != className);
CHECK(true);
CHECK_EQUAL(1,1);
LONGS_EQUAL(1,1);
DOUBLES_EQUAL(1.000, 1.001, .01);
STRCMP_EQUAL("hello", "hello");
FAIL("The prior tests pass, but this one doesn't");
}
There are some scripts that are helpful in creating your initial h, cpp, and Test files. See scripts/README.TXT
CppUTest is available through conan-center.
[requires]
cpputest/4.0
[generators]
cmake_find_package
cmake_paths
find_package(CppUTest REQUIRED)
add_executable(example_test ExampleTest.cpp)
target_link_libraries(example_test PRIVATE
CppUTest::CppUTest
CppUTest::CppUTestExt)
Sometimes you want to use CppUTest in your project without installing it to your system or for having control over the version you are using. This little snippet get the wanted version from GitHub and builds it as a library.
# CppUTest
include(FetchContent)
FetchContent_Declare(
CppUTest
GIT_REPOSITORY https://github.com/cpputest/cpputest.git
GIT_TAG master # or use release tag, eg. v4.0
)
# Set this to ON if you want to have the CppUTests in your project as well.
set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build")
FetchContent_MakeAvailable(CppUTest)
It can be used then like so:
add_executable(run_tests UnitTest1.cpp UnitTest2.cpp)
target_link_libraries(example_test PRIVATE
CppUTest::CppUTest
CppUTest::CppUTestExt)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。