views:

687

answers:

9

On Proggit today I was reading the comment thread on a submission entitled, "Why Unit Testing Is A Waste of Time".

I'm not really concerned with premise of the article so much as I am with a comment made concerning it:

The stem of the problem is that most “units” of code in business software projects are trivial.

Change the size of the unit until it is no longer trivial? Who the hell defined the unit of code as a single function or method anyway!?

and

Well, some of the guys I worked with wanted to define a unit as single functions. It was completely stupid. My favorite definition of "unit" is: the smallest piece of the code that can be usefully tested.

Are we spending too much time just to Mock out some objects and test a trivial piece of code and not really adding anything of value?

What should a "unit" be when Unit Testing? Are function level tests too granular?

+3  A: 

I'd say a unit is the smallest meaningful portion of work that can be separated from other steps in pseudocode.

For example, if you're trying to do a series of steps to process some data, like

  1. scan the dataset for the min, max, median, mean
  2. generate a histogram
  3. generate a remapping function
  4. remap the data
  5. output the data

Each of those steps would be a 'unit', and the entire series is itself also a 'unit' (to test that the cohesion between each one is working). Any one of these steps could be four functions (for the first step, if the programmer is running four loops), or one function, or whatever, but in the end, known inputs should give known outputs.

mmr
+2  A: 

Nothing is trivial, if you take into account the Murphy's Law.

Jokes apart and assuming an OO environement, I approach Unit Testing taking a class as the unit, because often the various methods modify the internal state and I want to be sure that the state between various methods is consistent.

Unfortunately, often the only way to check consistency is to invoke various method of a class to see if they fail or not.

akappa
+3  A: 

I've always tested at the function level, and that's worked just fine. The big point for me is that unit testing tests the contract between two pieces of code. The unit test just acts as a caller, and ensures that the code being tested (no matter how large - a single function or a huge, nested library that takes 30 minutes to test) returns results in a predictable way.

The whole point of a unit test it to ensure compatibility (by not breaking intereactions) and ensure predictable results. Testing at any place there's an exchange of information will help stabilize your application.

UPDATE: I've also always added a unit test whenever a customer or user reports an edge case that breaks an interaction in my application. Fixing it isn't sufficient for me - adding a unit test to ensure that the fix holds prevents regression, and helps keep things stable down the line.

rwmnau
If you're debugging a container, what's the sense of testing individually put, fetch and reduce? A serious test should involve all them in a single test function.
akappa
I'm not sure I understand what you're asking, but as usual, it depends - if a single "This container acts as I expect" using some test data is okay, then combine them into a single test. If you'd like to see functionality broken out, then break it into multiple tests. It's also worth mentioning that unit tests (for me, at least) serve as a test point - if I want to force an app to misbehave, I can modify my test slightly, instead of having to break an object in the context of the larger application, I can test a single object or feature of an object, per my tests.
rwmnau
A: 

In general, the smaller you can make your units, the more useful and effective your unit tests will be. The whole point of units tests is to be able to localize any newly introduced bug to a small part of the code.

Chris Dodd
+1  A: 

To me, a "unit" is any significant behavior of a class that, in 8-12 months down the road, I'm not going to remember.

And with well-written unit tests (think BDD), I won't have to, because my test will describe to me and verify the code in plain English.

Jeremy Frey
+10  A: 

It may seem trivial to quote Wikipedia, but I think it's very succinct and accurate in this case:

A unit is the smallest testable part of an application.

This seems to agree with the comment in your question that a unit is "the smallest piece of the code that can be usefully tested". In other words, make the unit as small as you possibly can such that it still makes sense to the developer/tester by itself.

Often you will want to test parts of a project in isolation, and then test how they interact in combination. Having various tiers (levels) of unit testing is often a wise thing to do, as it helps insure that your code is working as it should on all levels, from individual functions up to entire self-contained tasks. I personally do not believe that it is wrong, or even unhelpful, to test individual functions, so long as they are doing something useful in themselves, which can often be the case.

To be quite honest, there is no definite or rigorous definition of a "unit" in "unit testing", which is precisely why the vague term "unit" is used! Learning what needs to be tested and at what level is a matter of experience, and quite often, simply trial and error. It may sound like a slightly unsatisfying answer, but I believe it is a sound rule to follow.

Noldorin
@Noldorin: <- Wisdom at its finest :) -- Nearly anything I take from Wikipedia is with at least a unit of salt. So, if a 'grain' works, how about a pinch...now a tablespoon. - However, just like salt in a recipe, if the unit gets too large, then the test itself becomes overbearing.
dboarman
A: 

Change the size of the unit until it is no longer trivial? Who the hell defined the unit of code as a single function or method anyway!?

If it is too difficult to test a "unit" defined as a method, then it is likely that the method is too large or complicated for authoring a unit test.

I follow a similar methodology suggested by rwmnau, and test at the method level. In addition to creating one test per method, I create additional tests for the different code paths in each method. At times, stressing all of the code paths can become complicated. My challenge is to try to avoid writing the types of methods unless there is no better solution in terms of performance and code complexity.

I also use mocks to test contracts between components.

Steve Guidi
+3  A: 

A "unit" should be a single atomic unit for your definition. That is, precisely what defines a "unit" for unit testing is rather subjective; it can depend rather strongly on precisely what your architecture is, and how your application chooses to break down functional units. What I'm saying is that there is no clearly defined "unit", except for what you define. A "unit" can be a single method which has a single side effect, or it can be a related set of methods which together define a single, coherent set of functionality.

Rationality is rather important here; it's entirely possible to write unit tests for each accessor of each class that you have; however, that's clearly overkill. Similarly, it's possible but silly to define your entire application as a unit, and expect to have a single test to test it.

Generally, you want a "unit" for testing to describe one clearly defined, clearly distinct chunk of functionality. At each level of viewing your application, you should be able to see clear "atoms" of functionality (if your design is good); these can be as simple as "retrieve a record from the database" for a low-level view, and as complicated as "create, edit, and delete a user" at a high-level view. They're not mutually exclusive, either; you can easily have both as unit tests.

The point to be taken here is that the unit you want to test is the unit that makes sense. You want unit testing to validate and verify functionality; so what a unit test tests is what you define as functionality. What is a unit of functionality? That's up to your individual situation to define.

McWafflestix
+1  A: 

I suspect that "unit testing" is a misused term. Nowadays, the term isn't just used to refer to testing a small piece of code, but it's used for any test that's automated.

I tend to write my tests before writing my code, so I can't tell you when I first write it if it's going to cause the creation of a few new lines, a new method, or a new class.

So, in a word: mu.

Andrew Grimm