Reputation: 734
I'm creating a bunch of libraries for personal use and I'm inserting custom error handing into my code.
I put these error message in lots of places (well everywhere an error may occur, or something unpredicted happens).
I'm now creating test classes for my libraries (well actually I create the test classes as I go along, but....)
I've been doing some reading on code coverage etc, and I have a question about my process (I want to get into good habits).
As mentioned above my methods do a lot of error handling.
In my tests I create 2 tests
This seems like a valid method for testing my code, but the more I read the less sure I am.
Any advice on how to improve my test is welcome (or pointers to resources on the net).
Thanks in advance,
and sorry if this seems like a 'stupid' question (which it does to me.... a litle bit)
Upvotes: 2
Views: 1871
Reputation: 4121
This sounds like a good way of testing for me. If you test each possible scenario and get every outcome of every method covered, what more is there that you could do?
Make sure to test a successful run through, and then look at your code and see everything that can go wrong. Test that it fails in the way that you expect it to, and throws the exception expected etc. So, instead of just writing two tests like you currently have, write one for each possible failure, along with a few to check that it passes when expected. This may mean writing a lot of tests.
For instance, if you have code with something like this:
if (x && y && z) {
...
} else {
...
}
then it might be a good idea to test what happens if x and y but not z occur, if x occurs alone, if y and z occur etc. This may seem petty, but it is a good idea to cover as many possible scenarios as possible.
In terms of getting into good habits, the best way to write tests is to write them as you go along. So, write a test, write the code to pass the test, repeat. This means that all code written adds value and helps you break a problem down into smaller chunks. This is known as TDD (Test Driven Development). There are plenty of places to read up on TDD online, including sites such as http://www.agiledata.org/essays/tdd.html
Hope this answer helps. If you need any extra explanation on anything I have said, let me know.
Upvotes: 3