Unit testing is an important part of designing and developing code. Here are 5 reasons why.
- Unit testing allows you to test your code all the time, in an easy way. Instant gratification, if you will.
- 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.
- 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.
- 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.
- 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):
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.
TS_ASSERT( 1 + 1 > 1 );
or
TS_ASSERT_EQUALS( square(2),4 );
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.
ReplyDeleteZhouyun Feng
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.
ReplyDeleteI 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.
ReplyDeleteJon Anderson
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.
ReplyDeleteAnd I have to say: snazzy graphic!
-Derek Anderson
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!
ReplyDeleteCuong Nguyenn