Your-First-C-TDD-Project exemplifies Continuous Test-Driven Development by showcasing a simple addition test in C, seamlessly integrated with Python for automated testing and managed through efficient Makefiles, fostering an iterative and robust development process.
This project demonstrates a simple build system for a C program using a Makefile and incorporates a basic Test-Driven Development (TDD) approach. The project includes the following components:
main.c
: Main C source code file with a simple program.main.h
: Header file declaring functions used inmain.c
.my_C_test.py
: Python test script for testing the C program's functionality.Makefile
: Makefile to automate the compilation and building process.
-
main.c
- Contains the main C program with a
main
function and anadd
function. - The
main
function calls theadd
function to perform addition.
- Contains the main C program with a
-
main.h
- Header file declaring the
add
function for use inmain.c
. - Ensures proper function declaration and separation of concerns.
- Header file declaring the
-
my_C_test.py
- Python script for testing the C program's functionality.
- Uses the
subprocess
module to run the compiled C program and check its output. - Implements a simple test (
test_c_program
) to validate theadd
function's correctness.
-
Makefile
- Defines compiler options (
CC
,CFLAGS
) for GCC. - Specifies source files (
SRC
) and object files (OBJ
). - Contains rules to compile C source files, build the executable, and clean up generated files.
- Defines compiler options (
-
C Program Functionality Testing:
- Verify that the C program performs addition correctly.
- Test Method: The Python script
my_C_test.py
runs the compiled C program (my_program
) and checks if the addition result is as expected.
-
Testing Failure Scenarios:
- To demonstrate test failures, a deliberate error was introduced in the C code (
main.c
). - The
add
function inmain.c
was modified to produce an incorrect result. - Running the Python test script
my_C_test.py
after compiling the modified C code results in a test failure due to the intentional error.
Example Output of Failed Test:
Traceback (most recent call last): File "my_C_test.py", line 12, in <module> test_c_program() File "my_C_test.py", line 8, in test_c_program assert output == expected_output, f"Expected: {expected_output}, Got: {output}" AssertionError: Expected: Result: 12, Got: Result: 13
- To demonstrate test failures, a deliberate error was introduced in the C code (
-
Compilation and Building:
- The Makefile automates the compilation and building process.
- Compiles C source files into object files (
main.o
). - Links object files to create the executable (
my_program
).
-
Test-Driven Development (TDD) Approach:
- Python script
my_C_test.py
follows a TDD approach. - Writes a failing test (
test_c_program
) that checks the addition result. - Implements the C code (
main.c
) to pass the test. - Runs the test to validate the functionality.
- Python script
-
Automation with Makefile:
- Saves time and effort by automating the compilation process.
- Defines compiler options and rules for compiling, building, and cleaning.
-
Test-Driven Development (TDD):
- Ensures code correctness by writing tests before implementing code.
- Improves code quality, encourages modular design, and supports incremental development.
-
Separation of Concerns:
- Header files (
main.h
) separate function declarations from implementations. - Test scripts (
my_C_test.py
) focus on testing without cluttering the main codebase.
- Header files (
-
Clear Test Objectives:
- Enhances code reliability and facilitates future modifications.
project_folder/
│
├── main.c
├── main.h
├── my_C_test.py
└── Makefile
- Clone the repository.
- Navigate to the project directory.
- Compile the C code using
make
. - Run tests using
python my_C_test.py
.
This README.md provides an overview of the project's structure, objectives, and rationale for using specific files and approaches. It emphasizes automation, TDD principles, separation of concerns, and clear test objectives for maintaining code quality and functionality validation.
10 different types of tests that can be conducted using Python:
- Unit Tests: Test individual units or components of the code in isolation.
- Integration Tests: Test how different components/modules work together.
- Functional Tests: Test the functionality of the entire system or application.
- Regression Tests: Test to ensure that new code changes do not break existing functionalities.
- Performance Tests: Test the performance and efficiency of the code under various conditions.
- Security Tests: Test for vulnerabilities and security risks in the code.
- Stress Tests: Test the behavior of the system under high load or stress conditions.
- Boundary Tests: Test the behavior of the code at boundary conditions and edge cases.
- Compatibility Tests: Test the compatibility of the code with different platforms or environments.
- Usability Tests: Test the usability and user experience aspects of the application or system.
These are just a few examples of the types of tests that can be conducted using Python. Depending on the nature of your project, you may choose to conduct one or more of these tests to ensure the quality, reliability, and performance of your codebase.