Skip to content

stnuessl/ccmock

Repository files navigation

ccmock (Work in Progress)

Build CI License: GPL v2

Automatic mock creation for unit tests in C and C++.

ccmock either needs a compilation database or a compile_flags.txt to be able to parse a project's source files.

  • Automatic detection which functions need to be mocked.
  • Blacklist entries
  • Supported frameworks: * gmock * fff

Depending on which compiler you prefer, chose either * gcc 12.1.0 or * clang 14.0.0.

Also depending on what you prefer, chose either * make or * ninja 1.10.

In any case, you'll also need to following programs to build ccmock from source:

Install all required tools and libraries (chosing the most conservative options):

pacman -Syu llvm clang bash gcc git cmake make
apt-get install llvm llvm-dev clang libclang-dev bash gcc g++ git cmake make

Configure the build system:

cmake -B build -DCMAKE_BUILD_TYPE=release

Build the project with clang:

cmake --build build

Install the built program to /usr/local/bin:

cmake --install build --strip
  1. Generate a compilation database compile_commands.json for your project.

  2. Feed the source file and the compilation database into ccmock to create mocks for your unit test code.

    ccmock --compile-commands=compile_commands.json -o <output> <input>

  3. Use the preprocessor to include the generated output file in your unit test source file.

  4. Focus on writing unit test code.

ccmock
ccmock --help
ccmock -h
ccmock <input-file>
ccmock -o <output-file> <input-file>
ccmock --compile-commands=compile_flags.txt <input-file>
ccmock --compile-commands=compile_commands.json <input-file>
ccmock --config=<config>.yaml
ccmock --config=<config>.yaml,<config>.yaml <input-file>
CCMOCK_CONFIG=<config>.yaml ccmock --config=<config>.yaml,<config>.yaml <input-file>
ccmock --dump-config
ccmock --dump-config -o <output-file>
ccmock --config=<config>.yaml,<config>.yaml --blacklist=<name> --dump-config
ccmock --extra-args=-DNDEBUG,-Wall,-Werror <input-file>
  • Always name your parameters in function declarations.
  • Don't mock very fundamental functions like malloc(), open(), etc... as doing so will very likey break your unit test library in unpredictable ways.

Explore the test/system directory to see how ccmock can be integrated into a build system for automatic mock generation.

  • C++ * Mock function calls are not associated to any specific object instance. * The constructors and destructors of global objects cannot be checked with

    mock calls.

    • Global objects can only be automatically instantiated if they have a default constructor.
  • Mocking C++ is not implemented
  • Fake Function Framework generator not implemented
  • Generate global variable definitions
  • Restructure test tree
  • Automatic include handling
  • Provide multiple test runners (gtest, unity?, ctest?)
  • Add support for another mock library (cpputest?)
  • Implement own mocking functionality
  • --backend None to just print prototypes

About

ccmock - Automatic C and C++ Function Mock Generation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors