11/08/2011

I c u Test

Unit testing is an important part of designing and developing code. Here are 5 reasons why.
  1. Unit testing allows you to test your code all the time, in an easy way. Instant gratification, if you will.
  2.  If one starts unit testing early in the process of writing their code, then it leads to a better design.Your method names and classes will be based on the tests you write and can therefore help you organize your code.
  3. Unit testing allows one to make changes very easily to your code later down the line. Developing a good baseline of tests, lets you refactor you code easily because you know it works allready.
  4. Since unit tests actually "test your code," you can "try to break it." This will give you an understanding of your code you would not have if not testing.
  5. And the most important (for your boss)

This Is Why Unit Testing Is Important
CxxTest for C++ is a very nice way to write unit tests and do unit testing in C++. Cpp files are generated with perl from your regular classes and then you compile those and run them. You therefore do not have to declare your tests in your regular code and can just attach their framework to do your testing. It even has an awesome plugin for eclipse that lets you easily write tests and use them through a gui.

Some of the features (as taken from its own description):
  •     Doesn't require Runtime Type information.
  •     Doesn't require member template functions.
  •     Doesn't require exception handling.
  •     Doesn't require any external libraries (including memory management, file/console I/O, graphics libraries.)
  •     Is distributed entirely as a set of header files.
Testing is done by writing test cases mainly with a function called TS_Assert(). It works very similar to the standard Assert() macro. You just put whatever function you want to test inside it say,

     TS_ASSERT( 1 + 1 > 1 );
or
     TS_ASSERT_EQUALS( square(2),4 );

5 comments:

  1. Graph, bullet points, codes, very good post. I would say it's better if you elaborate a little bit more on the points. For example, I don't get it why "start unit testing early would lead to a better design". I'm assuming you mean a consistent design? Maybe you have omitted some really interesting details.

    Zhouyun Feng

    ReplyDelete
  2. I really liked your coverage of cxxTest; I actually understand some things better now. And, it shows that you understand it well. I also like how you point out that quick and easy testing can be gratifying, since you get instant feedback and can be reasonably sure that your code is correct if the tests pass. Something positive to keep in mind when deciding whether to write tests or not in addition to the dire predictions of project doom if you don't test.

    ReplyDelete
  3. I like how this blog is straight forward and gets right to the point. I will have to look into using that CxxTest plugin to help me better take advantage of CxxTest. It would be nice if you would explain some of the points a little more thoroughly though.

    Jon Anderson

    ReplyDelete
  4. CxxTest has an Eclipse plugin? Awesome! Anyway, well written and straight to the point. I like that. Also, points 1 and 4 are huge for me, I like know what I write works, and sometimes write stuff that works but I'm not sure why (tsk tsk tsk) and writing tests helps me figure it out sometimes.

    And I have to say: snazzy graphic!

    -Derek Anderson

    ReplyDelete
  5. Nice graph, and straight to the points. I liked how its bullet points, keeps it neat and easy to navigate. I did not know it had a eclipse plug-in I will have to give that a try, thanks!

    Cuong Nguyenn

    ReplyDelete