Alex Rivera | Logout

How do you know what to test when writing unit tests?

Asked 2008-09-15T13:03:22.050
128

Using C#, I need a class called User that has a username, password, active flag, first name, last name, full name, etc.

There should be methods to authenticate and save a user. Do I just write a test for the methods? And do I even need to worry about testing the properties since they are .Net's getter and setters?

Edit
Report

5 Answers

10

The rule is that you have to test every piece of logic you write. If you implemented some specific functionality in the getters and setters I think they are worth testing. If they only assign values to some private fields, don't bother.

answered 2008-09-15T13:07:50.790
4

Another canonical answer. This, I believe, from Ron Jeffries:

Only test the code that you want to work.

answered 2008-09-15T19:04:27.953
1

As I understand unit tests in the context of agile development, Mike, yes, you need to test the getters and setters (assuming they're publicly visible). The whole concept of unit testing is to test the software unit, which is a class in this case, as a black box. Since the getters and setters are externally visible you need to test them along with Authenticate and Save.

answered 2008-09-15T13:08:05.330
1

Don't test obviously working (boilerplate) code. So if your setters and getters are just "propertyvalue = value" and "return propertyvalue" it makes no sense to test it.

answered 2008-09-15T13:13:45.643
1

I would recommend writing multiple tests for your Authenticate and Save methods. In addition to the success case (where all parameters are provided, everything is correctly spelled, etc), it's good to have tests for various failure cases (incorrect or missing parameters, unavailable database connections if applicable, etc). I recommend Pragmatic Unit Testing in C# with NUnit as a reference.

As others have stated, unit tests for getters and setters are overkill, unless there's conditional logic in your getters and setters.

answered 2008-09-15T14:09:04.997

Your Answer