A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/cpputest/cpputest below:

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

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
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:

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)
Set up and tear down support

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

Customize CHECK_EQUAL to work with your types that support operator==()

The Extensions directory has a few of these.

Building default checks with 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

How is memory leak detection implemented?

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)
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
)
FetchContent_MakeAvailable(CppUTest)

It can be used then like so:

add_executable(example_test UnitTest1.cpp UnitTest2.cpp)

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4