A dockerized C template for running unit tests with CMake and Unity testing framework.
├── src/
│ ├── example.c # Example file to test
│ └── main.c # Main application entry point
├── tests/
│ ├── unity/
│ │ ├── unity.h # Unity testing framework header
│ │ └── unity.c # Unity testing framework implementation
│ ├── test_example.c # Unit tests for example file
│ └── test_main.c # Test runner main
├── CMakeLists.txt # CMake project configuration
├── Dockerfile # Docker configuration
├── build_docker.sh # Docker build script
├── run_tests.sh # Test execution script
└── README.md # This file
- C11 with CMake build system
- Unity testing framework for unit testing
- Docker support for containerized testing
-
Build the Docker image:
./build_docker.sh [image-name] [platform]
Examples:
./build_docker.sh # Uses default: c-unit-tests-base ./build_docker.sh my-c-app # Custom image name
-
Run tests in Docker:
./build_docker.sh my-c-app docker run --rm -it my-c-app ./run_tests.sh
-
Interactive Docker session:
docker run -it --rm c-unit-tests-base bash # or you can mount your local folder in docker docker run -it --rm -v $(pwd):/app c-unit-tests-base bash
Inside the container, you can run:
./run_tests.sh # run the build and tests valgrind --leak-check=full ./build/c_app # check for memory leaks ./build/c_tests # run the test binary for more output
This is a Template - Replace with Your Code!
This template provides the essential structure and configuration files for a C project with unit testing. You should replace the example code with your own C application while maintaining the project structure.
Keep these key files and structure for your project:
src/
- Your main application code (replacesrc/
with your own structure)tests/
- Your unit tests and Unity frameworkCMakeLists.txt
- CMake configuration (update for your project details)Dockerfile
- Docker configuration for containerized testing. LEAVE AS IS.build_docker.sh
- Docker build script. LEAVE AS ISrun_tests.sh
- Test execution script. LEAVE AS IS.
This template includes a lightweight Unity testing framework with these assertions:
TEST_ASSERT_TRUE(condition)
TEST_ASSERT_FALSE(condition)
TEST_ASSERT_EQUAL_INT(expected, actual)
TEST_ASSERT_EQUAL_STRING(expected, actual)
TEST_ASSERT_NULL(pointer)
TEST_ASSERT_NOT_NULL(pointer)
-
Update the main application:
- Replace the example main.c with your application's entry point
- Update the CMakeLists.txt to reflect your executable name
-
Replace the example tests:
- Replace files in
tests/
with your own test files - Follow the same naming convention (test_filename.c)
- Replace files in
-
Update CMake configuration:
- Modify
CMakeLists.txt
to add any additional libraries your project needs
- Modify
Important: Keep the Unity framework files and template build files but update their content as needed for your project.
- Create new header/source files in your directory structure
- Add corresponding tests in the test directory
- Update CMakeLists.txt if needed for additional dependencies
The template includes these key components:
- Unity testing framework (included)
- C11 standard library features
- Cross-platform build support
Add new dependencies to CMakeLists.txt
:
# Find additional packages
find_package(YourLibrary REQUIRED)
# Link to your targets
target_link_libraries(your_target YourLibrary::YourLibrary)
-
CMake version too old: Update CMake to version 3.16 or higher
-
Compiler errors: Ensure you have a C11 compatible compiler (GCC 4.7+, Clang 3.3+)
-
Memory leaks: Use valgrind to detect memory issues: