Testing is a huge huge topic, and I don’t claim to be the best at it, but that said I feel like I should talk about it given my experience, especially for the members of this audience (and also for my friends coding in Team Hack). Plus it’s a good way for me to level up — often talking about stuff leads to me learning it better. Let’s jump in!
What is Testing (in the context of Programming)?
Testing is the act of checking code that’s been written (either by you or someone else) and verifying that the code does exactly what it’s supposed to do — with confidence.
How Do You Feel Confident?
One way is to have others review your work.
In the old days of my programming career, all I had to do to show something was working was to have it checked by my boss. A lot of times I got tasks like:
- Can you update this copy?
- Can you add some more padding to the top and bottom of this section?
- Can you create a submission form that upon completion sends an email to my inbox?
My boss was typically the single source of truth — so if he liked the work I’d finished, I was confident in my work. If he had some issues with the work I’d done, I’d have to go back and “fix” until he was happy again. If there were a lot of problems with my work, I’d lose some confidence until I could fix the issues and get approval again.
Sometimes you have more than one boss
As I started leveling up in my career, I began making apps and websites that more people besides my boss would consume. In addition to my boss, I had project managers, C-level executives and marketing managers also giving input. Whatever problems they had, I had to make sure my code worked in their “scenarios” as well — we can call these scenarios “acceptance criteria” (AC for short), which is pretty standard in the corporate world. The more ACs I covered for, the more confident I felt again about my code working despite different users. Note: ACs grouped together are often called “stories”.
Testing For Hundreds, Thousands, Millions of People
I’ve now reached a point in my career where the work I do isn’t just going to be reviewed by one boss or a group of executives. It’s also going to be reviewed by millions of potential customers who will use the “app” that myself and my teammates are building.
How can I prove to myself (and my boss/exec team) that the code works? Some human testing is okay, but we can’t really corner every edge case that millions of people have. Or can we?
Enter the Computer
It turns out computer can do testing as well. Computers are really good at handling tasks, especially repetitive tasks. Computers are also very good at taking various inputs and generating responses in true or false output. Finally, computers can do millions of tasks in very little time — thank you 2.8 Gigahertz processor with eight cores! (That’s 2.8 billion instructions a second x 8 different core units). Nice.
So you can “test” different scenarios and have a computer help you check whether the code you work can put up with the stress of millions of “potential simulated customers”.
The more edge cases you test, the more likely you can feel 100% confident that your code is good for use regardless of customer. That’s a pretty bold assumption, but if the computer is backing you up — you’re in a better position, as last I checked — robots can’t lie.
Set up the Tasks, Have the Computer Verify
Back to some of the work I did in the past — how could I use a computer instead? With some creative thinking and computer savviness — you’d be surprised at how much of the tasks we do as humans can also be done by computers:
- Inspect whether the copy on the website is updated
- A computer can do that with regular expressions across hundreds of files in milliseconds.
- Check that the padding is updated to desired padding for top and bottom
- A computer can do this by running a diff between file 1 and file 2 and verifying the padding matches the desired padding.
- Create a form and on submit, send an email to his inbox
Change The Input, Test For More Edge Cases
It’s not good enough these days if the test just shows that the code works for the problem it solves. We should also show that if we change the inputs, or change the scenario of our code use — e.g. put into place with other production code — we don’t get unintended side effects.
- Updated copy should not break other existing copy — can we prove this with a computerized (aka automated) test?
- If we change the screen size of our app to some other size, will our automated test show us an error (e.g. websites are responsive nowadays so they have to look good on mobile, tablet and desktop screens).
- If we create a form, can we verify that the form actually has all the correct fields and inputs? Can we verify that if we add more content to the email than required that we still get an email? Will the email contain garbage or will it account for any undesired information and clean the content?
But, This is More Work For Me!
You’re absolutely right. I felt this way too. But I told myself, I’m writing code again for MILLIONS of potential users. It has to work — because otherwise millions of potential users can take their business elsewhere — and that’s a conversation I’d not like to have with my organization if I caused that problem.
Plus it’s a mind shift — if you really want to feel like you’re a professional, stand behind your work and show that you’ve tested it. The more confidence you can build in showing that your code works under all kinds of edge cases, the more you can feel confident in yourself as a programmer. So get testing!
Good News, There Are Tools & Frameworks You Can Use
A lot of other programmers out there have already thought about how to write tests for their software. You don’t have to reinvent the wheel, instead you can read up on testing software design patterns as well as look up libraries and frameworks designed to help you test accurately.