Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm having trouble developing unit testing approaches to testing both if the "code does what I want it to do", and testing "does my code work".

For example, I'm writing code to validate input that will be inserted into a database table, so I need to test if my code does what I want it to do - Good input is accepted, bad input is rejected. Here's some of the code, in VB.NET:

    'Let's check if we are going to update all the fields that must be updated...
    If record.table.RequiredMustBeSuplied IsNot Nothing Then
        For Each req In record.table.RequiredMustBeSuplied
            If query.UpdateFields(req.Key).value Is Nothing Then
                Throw New ArgumentNullException
            End If
        Next
    End If

So, for my unit test I would pass in a query without the proper update fields and expect that it would result in a ArgumentNullException, and a corresponding test that passes. The problem that happens to me is later on I'll notice a bug, and realize my code doesn't work correctly. For example, the correct code in this case was:

    'Let's check if we are going to update all the fields that must be updated...
    If record.table.RequiredMustBeSuplied IsNot Nothing Then
        For Each req In record.table.RequiredMustBeSuplied
            If query.UpdateFields(req.Key).value Is Nothing Then
                Throw New ArgumentNullException
            End If
        Next
    End If

So, I was able to test for problems I know about (missing fields) but not a problem I don't know about (the difference between dbnull and nothing). Is unit testing just not very good at finding these kinds of errors, or is there an approach someone could suggest?

share|improve this question
    
Hopefully the lack of code doesn't make this question to general. I just got done with a project updating some legacy code and my attemps at adding unit testing did not result in me stepping through the debugger anymore more then if I had not used unit testing. –  JEC Aug 7 '12 at 22:04
1  
I'd actually like to see an answer. I run into the same sort of thing. Is his biggest problem with setup? The language (and/or his understanding) makes it tough? –  Paul Aug 7 '12 at 22:30
    
Just a comment on code quality: the code can be simplified by having RequiredMustBeSuplied never be Nothing, but an empty enumeration instead. This makes the If redundant and hence the code cleaner. Using empty collections instead of null values is a good practice to follow, advised by many good books (e.g. Effective Java by Josh Bloch). –  Konrad Rudolph Aug 8 '12 at 10:48
add comment

2 Answers

up vote 4 down vote accepted

Unit tests should be built based on requirements and constraints that you know about. They will only ever be as complete as your thought processes. The wonderful thing, however, is that when you encounter a bug that you had not foreseen or tested, you can create a new unit test that covers that case even before you implement the fix, and then you never have to worry about that bug again. (use TDD for the win)

share|improve this answer
    
Thanks. I like your expalantion of how unit testing is an iterative process. –  JEC Aug 8 '12 at 16:30
add comment

Your specifications should cover conditions that is known in the system artifacts, such as schema and business rules. However, as many developers do, the jump into code without taking the time to study the database design or even the specific requirements. This makes constructing complete test cases error prone because the development activity becomes mingled with design activity. Usually programming dominates since it consumes most of the effort and time. My point is you can avoid many problems if you take the time to design and spec out the problem at hand before coding.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.