Skip to main content

CppUMockify

Cloned from Marco M/Mockify

Build Status PyPI version PyPI - License PyPI - Python Version

Generate complete boilerplate code for CppUTest C/C++ mocks.

When writing mocks, the majority of the code is just boring boilerplate. For example, to mock

int zoo_cat(int a)

one has to write:

1    int zoo_cat(int a) {
2        mock().actualCall("zoo_cat")
3            .withParameter("a", a);
4        if mock().hasReturnValue() {
5            return mock().intReturnValue();
6        }
7        return something(a);
8    }

where everything but line 7 is boilerplate. Multiply this for the tens of mocks needed also for the smallest unit test and the task quickly becomes boring and error-prone.

CppUMockify is written in Python and thanks to the excellent pycparser parses the C code of the function prototype to mock and generates all the needed boilerplate :-)

Development status

Already usable with basic functionalities. Most important missing parts:

  • support for basic typedefs (eg size_t) both as function type and as function argument
  • support for output parameters (eg void foo(int* bar))

Installation

pip install CppUMockify

Usage

CppUMockify is designed to be used with any editor or IDE, or just from the shell.

Assuming that function zoo_cat() is declared in header file zoo.h, calling:

cppumockify.py zoo "int zoo_cat(int a);"

will generate mock file zoo_mock.cpp, containing the mock boilerplate for function zoo_cat().

Subsequent calls to cppumockify matching on the header file will append the new mock boilerplate to zoo_mock.cpp. For example

cppumockify.py zoo "int zoo_dog(int b);"

will append the boilerplate mock for zoo_dog().

Suggested workflow

Bring a source file under unit test. Once the compilation is succesful, you will start to have linker errors for all the functions you need to mock.

Copy the first unfound symbol from the linker, search for it (using your IDE, cscope, ctags, ...), go to the corresponding header file. Copy and paste the function declaration and pass it to cppumockify.

The above steps can be automated within your editor or IDE. For CLion, you can use it as an "external tool" and pass it automatically the name of the header file and the copy and paste of the function declaration.

Add the generated mock file to the build. Open the generated mock file, check and complete the mock boilerplate.

Rinse and repeat :-)

Release files for CppUMockify 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CppUMockify 0.0.6
File Size Uploaded
CppUMockify-0.0.6.tar.gz 5.7 kB Details

Release files / CppUMockify-0.0.6.tar.gz

Download URL CppUMockify-0.0.6.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
85b43919e6beca5b7d6dd010ffb0e8059ed176d9095f1da6b8d8e38eeb8fc17b
BLAKE2b-256 checksum
How to use checksums
3952220c3741ff7af2b72faae7dee2018288bd63b96951818a2d269ede6bb83a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.6 This release

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page