Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a unit test? [duplicate]

Tags:

unit-testing

Possible Duplicates:
What is unit testing and how do you do it?
What is unit testing?

I recognize that to 95% of you, this is a very WTF question.

So. What's a unit test? I understand that essentially you're attempting to isolate atomic functionality but how do you test for that? When is it necessary? When is it ridiculous? Can you give an example? (Preferably in C? I mostly hear about it from Java devs on this site so maybe this is specific to Object Oriented languages? I really don't know.)

I know many programmers swear by unit testing religiously. What's it all about?

EDIT: Also, what's the ratio of time you typically spend writing unit tests to time spent writing new code?

like image 896
Tyler Avatar asked Jul 13 '09 21:07

Tyler


1 Answers

I'm Java now, before that C++, before that C. I am entirely convinced that every piece of work I have done, that I am now unashamed of, was enhanced by the testing strategies I picked. Skimping the testing hurts.

I'm sure that you test the code you write. What techniques do you use? For example, you might sit in a debugger and step through the code and watch what happens. You might execute the code against some test data someone gave you. You might devise particular inputs because you know that your code has some interesting behaviours for certain input values. Suppose your stuff uses someone else's stuff and that's not ready yet, you mock up their code so that your code can work with at least some fake answers

In all cases you may be to some degree Unit Testing. The last one is particulalry interesting - you are very much testing in isolation, testing your UNIT, even if theirs is not yet ready.

My opinion:

1). Tests that can easily be rerun are very useful - catch no end of late creeping defects. In contrast testing sitting in a debugger is mind-numbing.

2). The activity of constructing interesting tests as you write your code, or BEFORE you write your code makes you focus on your fringe cases. Those annoying zero and null inputs, those "off by one errors". I perceive better code coming out as a result of good unit tests.

3). There is a cost of maintaining the tests. Generally it's worth it, but don't underestimate the efforrt of keeping them working.

4). There can be a tendency to over-ephasise Unit Tests. The really interesting bugs tend ot creep in when pieces are integrated. You replace that library you mocked with the real thing and Lo! It doesn't quite do what it said on the tin. ALso there is still a role for manual or exploartory testing. The insightful human tester finds special defects.

like image 160
djna Avatar answered Sep 28 '22 01:09

djna