Chapter 1. Intro to Testing
What Is Software Testing?
In short, you can test software against a specification.
Letâs say youâre writing a simple calculator that just does addition. Before you even start, think about how it should behave. It should be able to add positive integers. It should be able to add negative integers. It should be able to add decimal numbers, not just integers. You can think of many different ways that your calculator needs to work.
Before youâve written any of the code, you know how you want it to behave. You have a specification for its behavior.
You can write these specifications in code. Youâd say, âOK, it should work this way.â Youâd make tests that added 1 and 1, 2 and 2, â1 and 5, â1.2 and 6.8, 0 and 0, and so on. When you run these tests, youâll either get a success (it works according to the specification) or a failure (it doesnât). If you ran all of your tests and saw success for each, then you can be pretty sure that your calculator works. If you ran these tests and saw some failures, then you know that your calculator doesnât work.
Thatâs software testing in a nutshell. Youâre testing your code against a specification. There are many tools (Jasmine among them) that help you automate these software tests.
Itâs important to know that itâs difficult (and often impossible) to write tests for every case. In the calculator example, there are an infinite number of possible combinations. When testing, you should try to cover every reasonable case by testing a number of different groups (integers, negative numbers, mixes of the two, etc.). You should also identify boundary conditions (zeroes, for example) and edge cases, testing as many different scenarios as possible.
Why Is It Useful?
Testing is useful for a number of reasons.
First, these tests can evaluate a programâs correctness after a change. Letâs say all the tests are passing, and then I decide I want one of my functions to be faster. I can dive in, make some changes, and see that it is indeed faster. But if I run the tests again and see that some are failing, I quickly discover that my fix has broken some part of the code. Automated testing lets me see those errors before they happen in the âreal world.â
These tests can also function as good examples for other developers. If a developer is trying to figure out how to use some undocumented part of your code, a well-written test can help him see how that piece works.
Test-Driven Development
A relatively new software development technique is called test-driven development, or TDD. The process works like this:
- Write test cases for a specific part of your code. In the calculator example, youâd write tests for adding positive numbers, negative numbers, integers, and so on. You havenât written the calculator yet, so all of these tests should fail!
- Write your code to âfill inâ the tests. Your code only serves to make all of your tests pass, and nothing more.
- Once all of your tests pass, go back and clean up your code (this is called refactoring).
Test-driven development allows developers to think clearly about the specifications before their minds are clouded with the implementation details. It also ensures that tests are always written, which is always useful.
Behavior-Driven Development
With behavior-driven development, or BDD, you write specifications that are small and easy to read. There are basically two key parts of BDD:
- Your tests must be small and test one thing. Instead of testing the entire application, you write many small tests. In the calculator example, you would write one test for each addition pair: one test for 0 + 0, one test for 1 + 1, one test for â5 + 6, one test for 6.2 + 1.2, and so on.
- Your tests should be sentences. In the calculator example, sentences would look like âCalculator adds two positive integers.â The testing framework that you use (Jasmine, in this bookâs case) should do this automatically for you.
These two tenets allow you to run your test suite and see exactly whatâs wrong at a glance. If you see a bunch of successes but one failure on âCalculator adds two negative numbers,â you know where to look.
Note
Dan North is credited with BDDâs invention. He describes the system in more detail on his website.
So, enough about testing. Whatâs Jasmine?
Get JavaScript Testing with Jasmine now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.