tags:

views:

115

answers:

5

Hi,

I just read question that answered what were desirable features of unit tests, but what should be avoided? What makes a unit test "bad"?

What are the worst unit tests you've seen? (For example. I remember a developer telling me he once found a test suite with plenty of methods but completely devoid of any asserts).

I'm particularly interested in slightly more subtle and specific problems with unit tests e.g. suppose you have a test-suite that runs quickly with good coverage, what problems could it still have?

+1  A: 

The best unit tests were simple to read and understand. Quick to execute. Tested specific functionality, well refactored and were maintained.

The worst were not the above.

Preet Sangha
+2  A: 

Tests that are brittle usually have an unacceptable maintenance overhead which will evenutally lead to the tests not being updated, remaining in a broken state, and not being run since they are out of synch with the source code.

Brittle tests usually have dependencies on file system, registry keys, databases etc... These are fine with Integration and System tests but sometimes I see tests masquerading (spelling?) as unit tests with these properties and that's usually a problem.

Ben Cawley
+1  A: 

In the spirit of CW, I knew this would come in handy one day.

Also you did ask for a specific one :) See the MAGIC block below

@Test
      public void testCheckForDuplicateCustomer() {
            //List<CustomerSearch> customerInfo = null;
            String customerName = null;
            boolean status = false;
            try {
                  status = custSearchService.checkForDuplicateCustomer(customerName);

/*************/ MAGIC BEGINS HERE
                      if(status){
                            assertEquals(true, status);
                      } else {
                            assertEquals(false, status);
                      }
                      /**************/ MAGIC ENDS HERE
                } catch (Exception e) {
                      //fail();
                }
          }
JoseK
That made me smile, so I upvoted. However, I'm really looking for examples of code that isn't necessarily bone-headed but doesn't make a good unit test.
Adrian Mouat
+3  A: 
  • Tests with external dependencies (DB, file, server, time...)

  • Tests that depend on each other

  • Tests that verify the implementation rather than the behavior

  • Tests that are so slow that no one execute them

  • Tests that test too much things

There is also the TDD anti-patterns.

philippe
Good link! I'd forgotten about that post, but I found that I'd actually commented on it. It's a small world...
Adrian Mouat
I honestly don't mind a test having external dependencies as long as they are not significantly slowing down the test and they work for everyone. Often it's more work than it's worth to mock them out (I'm thinking specifically of DBs and files here).
Adrian Mouat
Some dependencies are annoying, think about a missing configuration or data file, or a test attempting to create a file in directory without write permissions...
philippe
I'm also wondering about the implementation vs behaviour point, but I don't have enough room to discuss it here. Perhaps this is more sbjective than I first thought...
Adrian Mouat
Tests that verify the implementation break when the implementation changes. Tests must assert what is done and not how it is done. Tentative example: test of some data storage class should use the method to retrieve the data, not directly peek in the storage array because it will break if the array is replaced with a vector.
philippe
+1  A: 

For me readability is King when it comes to unit tests. If I can't read and understand the test in 2 seconds, there's probably something wrong. Any test that's more than 5 lines long had better have a pretty good excuse.

Sometimes people take the refactoring too far and I have to look at various helper classes or parent classes to find out what exactly is being tested. Always keep readability in mind when refactoring tests. sometimes it's better to leave a bit of duplication in there if it means the test is clearer.

Jonny Cundall