You may have heard of TDD (test driven development), but if you’re new to programming you may be curious to learn what a software test is and why it’s useful. Most of the literature about testing is targeted at experienced programmers and dives into the details without explaining basic things like why it’s useful to test your code in the first place and how a test works. This post aims to fill that gap.

Writing tests for your application lets you automatically ensure it’s working properly. For instance, if you’re writing a calculator app in Ruby that lets users add and subtract numbers, you would write a test for it that looks something like this:
describe Calculator do
it "should be able to add 1+1" do
Calculator.add(1, 1).should == 2
end
it "should be able to subtract 1-1" do
Calculator.subtract(2, 1).should == 1
end
end
For now you don’t need to worry about the exact syntax of how this works, just notice that this code is basically a checklist to make sure that the calculator can properly add and subtract. If we tell the Calculator to add 1 and 1, the result should be equal to 2. If we tell the Calculator to subtract 2 and 1, the result should be equal to 1.
When you run the test, if something is broken it will tell you what you need to fix. Handy!
This should give you some intuition about how software tests work, but you’re probably wondering why this kind of thing is useful. It would be easier to just write the Calculator code and use it to see if it works as expected. We can skip all that tedious test-writing stuff, right?
Not exactly…
For our contrived calculator example, it’s perfectly fine to leave your code untested. But imagine a Super-Duper Calculator with thousands of functions. Every time you edit the source code there’s a possibility that something can break. It would take forever to manually check all 1,000 functions. So you start to fear making changes to the codebase. Features don’t get shipped. Customers are unhappy. Everyone is sad. :(
If you had good test coverage of your Super-Duper Calculator, you could have run a test with one command that would have checked all 1,000 functions in mere seconds. You’d be able to write new features with confidence.
Tests sound pretty good now, don’t they? But there’s more! There are different types of tests.
The example I showed you was a Unit test, but there are other types of tests you can use. The main three are Unit, Integration, and Acceptance tests.
A unit test measures whether isolated chunks of code in your program (like the “add” function) work properly. It’s the lowest-level type of test.
These come in handy when you need to test higher-level functionality, like when multiple parts of the program need to work together, access external resources over the internet or from a database, etc.
Acceptance tests focus on the end result that the user sees. For example, an acceptance test would simulate a user filling out a signup form to make sure that the next page shows them their profile correctly. It treats the code more as a black box. Sometimes acceptance tests are referred to as “functional” tests.
For more details, check out this Stack Overflow answer.
It would be pretty hard to write software tests from scratch. Luckily, smart people have already created testing libraries for all the major programming languages and there is a lot of good documentation that can teach you how to use them. Here are some of the main ones for Ruby, Python, and Javascript:
Here’s the main points you can take home:
If you enjoyed this, you might be interested to know we’re writing a book that will teach you how to build simple web-applications for yourself. It’s called Enough to be Dangerous, and you can check it out at enoughtobedanger.us.
Thanks!
comments powered by Disqus