TDD using CUnit: First Steps

# apt-get install libcunit1 libcunit1-doc libcunit1-dev
#include "CUnit/Basic.h"int main() {
// Initialize the CUnit test registry
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
// Sets the basic run mode, CU_BRM_VERBOSE will show maximum output of run details
// Other choices are: CU_BRM_SILENT and CU_BRM_NORMAL
CU_basic_set_mode(CU_BRM_VERBOSE);
// Run the tests and show the run summary
CU_basic_run_tests();
return CU_get_error();
}
$ gcc -o sum_test sum_test.c -lcunit
$ ./sum_test
CUnit - A unit testing framework for C - Version 2.1-2
http://cunit.sourceforge.net/
Run Summary: Type Total Ran Passed Failed Inactive
suites 0 0 n/a 0 0
tests 0 0 0 0 0
asserts 0 0 0 0 n/a
Elapsed time = 0.000 seconds
void sum_test() {
// Check if first param matches with second[2]
CU_ASSERT_EQUAL(sum(2, 14), 16);
CU_ASSERT_EQUAL(sum(0, 0), 0);
CU_ASSERT_EQUAL(sum(-1 ,6), 5);
}
CU_pSuite pSuite = NULL;// Add a suite to the registry
pSuite = CU_add_suite("sum_test_suite", 0, 0);
// Always check if add was successful
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
// Add the test to the suite
if (NULL == CU_add_test(pSuite, "sum_test", sum_test)) {
CU_cleanup_registry();
return CU_get_error();
}
int sum(int a, int b){
return a + b;
}
$ ./sum_test     CUnit - A unit testing framework for C - Version 2.1-2
http://cunit.sourceforge.net/
Suite: sum_test_suite
Test: sum_test ...passed
Run Summary: Type Total Ran Passed Failed Inactive
suites 1 1 n/a 0 0
tests 1 1 1 0 0
asserts 3 3 3 0 n/a
Elapsed time = 0.000 seconds

--

--

--

computer science student ~ working as software engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marina Limeira

Marina Limeira

computer science student ~ working as software engineer

More from Medium

Experience During Application Framework Group Project

What is OpenAPI?

How to create dynamic E2E testing environments

Why Software Development is a Never-Ending Process?