Testing is something we all know we need to do, but no one ever seems to enjoy it. Well, let’s see if we can figure out how to have fun doing this.
Didn’t we already learn how to write flawless code? Do we need to test it as well?
Well first let’s look at the reasons we might want to test our code.
- The code was inherited.
- The code was modified by someone else.
- You are human.
OK, so maybe there might possibly be a flaw in our code. We should find it before some user does. Of course, once we find it, we’ll want to fix it, and then want to insure that we did in fact fix it and didn’t break something else in the process. So we test.
The best way to test any piece of code is to figure out what will make the code fail. If none of our tests ever fail, then how can we be sure we are testing what we need to?
When you start testing, consider two things: keep track of your tests, and look for weak spots in the design, places where mistakes are more likely. You should do positive testing and negative testing.
Positive testing means running tests that in principle show the code does what we expect it to.
Negative tests instead try to supply bad input, invalid parameters, or error conditions. They test the code to see if it can handle failures or errors gracefully.
Automation can be most effectively used for positive testing. “Do this task and expect this result.” I recommend a more manual approach to negative testing. In both cases I like to write a testing framework so both types of tests can be run. This can be as simple as a list of links on a page that perform various tests. The output can be visually inspected for success or failure.
OK, so how do we have fun doing this? It seems pretty tedious to me. It is actually pretty easy: Try to find at least one flaw in your code. If you can find one, certainly you can find two. Try to outsmart yourself. I’m almost certain I’m smarter than I am.
Be creative. Be daring. Look at the problem from upside down or umop ep!sdn. It you come across good testing tools, I'd love to hear about them. Never stop learning how to do things better.