Giter Site home page Giter Site logo

cpputest / cpputest Goto Github PK

View Code? Open in Web Editor NEW
1.3K 1.3K 496.0 11.62 MB

CppUTest unit testing and mocking framework for C/C++

Home Page: http://cpputest.github.io

License: BSD 3-Clause "New" or "Revised" License

Makefile 3.70% Shell 1.26% CMake 2.40% C++ 82.81% C 4.05% Ruby 1.07% PowerShell 0.51% Batchfile 0.15% M4 4.04% sed 0.01%
c-plus-plus cpputest memory-leak mocking-framework test-driven-development unit-testing very-kewl

cpputest's Introduction

CppUTest

GitHub Actions AppveyorBuild status Coverage Status ConanCenter package

CppUTest unit testing and mocking framework for C/C++

More information on the project page

Slack channel: Join if link not expired

Getting Started

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
mkdir cpputest_build
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
cd cpputest
mkdir cpputest_build
cmake -B cpputest_build
cmake --build cpputest_build

Then to get started, you'll need to do the following:

  • Add the include path to the Makefile. Something like:
    • CPPFLAGS += -I$(CPPUTEST_HOME)/include
  • Add the memory leak macros to your Makefile (needed for additional debug info!). Something like:
    • CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
    • CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h
  • Add the library linking to your Makefile. Something like:
    • 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)

Command line switches

  • -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 way

Test Macros

  • TEST(group, name) - define a test
  • IGNORE_TEST(group, name) - turn off the execution of a test
  • TEST_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 Utest
  • TEST_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.

Set up and tear down support

  • Each 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.

Assertion Macros

The failure of one of these macros causes the current test to immediately exit

  • CHECK(boolean condition) - checks any boolean result
  • CHECK_TRUE(boolean condition) - checks for true
  • CHECK_FALSE(boolean condition) - checks for false
  • CHECK_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 numbers
  • BYTES_EQUAL(expected, actual) - Compares two numbers, eight bits wide
  • POINTERS_EQUAL(expected, actual) - Compares two const void *
  • DOUBLES_EQUAL(expected, actual, tolerance) - Compares two doubles within some tolerance
  • ENUMS_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 type
  • FAIL(text) - always fails
  • TEST_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==()

  • Create the function: SimpleString StringFrom(const yourType&)

The Extensions directory has a few of these.

Building default checks with TestPlugin

  • CppUTest can support extra checking functionality by inserting TestPlugins
  • TestPlugin is derived from the TestPlugin class and can be inserted in the TestRegistry via the installPlugin method.
  • TestPlugins can be used for, for example, system stability and resource handling like files, memory or network connection clean-up.
  • In CppUTest, the memory leak detection is done via a default enabled TestPlugin

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

  • A platform specific memory leak detection mechanism is provided.
  • If a test fails and has allocated memory prior to the fail and that memory is not cleaned up by TearDown, a memory leak is reported. It is best to only chase memory leaks when other errors have been eliminated.
  • Some code uses lazy initialization and appears to leak when it really does not (for example: gcc stringstream used to in an earlier release). One cause is that some standard library calls allocate something and do not free it until after 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.

How is memory leak detection implemented?

  • Before setup() a memory usage checkpoint is recorded
  • After teardown() another checkpoint is taken and compared to the original checkpoint
  • In Visual Studio the MS debug heap capabilities are used
  • For GCC a simple new/delete count is used in overridden operators new, 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];
}

Example Main

#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, char** av)
{
  return RUN_ALL_TESTS(ac, av);
}

Example Test

#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

Conan

CppUTest is available through conan-center.

conanfile.txt
[requires]
cpputest/4.0

[generators]
cmake_find_package
cmake_paths
CMake
find_package(CppUTest REQUIRED)

add_executable(example_test ExampleTest.cpp)

target_link_libraries(example_test PRIVATE
    CppUTest::CppUTest
    CppUTest::CppUTestExt)

Integration as external CMake project

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)

cpputest's People

Contributors

andne avatar asgeroverby avatar basvodde avatar bortsov avatar charlesnicholson avatar dawid-aurobit avatar devmichaeljones avatar dmitrykos avatar flplv avatar itavero avatar jacob-keller avatar jgonzalezdr avatar jkohvakk avatar jwgrenning avatar katcipis avatar kisimre avatar marmidr avatar martiert avatar matdfg avatar mtfurlan avatar offa avatar paulbussmann avatar pjshelton avatar ryanplusplus avatar sonofusion82 avatar terryyin avatar thetic avatar uecasm avatar vpod avatar yocchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpputest's Issues

Unavailable in Ubuntu?

When I wanted to try this test framework, I discovered that it was not available on my virtualbox whose operating system is Ubuntu.

I used the newest release of the CppUTest, and my gcc version is 4.2.3. But when I use the command of 'make', there will be the error information like this:

monkey@monkey-laptop:~/project/cpputest3.3$ make
compiling AllocationInCppFile.cpp
cc1plus: error: unrecognized command line option “-Wsign-conversion”
cc1plus: error: unrecognized command line option “-Wsign-conversion”
make: *** [objs/tests/AllocationInCppFile.o] error 1

Mock tracing mode

Mock now has a tracing mode. But it doesn't work that good yet. It needs to be extended.

Optimize the mock calls

When having a lot of mock expectations, it goes really slow :) Needs to optimize this a bit.

ORDERED_TEST and -r

ORDERED_TESTS fail when running -r. Wonder why...

When this is fixed, turn on the -r for the CppUTestExtTests in the Makefile.am

Let UT_PRINT output be in JUnit xml files

Currently, the UT_PRINT output is ignored for JUnitTestOutput. However, Anton Vasilyev has convincing arguments for putting it in the xml files. So, therefore, this needs to be implemented!

SimpleStringTest reports an error

From Sourceforge:

SVN revision: 710

With IAR Embedded Workbench for ARM, tests\SimpleStringTest.cpp:443: gives an error when running the tests:
Failure in TEST(SimpleString, StringFromFormatpointer)
Off %p behavior

Please add:
else if (h1.size() == 1)
STRCMP_EQUAL("1", h1.asCharString())

.. to the test, so that it does not fail.

According to the development guide for IAR Embedded Workebench,
chapter "Implementation-defined behavior":
%p in printf() (7.19.6.1, 7.24.2.1)
The argument to a %p conversion specifier, print pointer, to printf() is treated as
having the type void *. The value will be printed as a hexadecimal number, similar to
using the %x conversion specifier.

%x does not add "0x" to the added string, and so does not %p.

Two comments:

  1. Should the error say "Odd" instead of "Off"?
  2. Perhaps it is a little more interesting to test StringFromFormat("%p", 15), it should print "F", or "0xF", or "0000000F".

When closed, also close:
https://sourceforge.net/tracker/?func=detail&aid=3487844&group_id=199196&atid=968518

Clang compile error 'no member named 'int_fast8_t''

Compling the latest release of CppUTest (v3.3) with Clang v3.1 on Ubuntu Linux v12.04 throws the below error(s).

I am unsure if this is a bug in CppUTest or if it is something I am doing.

I'd appreciate any help.

clang++ -D__DEBUG -D_UNIT_1_ -g -O0 -faddress-sanitizer -fno-omit-frame-pointer -fms-extensions -Weverything -std=gnu++11 -c CodeMemoryReportFormatter.cpp -o CodeMemoryReportFormatter.o -isystem /usr/include/clang/3.1/include
In file included from CodeMemoryReportFormatter.cpp:28:
In file included from TestHarness.h:71:
In file included from Utest.h:34:
In file included from SimpleString.h:137:
In file included from /usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/string:41:
In file included from /usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/bits/char_traits.h:377:
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:69:11: error: no member named 'int_fast8_t' in the global namespace
  using ::int_fast8_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:70:11: error: no member named 'int_fast16_t' in the global namespace
  using ::int_fast16_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:71:11: error: no member named 'int_fast32_t' in the global namespace
  using ::int_fast32_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:72:11: error: no member named 'int_fast64_t' in the global namespace
  using ::int_fast64_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:74:11: error: no member named 'int_least8_t' in the global namespace
  using ::int_least8_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:75:11: error: no member named 'int_least16_t' in the global namespace
  using ::int_least16_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:76:11: error: no member named 'int_least32_t' in the global namespace
  using ::int_least32_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:77:11: error: no member named 'int_least64_t' in the global namespace
  using ::int_least64_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:79:11: error: no member named 'intmax_t' in the global namespace
  using ::intmax_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:80:11: error: no member named 'intptr_t' in the global namespace
  using ::intptr_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:82:11: error: no member named 'uint8_t' in the global namespace
  using ::uint8_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:83:11: error: no member named 'uint16_t' in the global namespace
  using ::uint16_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:84:11: error: no member named 'uint32_t' in the global namespace
  using ::uint32_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:85:11: error: no member named 'uint64_t' in the global namespace
  using ::uint64_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:87:11: error: no member named 'uint_fast8_t' in the global namespace
  using ::uint_fast8_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:88:11: error: no member named 'uint_fast16_t' in the global namespace
  using ::uint_fast16_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:89:11: error: no member named 'uint_fast32_t' in the global namespace
  using ::uint_fast32_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:90:11: error: no member named 'uint_fast64_t' in the global namespace
  using ::uint_fast64_t;
        ~~^
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../include/c++/4.6/cstdint:92:11: error: no member named 'uint_least8_t' in the global namespace
  using ::uint_least8_t;
        ~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

GetPlatformSpecificTimeInMillis wrong in Visual C++

This makes tests built with Visual C++ report taking almost no time at all.

In src/Platforms/Gcc/UtestPlatform.cpp
static long TimeInMillisImplementation()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000) + (long)((double)tv.tv_usec * 0.001);
}
This returns milliseconds like the function name suggests.

src/Platforms/VisualCpp/UtestPlatform.cpp
static long TimeInMillisImplementation()
{
return timeGetTime()/1000;
}
This returns seconds. Should just be:
static long TimeInMillisImplementation()
{
return timeGetTime();
}
as timeGetTime already returns milliseconds:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx

Extract and rename CppUMock

Extract CppUMock out of the extensions and perhaps out of CppUTest itself so that it can be used by itself?

Defining TEST_GROUP() in header file for multiple source files

We want to share the same TEST_GROUP from multiple sources (to reduce duplicate test class code) since we have some tests for the same function split into multiple ticket issue source files.

With TEST_GROUP_BASE() defined as:

#define TEST_GROUP     (testGroup) \
        TEST_GROUP_BASE(testGroup, Utest)
#define TEST_GROUP_BASE(testGroup, baseclass) \
   int externTestGroup##testGroup = 0; \
   struct TEST_GROUP_##CppUTestGroup##testGroup : public baseclass

… multiple source files can include a header with a TEST_GROUP() and compile successfully but will NOT link because externTestGroup##testGroup is multiply declared.

If TEST_GROUP_BASE() is re-defined to remove externTestGroup##testGroup:

#define TEST_GROUP_BASE(testGroup, baseclass) \
   struct TEST_GROUP_##CppUTestGroup##testGroup : public baseclass

… then multiple source files can include the TEST_GROUP() header and WILL successfully compile, link, and run all tests. However, removing externTestGroup##testGroup deprecates IMPORT_TEST_GROUP:

#define IMPORT_TEST_GROUP(testGroup) \
  extern int externTestGroup##testGroup;\
  int* p##testGroup = &externTestGroup##testGroup

_
Is there some method to defining a TEST_GROUP in a C/C++ header file so multiple C/C++ source files can share it?

_
See similar issue: http://stackoverflow.com/questions/12374286/linker-errors-when-attempting-to-use-test-group-base-shared-test-group-when-test .

Show stack trace on failure

Would be really kewl. gtest does have some code for this. Perhaps check if it can be done for at least gcc..

Memory allocated by strdup and strndup is not tracked

Adding strdup support WILL mean need to automake in order to detect strdup is available. See additional comments on sourceforge

From Sourceforge:

When you use the memory leak detector, memory allocated by strdup is not properly tracked. The header files gives this reason why it has not been done:

strdup was implemented earlier, however it is not an Standard C function but a POSIX function.
Because of that, it can lead to portability issues by providing more than is available on the local platform.
For that reason, strdup is not implemented as a macro.

I don't really understand this comment. What kind of problem ?

Despite this I would suggest that you need to enable by default tracking of memory allocated by strdup and doing whatever is required for this. But you should make it possible to disable it for the platforms where it can be problematic. Furthermore, maybe you could add a configure check that detects those platforms and that disables it automatically on those...

Having to manually mock strdup() to get a working memory detector is a pain. Thank you in advance.

Also close:
https://sourceforge.net/tracker/?func=detail&aid=3533980&group_id=199196&atid=968518

Non standard shebang lines for ruby scripts

There are 4 ruby scripts which have non-standard shebangs. Please use #!/usr/bin/ruby or at least #!/usr/bin/env ruby and not #!/bin/ruby.

The files are:

  • scripts/convertToUnity/cpp_u_test_to_unity.rb
  • scripts/convertToUnity/cpp_u_test_to_unity_utils_tests.rb
  • scripts/convertToUnity/create_group_runner.rb
  • scripts/convertToUnity/create_unity_test_runner.rb

STRCMP_EQUAL could handle NULL better

STRCMP_EQUAL("Hello", 0) crashes instead of failing the test. UtestShell::assertCstrEqual tries to check for NULL strings, but the StringEqualFailure constructor accesses the strings without checking.

CppUTest 3.3.

Better output for CppUMock

CppUMock should be able to give more clear & precise output information about the (missing)actual & expectation.
And it will be really great if it can give the line number of the missing expectation.

Distribute the config.h in make install

We'll probably need to distribute a config.h in the make install and try to detect differences between the installed version and the defines passed to the command line.

Travis CI - libCppUTest.a: file format not recognized; treating as linker script

Not sure if this is an issue with CppUTest, or my project, but my tests run fine locally, and fail on Travis CI, in a pretty vanilla setup. My code under test is C, and I have tried both the c and cpp language settings (I'm assuming cpp is right, though): https://travis-ci.org/#!/tempodb/tempodb-embedded-c/builds/2845623

My tests: https://github.com/tempodb/tempodb-embedded-c/blob/master/tests/TempoDb/TempoDbTest.cpp

Any guidance would be appreciated.

Output:

$ export CXX=clang++
$ export CC=clang
$ clang --version
clang version 3.1 (tags/RELEASE_31/final)
Target: i386-pc-linux-gnu
Thread model: posix
$ make
compiling TCPSocketStub.c
compiling AllTests.cpp
compiling base64.c
compiling tempodb.c
Building archive lib/libTempoDb_CppUTest.a
ar: creating lib/libTempoDb_CppUTest.a
a - objs/src/tempodb/base64.o
a - objs/src/tempodb/tempodb.o
Linking TempoDb_CppUTest_tests
/usr/bin/ld:CppUTest/lib/libCppUTest.a: file format not recognized; treating as linker script
/usr/bin/ld:CppUTest/lib/libCppUTest.a:1: syntax error
collect2: ld returned 1 exit status
make: *** [TempoDb_CppUTest_tests] Error 1

Done. Build script exited with: 1

Issues using log4cxx logging framework in cpputest

Hi,

I want to use log4cxx in my project. I've wrote a simple test

#include "CppUTest/TestHarness.h"
#include <log4cxx/logger.h>


TEST_GROUP(Log4CxxTest) {
    log4cxx::LoggerPtr logger = log4cxx::LoggerPtr(log4cxx::Logger::getLogger("com.foo"));
};

TEST(Log4CxxTest, testUsage) {
    LOG4CXX_INFO(logger, "Simple message text.")

    const char* region = "World";
    LOG4CXX_INFO(logger, "Hello, " << region)
}

which fails with the following errors:

.....log4cxx: No appender could be found for logger (com.foo).
log4cxx: Please initialize the log4cxx system properly.

./libara/other/Log4cxxTest.cpp:12: error: Failure in TEST(Log4CxxTest, testUsage)
    Memory leak(s) found.
Alloc num (1070) Leak size: 29 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22b98d0> Content: "�"
Alloc num (1028) Leak size: 32 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c1ae0> Content: ""
Alloc num (1031) Leak size: 48 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3cd0> Content: ""
Alloc num (1071) Leak size: 32 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c8ab0> Content: "(�MS��"
Alloc num (1053) Leak size: 44 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c69a0> Content: "�"
Alloc num (1046) Leak size: 16 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4400> Content: "P�j"
Alloc num (1036) Leak size: 24 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3f70> Content: " rMS��"
Alloc num (1045) Leak size: 64 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4370> Content: "�"
Alloc num (1030) Leak size: 48 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3c50> Content: ""
Alloc num (1048) Leak size: 64 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c44e0> Content: #MS��"
Alloc num (1038) Leak size: 32 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4050> Content: "(�MS��"
Alloc num (1033) Leak size: 32 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3dc0> Content: "(�MS��"
Alloc num (1042) Leak size: 28 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c42a0> Content: "�"
Alloc num (1041) Leak size: 56 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4210> Content: "�"
Alloc num (1027) Leak size: 64 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22b9940> Content: "��MS��"
Alloc num (1052) Leak size: 46 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4460> Content: "�"
Alloc num (1029) Leak size: 160 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c1b50> Content: "��MS��"
Alloc num (1040) Leak size: 128 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c4140> Content: "P NS��"
Alloc num (1035) Leak size: 29 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3f00> Content: "�"
Alloc num (1037) Leak size: 28 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3fe0> Content: "�"
Alloc num (1032) Leak size: 30 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3d50> Content: ""
Alloc num (1039) Leak size: 40 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c40c0> Content: "@'NS��"
Alloc num (1034) Leak size: 128 Allocated at: <unknown> and line: 0. Type: "new"
     Memory: <0x22c3e30> Content: "��NS��"
Total number of leaks:  23

If I'm using the code outside cpputest, it compiles and works fine. I've also run valgrind on the compiled test suite and it gives me the following heap summary:

==5050== HEAP SUMMARY:
==5050==     in use at exit: 148 bytes in 1 blocks
==5050==   total heap usage: 3,915 allocs, 3,914 frees, 622,358 bytes allocated
==5050== 
==5050== LEAK SUMMARY:
==5050==    definitely lost: 0 bytes in 0 blocks
==5050==    indirectly lost: 0 bytes in 0 blocks
==5050==      possibly lost: 0 bytes in 0 blocks
==5050==    still reachable: 148 bytes in 1 blocks
==5050==         suppressed: 0 bytes in 0 blocks
==5050== Rerun with --leak-check=full to see details of leaked memory

I'm wondering if I should consider something in writing unit tests while using a logging framework. I'm a bit puzzled why this code works outside of cpputest. Any help appreciated.

Thanks in advance!
Best regards,
Michael

P.S.: I'm using g++ 4.7 (but I'm afraid this doesn't matter).

Testing of assertions (Design by Contract)

Patch is at sourceforge!

From SourceForge

CppUTest does not support testing assertions (Design by Contract) at this time. Please note that when an assertion fails the test cannot continue, because executing the code past assertion failure makes no sense. But it is not sufficient to place FAIL() in the assertion handler, because sometimes you exactly want to hit the assertion, so hitting it means actually success. Conversely, when you expect an assertion and you don't hit one, the test should fail.

This feature request is for adding an assertion testing plugin, which would allow specifying expectation for an assertion and would evaluate whether assertion actually happened. All unexpected assertions would represent test failure.

Also close this:
https://sourceforge.net/tracker/?func=detail&aid=3386854&group_id=199196&atid=968521

Improve macro for checking conditions

Currently, the macro CHECK(cond) is provided for checking arbitrary conditions.
A typical use is CHECK(actual>=minimum). Unfortunately, the output of this macro is somewhat limited.
For the example above, the failure message is "CHECK(actual>=minimum) failed."

To get more insight into the problem, I found the following macro very helpful:

define CHECK_RELATION(first, relop, second)\

CHECK_LOCATION((first) relop (second), (StringFrom(#first #relop #second ", ")
+StringFrom(first)+StringFrom(#relop)+StringFrom(second)).asCharString(), FILE, LINE)

Example: CHECK_RELATION(actual, >=, minimum)
Output on failure: "CHECK(actual>=minimum, 0.5>=0.8) failed"

In conclusion, I suggest to add the above macro (or something a bit more elegant) to utestmacros.h.

Also close in SourceForge at:
https://sourceforge.net/tracker/?func=detail&aid=3365842&group_id=199196&atid=968521

unexpected "Deallocating non-allocated memory"

version: v3.3 and trunk
Reproduce steps: make a new project with only below file, turn on memory leak detection ("CXXFLAGS += -include ../../cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h"), the result is "Deallocating non-allocated memory" and a segmentation fault:

#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestRegistry.h"

class MyPlugin: public TestPlugin {
    int* dummy_;
public:
    MyPlugin() :
            TestPlugin("MyPlugin")
    {
        //dummy_ = new int[100];
        dummy_ = (int*) malloc(100*sizeof(int));
    }
    virtual ~MyPlugin()
    {
        //delete[] dummy_;
        free(dummy_);
    }

};

int main(int ac, const char** av)
{
    MyPlugin m;
    TestRegistry::getCurrentRegistry()->installPlugin(&m);

    int res = CommandLineTestRunner::RunAllTests(ac, av);

    return res;
}

I've debugged it, the MemoryLeakWarningPlugin in CommandLineTestRunner::RunAllTests is a local variable, its destructor gets invoked just before the function returns, so the information of 'new' in MyPlugin() got cleared in MemoryLeakWarningPlugin::destroyGlobalDetector(). Then, the 'delete' in ~MyPlugin() can't find a matched 'new', BANG!.

./testcppu

OK (0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)


unknown file:0: error: Failure in TEST(
     NOTE: Assertion happened without being in a test run (perhaps in main?), 
           Something is very wrong. Check this assertion and fix)

           Something is very wrong. Check this assertion and fix:0: error:
    Deallocating non-allocated memory
   allocated at file: <unknown> line: 0 size: 0 type: unknown
   deallocated at file: main.cpp line: 16 type: free


Segmentation fault (core dumped)

Two bashisms in ReleaseCppUTest.sh

It has been reported on the Debian Bug Tracker that ReleaseCppuUTest.sh has a /bin/sh shebang and yet uses bashisms (i.e. non-POSIX contructs):
GENERATED_FILES+=" $filename.sh"
[...]
GENERATED_FILES+=$versionFile

In both case, you should use something like FOO="$FOO $texttoadd" instead. Thank you!

FYI the Debian bug report is at http://bugs.debian.org/690714

Detailled logging

A request was on the CppUTest mailing list for more detailled logging, including the which asserts were executed and which actual/expected values there were. Not sure how useful this is but it is certainly interesting to implement :)

Failure messages

Make sure that all the failure messages are inside the TestFailure classes

Compile Error in 3.2 release

I downloaded the latest 3.2 .zip file and tried to compile, which aborted with errors.

$ make
compiling AllocationInCppFile.cpp
compiling AllocLetTestFreeTest.cpp
compiling AllTests.cpp
compiling CheatSheetTest.cpp
compiling CommandLineArgumentsTest.cpp
compiling CommandLineTestRunnerTest.cpp
compiling JUnitOutputTest.cpp
compiling MemoryLeakDetectorTest.cpp
compiling MemoryLeakOperatorOverloadsTest.cpp
compiling MemoryLeakWarningTest.cpp
compiling NullTestTest.cpp
compiling PluginTest.cpp
compiling PreprocessorTest.cpp
compiling SetPluginTest.cpp
compiling SimpleStringTest.cpp
compiling TestFailureTest.cpp
compiling TestFilterTest.cpp
compiling TestHarness_cTest.cpp
compiling TestInstallerTest.cpp
compiling TestMemoryAllocatorTest.cpp
compiling TestOutputTest.cpp
compiling TestRegistryTest.cpp
compiling TestResultTest.cpp
compiling UtestTest.cpp
compiling AllocationInCFile.c
compiling AllocLetTestFree.c
compiling TestHarness_cTestCFile.c
compiling CommandLineArguments.cpp
compiling CommandLineTestRunner.cpp
compiling JUnitTestOutput.cpp
compiling MemoryLeakDetector.cpp
compiling MemoryLeakWarningPlugin.cpp
compiling SimpleString.cpp
cc1plus: warnings being treated as errors
src/CppUTest/SimpleString.cpp: In member function ‘void SimpleString::split(const SimpleString&, SimpleStringCollection&) const’:
src/CppUTest/SimpleString.cpp:153: error: conversion to ‘size_t’ from ‘int’ may change the sign of the result
make: *** [objs/src/CppUTest/SimpleString.o] Error 1

different interpretations of MACRO between Makefile and C preprocessing

In makefile, CPPUTEST_USE_STD_CPP_LIB (and other definitions) can be set to 'Y' or 'N', they're different literal strings, works well in Makefile (in fact, it's a 'variable' not a 'macro' in Makefile), but in C/C++ code, since 'Y' and 'N' are not defined, they're all interpreted as constant zero, so macro check in below code seems wrong, CPPUTEST_USE_STD_CPP_LIB is always zero:

void UtestShell::exitCurrentTest()
{
#if CPPUTEST_USE_STD_CPP_LIB
    throw CppUTestFailedException();
#endif
    exitCurrentTestWithoutException();
}

http://gcc.gnu.org/onlinedocs/cpp/If.html

Identifiers that are not macros, which are all considered to be the number zero. This allows you to write #if MACRO instead of #ifdef MACRO, if you know that MACRO, when defined, will always have a nonzero value.

Support system-wide installation and pkg-config

This needs to be done together with automake support

From SourceForge

It would be nice if CppUTest could support the usual "make install" target. It would install the public headers in /usr/include/ and the libraries in /usr/lib. This should allow usage of CppUTest without having to hardcode any CPPUTEST_HOME in all projects.

Furthermore, it would be nice if this would also provide a working pkg-config file to avoid having to hardcode the precise set of compiler flags that are needed to build with cpputest. For more information about pkg-config, have a look here:
http://www.freedesktop.org/wiki/Software/pkg-config
http://people.freedesktop.org/~dbn/pkg-config-guide.html

Also close this:
https://sourceforge.net/tracker/?func=detail&aid=3532113&group_id=199196&atid=968521

Malloc hooks

gcc c library has some malloc hooks that can also be used for memory leak checking. Check them out?

Why TestHarness.h includes MemoryLeakWarningPlugin.h?

Seems unnecessary to me, user can always include it if he/she wants to control memory leak detection behavior.

Or maybe it's in there before "MemoryLeakDetectorNewMacros.h" was created? This header file was used to redefine 'new'?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.