Unit Test

| 4 Comments

"Unit testing is the execution of a complete class, routine, or small program that has
been written by a single programmer or team of programmers, which is tested in
isolation from the more complete system" (Code Complete 2). Before we get into unit
tests, we should know the concept of a unit in a program. A unit is the smallest
testable part of an application. A unit can be a function, procedure or a class in object-
oriented programming. Thus we can say that unit testing is the most basic test in a
program.
Unit testing is very important since it actually helps us save a lot of time in big project
development. I am going to list some reasons why a programmer should always write
unit tests.
Firstly, "unit testing is easy to write" (Code Complete). It doesn't cost you a lot of
time to write unit tests. However, you will find that spending time on unit testing is
worth it, as without unit testing, it is easy to run into unexpected errors in debugging.
Secondly, writing unit tests can help you understand the design of your project. One
of the goals of writing tests is to make our program fail, so we have to think of all
kinds of situations to make it happen. This approach in thinking gives us a good
chance to refresh our understanding of program design.
Thirdly, "unit testing exposes requirements problems as soon as possible" (Code
Complete). Since the goal of testing is to make the program fail, programmers have to
take many different situations into account. Therefore, we have to ask our client if a
situation is possible or not, during this procedure, we may find more requirement
details.
After reading the above reasons, I think that you may agree with me that unit testing
is useful and time-saving. What's more, there are many test frameworks such as
cxxTest. The framework cxxTest makes unit testing easy to write. However, as a
beginner I run into a few troubles. For example, do make sure you include the file that
you want to test in the header. I know this is a stupid mistake, but as a beginner it is a
frequent one. Also, I have some issues with TS_ASSERT_EQUALS problem. When I
finished my code and tried to test my assertion, the terminal showed me an error
message with a bunch of weird numbers. I definitely cannot understand these numbers
and so googled them. Fortunately, I found out that the two parameters in
TS_ASSERT_EQUALS are different types but the same value. More specifically, we
know that NULL is a macro and it actually is integer 0 in C++. Yet
TS_ASSERT_EQUALS requires the two parameters be the same type. To solve this I
defined another constant variable (const char* nul = NULL), and passed it to
TS_ASSERT_EQUALS. Finally, it worked and showed 100% test passed.
All in all, cxxTest framework is very useful and convenient since writing assertions is
only one sentence. However, now I am still a beginner and need to spend more time
practicing how to write good unit tests.

4 Comments

Good use of quoting the textbook in your descriptions, it strengthens the legitimacy of your statements. I also like that you provided a clear example of an issue you had with cxxTest without dwelling on it, yet still included a resolve.

-Anna Mandy

I feel like your argument that unit tests are "easy to write" and "[don't] cost you a lot of time" are unsupported. If you start a paragraph with such a statement, I would typically expect the remainder of the paragraph to support that argument.

Bobby Homan

I also think testing is one of the most important parts of constructing a program. It makes sure that our program is doing exactly what its supposed to be doing.Although is seems like it takes a lot time to write test code, actually, it saves a lot of time because it is almost impossible to fix bugs after you finish entire project.

Wenliang Tong

Hello Fan,

I learned something new about how CxxTest and C++ work by reading your blog, thank you :)

Bela Demtchouk

Leave a comment

Recent Entries

Unit Test
"Unit testing is the execution of a complete class, routine, or small program that has been written by a single…
Subversion
"Version control system, also called source code control system, is a place to store all the various revisions of the…
Benefits and challenges in team work
I was really happy when my partner and I finished the first iteration and I realized the benefits of…