I am trying to add a couple of tests to a legacy C project. The project basically consists of a command line tool that prints something to stdout every time an event happens. Now, since writing unit tests would be pretty hard given the fact that the modules are pretty tight coupled, I am trying to write some functional tests in order to validate the current behaviour and then I'll go on splitting the modules so I can unit test them.

Does it make sense to have the testing framework something like a few Python scripts (keep in mind that the project is pure C) that handles all the functional tests? Basically, Python should call my command line tool, fake its input and expect a valid output.

  • 6
    Slightly related: I'd suggest a look at Working Effectively with Legacy Code, by Michael Feathers, which contains sound advice and patterns to deal with testing, extending, refactoring legacy projects.
    – Giulio Piancastelli
    Sep 6 '11 at 22:42
  • Thanks @Giulio, I know about the book, I was looking for more hands-on experience with this approach.
    – hyperboreean
    Sep 6 '11 at 22:47

Yes it does and is a wise Idea.

You can also go in the details using Cython and calling some of the legacy C functions.



If the tests use the application as a black box, through the command line, then they don't need to be in the same language as the application. They can be in a language that is best suited for writing the tests.

Scripting languages, such as Python or Ruby, are generally a good fit for text parsing and calling command line applications, whereas with C it would be much harder. They also have good testing tools to help in writing those tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy