tags:

views:

367

answers:

7

Is Test Driven Development limited to OO? Or is it possible/useful to use it in conjunction with a procedural language? I have to start a greater project in C the next few weeks and I'm thinking of ways how to the developing.

+5  A: 

Test Driven Development just means that you develop code starting with the test pattern first, then building out functionality until it passes the test. This is equally applicable to any programming paradigm: structured/procedural, object-oriented, functional -- anything that can take inputs and outputs can be tested.

Dan Story
+8  A: 

TDD is a design paradigm and as such is not tied to any specific programming paradigm.

Simply put, you write a test for your code before writing your of code.

See this slide deck about TDD with C (the Bowling Game Kata).

Oded
+1  A: 

As others have noted, TDD is not in itself tied to OOP. However, in practice it is tied to modularity. Each unit-test should only test a very limited part of the program, so if you have a lot of global state it will be almost impossible to unit-test the program.

It is definitely possible to write modular programs in C, but it requires different techniques. TDD might force you to write a modular program, but it might also make your development grind to a halt, if you are not used to writing modular C-programs.

Jørgen Fogh
A: 

There are unit test frameworks for c. Googling for "c unit test" will reveal some. I wrote several projects in C with TDD and definitely TDD is not only for OO languages. Even if it is harder to do TDD in C.

Oleg
A: 

TDD is defined by Wikipedia as follows:

Test-driven development (TDD) is a software development technique that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.

There is absolutely nothing here that makes it specific to OOP. On the contrary, TDD is quite suitable for all kinds of programming. In particular, it is definitely possible to use it in conjunction with a procedural language, including C.

Eli Bendersky
+1  A: 

I don't think TDD is limited to OO at all.

Try the frameworks Unity & CMock.

an example from their wiki:

void test_ShowSomeSillyExamples(void)
  {
    TEST_ASSERT_NOT_EQUAL(0, -1);
    TEST_ASSERT_EQUAL_INT(1, 1);
    TEST_ASSERT_EQUAL_HEX16(0x1234, 0x1234);
    TEST_ASSERT_EQUAL_STRING("These Are The Same", "These Are The Same");
    TEST_ASSERT_BITS(0x1111, 0x5555, 0x7175);
    TEST_ASSERT_INT_WITHIN(5, 100, 102);
  }

see: http://sourceforge.net/apps/trac/embunity/wiki

A book:

http://www.pragprog.com/titles/jgade/test-driven-development-for-embedded-c

A Yahoo group about this stuff:

http://tech.groups.yahoo.com/group/AgileEmbedded/

If you would do TDD in C, would you need to write your tests in C? I would probably write the production code in C and the tests in C++ using Google's gtest.

Eddy Pronk
A: 

TDD means that you use Unit Testing before you write the code (Test First) and drive the design with that, of course it suites C, but i imagine two problems that you would face ( I did not try)

  • The slowness of compilation if it is a big project, TDD requires baby steps in development and running code frequently, C/C++ is much slower in compilation than C#, java, etc.. and of course even slower than dynamic languages :).

  • Testing in isolation, Mocking sub systems so you test each one in isolation (because you are TDDing so you will have systems that are not yet implemented but you have a dependency to them or you don't want your tests be infected with external effectors like FileSystem, Sockets, etc..), and of course that depends also on the scale of the application and your design, i don't know what is the strategies to do that, I imagine passing function pointers (So you pass a pointer to the fake function to the function in test) would be a way to go, but you have to research that.

Thanks

Kariem Abd El-Fattah