Andy said:
I have a class called Address. It holds strings to represent different parts
of an address (street, city, state, zipCode). It doesn't really have
anything in it other than a blank constructer and some default properties to
set and get the string values. How would you write tests on this class to
make sure everything is running ok? Or are tests on something like a string
container like this not really useful? Let me know what some ideas are. The
thing that would be tested for is that there are only strings being saved in
the container and nothing else. I thought of even putting a constructer to
create the address itself instead of having to assign every property.
To be honest I'm a bit ambivalent to this question.
On one hand I advocate testing everything, for various reasons (I'm
getting to those), but on the other hand, a class that holds strings
(struct with fields?), do you really foresee much problems in creating
that class?
My goal for 100% tests is to be able to do code coverage that ends up in
100% coverage. If you can attain that, it's easy to figure out what
parts of your code you're missing a test for, you can just run code
coverage.
On the other hand, I also advocate writing the tests first, so that you
can afterwards check that the class functions according to specifications.
But, if you don't want to, or can't, go to 100% code coverage for unit
tests, you should prioritize your tests, so that you don't spend time
writing tests for one class that really should've been spent on writing
tests for another more important class.
Basically, for each class/code blocks/testable unit, you should decide
on the following two criteria:
1. what is the risk of this code being wrong?
2. what is the consequence of this code being wrong?
If the answer to the above questions are:
1. low
2. little
then write the test to it last.
If on the other hand the answers are:
1. medium to high (complex class)
2. critical showstopping bug (payment system down, ordering system
producing the wrong orders, etc.)
then by all means, test it to death.
To me, it sounds like your class belongs in the first category. Not much
that can go wrong. It might have dire consequences if it goes wrong, but
you also need to decide on what could go wrong. If one thing that could
go wrong was that a field of the class was null, and this could crash
your app with a null reference exception, then it would depend on the
application wether this was "critical" or not.
I don't think anyone but you yourself can really decide wether the class
is worthy of its own test or not.
Personally I would write a test for it, but it would be written towards
the end of the project, given enough time to do it, at least with what
you've told us about it so far.