tags:

views:

71

answers:

3

Let’s say I have a function that accepts a user name and password, it then retrieves the record from a database and performs the following checks on that data

  • Today’s date is outside a date range
  • User is disabled
  • A parameter comparison is made

If any of these conditions are true an exception is thrown.

Obviously I want to write my unit test to test his logic, however if I do an exception will be thrown and my test will fail, which isn’t correct - is it?

+5  A: 

Since you don't say which testing framework you are using I can't go into specifics, but the framework will allow you to mark a test as expecting an exception. This will stop the test failing when an exception is thrown as well as allow you to check the type and contents of the exception itself.

On the other hand, if you are using a framework that doesn't support this (though I don't think any don't) you can manually add a try...catch block inside the test so the exception doesn't propagate out and cause the test to fail.

Martin Harris
+1 - exactly what you should do. Most unit test frameworks allow you to go further, and will fail if an expected exception is not thrown. Unit tests should test not only that functions work when given the conditions to succeed, but fail appropriately in the absence of those conditions.
Dominic Rodger
+1  A: 

I'm not sure I see the problem here. There is a contract between a caller and callee and, whether that contract involves an exception being thrown, or an error return code (such as if the exception is caught within the callee and translated to a return code), you just need to check that the problem is signalled somehow.

For example, if your contract states the getDbRow() will throw DbNotOpenException if the database isn't open, do something like:

db.Close()
try:
    x = db.getDbRow()
catch DbNotOpenException e:
    return PASSED_TEST
return FAILED_TEST

In other words, if your unit tests expect a specific exception for a given circumstance, they should trap it and report that as okay. They should flag a failure if that exception doesn't occur.

paxdiablo
+1  A: 

Yes, your test will correctly throw an exception, but in most (all?) test frameworks you can specify the expected exception and the test will pass.

In MSTest and NUnit

[ExpectedException(typeof(ArgumentException))]

Just be wary of cases where exceptions might be thrown but for reasons other than those you expect.

Ed Guiness