Test-Driven Development Based on Mock Objects for Non-OO Languages - Embedded.com

Test-Driven Development Based on Mock Objects for Non-OO Languages

Much software development is done in the C programming language despite the fact that C is a low level language. The advantages of C programming language are its simplicity, ease to learn, clear syntax, faster compilation time and the fact that it requires less run time.

The C language is mainly used for embedded software development, where low level development is required. Even if an assembly-level language can be used for developing embedded software, C is the most widely used language due to its efficiency, less overhead, less development time, more readability and more C compilers are available for different platforms for free.

On the other hand, the C language does not have constructors, destructors, modularity concept, polymorphism, encapsulation and abstraction, but these concepts are necessary to use Test- Driven Development (TDD). TDD is useful to avoid more bugs in the later stages of the software development. In TDD, the test cases drive the development of the software system. So the test cases are maintained for each function of the code. Because of that reason bugs can be found easily at unit test level.

This work therefore focuses on how to use TDD for a non-object oriented language like C. Moreover, it concentrates on how to solve dependencies in a better way and also it analyzes different frameworks based on TDD.

As its name implies in TDD the test case drives the development gradually, unlike traditional software development, where there is no custom of writing and maintaining test cases at unit test level, which results in introduction of bugs in the old code when a new requirement is designed. These bugs will be visible after integration testing is done. At that stage, the bugs can be difficult to find. Moreover, more time is spent on developing the code from the requirements and less time is left to write the test cases.

In the end, because of time and budget constraints, it may not be possible to write sufficient test cases. Due to these reasons, a lot of bugs are missed resulting into extra effort to find those bugs during debugging. However with TDD, the code is developed after a test case is written.

So, the test cases are maintained for each function or logic in the code. Additionally, all test cases are run after every new function is developed to avoid creation of new bugs in the old code. Hence, TDD helps to reduce bugs, which in turn reduces debugging time that increases the code quality and worker productivity.

In TDD, one module is developed at a time. To test that module, it must be isolated from its collaborators. The collaborators are dependent modules. To isolate the module, a polymorphic interface is used in the object-oriented (OO) languages.

Similar to the polymorphic interface, stubs can be used in the C language to isolate the modules. A stub is a concept where the function calls required for module under test can be faked from its collaborators. The idea of using stub is taken from the embedded projects where stub is used when hardware is not ready.

Hence with stubbing, the same benefits as with the polymorphism can be achieved.These concepts are necessary to use TDD because in TDD, one module is developed at a time. To test that module, it must be isolated from its collaborators. The collaborators are dependent modules.

To isolate the module, a polymorphic interface is used in the object-oriented (OO) languages. Similar to the polymorphic interface, stubs can be used in the C language to isolate the modules. A stub is a concept where the function calls required for module under test can be faked from its collaborators. The idea of using stub is taken from the embedded projects where stub is used when hardware is not ready. Hence with stubbing, the same benefits as with the polymorphism can be achieved.

To read this external content in full, download the complete paper from the author online archives of Lnkoping University at DIVA..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.