Problem and solution of TDD in C

Yasuo Hosotani(yasuohosotani@yahoo.co.jp)
2005/1/31

1.Introduction

Today, Automatic unit test using xUnit is popular in Object-Oriented language(Java,C++,C# etc.). But it is not popular in C.

C is major language for development of embedded software. I think that TDD is very useful for Embedded software because bug fix is difficult after release.

I will explain problem and solution of TDD in C.

2.Framework

 I used CUnit for Mr.Ando for trying this method. CUnit for Mr.Ando is simple test framework for C.

This method doesn't depend on this framework. You can use other framework in C. ex. CUnit

3.Unit test in C

Target of unit test in OO language is a class but it is a function in C. In OO language, Mock object is used when target class call other class's method.

In C, Stub is used when target function call other function.

C_TDD_E1.JPG - 6,609BYTES

 

4.Problem of automatic unit test in C

I faced following problem when I tried automatic unit test using test-framework.

(1)Conflict of real and stub Real function's name comes into conflict stub's name.

C_TDD_E2.JPG - 12,004BYTES

(2) Conflict of stub to each other Stub's name comes into conflict stub's name for other function's test.

C_TDD_E3.JPG - 15,975BYTES

 

 Compile error occur in these cases. We can't avoid it because we can't use namespace in C.

 5.Solution for these problems

I will explain solution for conflicts. You can avoid conflicts by the following process.

(1)Define target function as another name in test project.

#ifdef TEST
func_true()
#else
func()
{
 ....
 ....
}

You can define as another name in test project by using '#ifdef'.

(2)Define following functions.

Function’s pointer

SetFunctionPointer(fp)
{
  Function’s pointer=fp;
}
Function()
{
  Function’s pointer();
}

The function is pointed by Function's pointer will execute when you run test. You can test in these cases.

 

(i)Test for Function1. Function1 call Function2.

C_TDD_E4.JPG - 13,602BYTES

 (a)Set the stub's pointer to Function's pointer for Function2 by using SetFunctionPointer.

 (b)Run test for Function1. Function1's test call Function1.

 (c)Function1 call Function2. Function2 call function pointed by Function's pointer.

 (d)The stub is executed because Function's pointer point to the stub.

(ii)Test for Function2

C_TDD_E5.JPG - 12,677BYTES

(a) Set Function2's pointer to Function's pointer for Function2 by using SetFunctionPointer.

(b)Run test for Function2. Test func call Function2.

(c)Function2 call function pointed by Function's pointer. Function2_true is executed because Function's pointer point to Function2_true.

6. Conclusion

I can automatic unit test in C by using this method. I think next step.

(i) Tools for automatic making code of define function for avoid conflict. Light rhythm is important in TDD. Code for this method can make automatically from target function's definition.

(ii) Arrangement of design and test patterns in C    ex. Implementation and test of state pattern in C

Would you please send your feedback?. e-mail  yasuohosotani@yahoo.co.jp