C language content
Writing test cases in C involves creating a suite of test functions to verify the behavior of your code. These test cases check if your functions work as expected under different conditions. Here’s a detailed guide on writing test cases in C:

1. Understanding the Functionality

Before writing test cases, thoroughly understand the functionality of the code you’re testing. Let’s assume you have a function int add(int a, int b) that adds two integers.

2. Setting Up the Test Environment

You’ll need a testing framework or write your own simple test harness. One popular lightweight framework is CUnit, but you can also create custom test functions if you prefer simplicity.

3. Creating the Test Suite

Using a Custom Test Harness

1. Define Test Functions:

Write separate functions to test various aspects of add.

				
					#include <stdio.h>
#include <assert.h>

int add(int a, int b) {
    return a + b;
}

void test_add_positive_numbers() {
    int result = add(2, 3);
    assert(result == 5);
    printf("test_add_positive_numbers passed\n");
}

void test_add_negative_numbers() {
    int result = add(-2, -3);
    assert(result == -5);
    printf("test_add_negative_numbers passed\n");
}

void test_add_zero() {
    int result = add(0, 0);
    assert(result == 0);
    printf("test_add_zero passed\n");
}

void test_add_mixed_sign_numbers() {
    int result = add(-2, 3);
    assert(result == 1);
    printf("test_add_mixed_sign_numbers passed\n");
}

int main() {
    test_add_positive_numbers();
    test_add_negative_numbers();
    test_add_zero();
    test_add_mixed_sign_numbers();
    printf("All tests passed!\n");
    return 0;
}

				
			

2. Compile and Run the Tests:

Compile the code using a C compiler like gcc.

				
					gcc -o test_add add.c
./test_add

				
			

Using CUnit Framework

1. Install CUnit:

You need to install CUnit. On a Linux system, you can install it using your package manager:
				
					sudo apt-get install libcunit1 libcunit1-doc libcunit1-dev

				
			

2. Include CUnit in Your Code:

				
					#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

int add(int a, int b) {
    return a + b;
}

void test_add_positive_numbers() {
    CU_ASSERT(add(2, 3) == 5);
}

void test_add_negative_numbers() {
    CU_ASSERT(add(-2, -3) == -5);
}

void test_add_zero() {
    CU_ASSERT(add(0, 0) == 0);
}

void test_add_mixed_sign_numbers() {
    CU_ASSERT(add(-2, 3) == 1);
}

int main() {
    CU_initialize_registry();

    CU_pSuite suite = CU_add_suite("add_test_suite", 0, 0);

    CU_add_test(suite, "test_add_positive_numbers", test_add_positive_numbers);
    CU_add_test(suite, "test_add_negative_numbers", test_add_negative_numbers);
    CU_add_test(suite, "test_add_zero", test_add_zero);
    CU_add_test(suite, "test_add_mixed_sign_numbers", test_add_mixed_sign_numbers);

    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();

    CU_cleanup_registry();
    return 0;
}

				
			

Compile and Run the Tests:

Compile the code using a C compiler and link it with CUnit:
				
					gcc -o test_add add.c -lcunit
./test_add

				
			

4. Interpreting Results

For both custom test harness and CUnit:

  • If all assertions hold true, the tests pass.
  • If any assertion fails, the test harness or CUnit will report the failed test, making it easier to locate and fix issues.

5. Best Practices

  • Modular Tests: Keep your tests modular. Each test function should test a single aspect or scenario.
  • Edge Cases: Test edge cases, such as adding very large or very small numbers, or combinations of positive and negative numbers.
  • Readable Output: Ensure test output is clear and easy to understand.
  • Automated Testing: Integrate tests into an automated testing pipeline if possible.

By following these steps, you can create comprehensive test cases to ensure the reliability and correctness of your C programs.

Scroll to Top