Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Software Testing Strategies

You're reading from   Software Testing Strategies A testing guide for the 2020s

Arrow left icon
Product type Paperback
Published in Dec 2023
Publisher Packt
ISBN-13 9781837638024
Length 378 pages
Edition 1st Edition
Arrow right icon
Authors (2):
Arrow left icon
Matthew Heusser Matthew Heusser
Author Profile Icon Matthew Heusser
Matthew Heusser
Michael Larsen Michael Larsen
Author Profile Icon Michael Larsen
Michael Larsen
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Part 1:The Practice of Software Testing
2. Chapter 1: Testing and Designing Tests FREE CHAPTER 3. Chapter 2: Fundamental Issues in Tooling and Automation 4. Chapter 3: Programmer-Facing Testing 5. Chapter 4: Customer-Facing Tests 6. Chapter 5: Specialized Testing 7. Chapter 6: Testing Related Skills 8. Chapter 7: Test Data Management 9. Part 2:Testing and Software Delivery
10. Chapter 8: Delivery Models and Testing 11. Chapter 9: The Puzzle Pieces of Good Testing 12. Chapter 10: Putting Your Test Strategy Together 13. Chapter 11: Lean Software Testing 14. Part 3:Practicing Politics
15. Chapter 12: Case Studies and Experience Reports 16. Chapter 13: Testing Activities or a Testing Role? 17. Chapter 14: Philosophy and Ethics in Software Testing 18. Chapter 15: Words and Language About Work 19. Chapter 16: Testing Strategy Applied 20. Index 21. Other Books You May Enjoy

Introducing FizzBuzz

When we interview programmers who will write code to help with testing, we like the exercise FizzBuzz. The exercise requires the programmer to understand conditionals (which are if statements), looping, and the modulus operator (which is the remainder in division). Let’s see what a typical assignment might look like.

In the children’s game of Fizzbuzz, players rotate, keeping a count that starts with one. If the next number is divisible by three, players say “Fizz.” If it is divisible by five, they say “Buzz.” If it is not divisible, they say the number. The goal is to write a computer program that runs on the command line and takes in the number to count up to in FizzBuzz math, then puts the output on the screen.

Matt wrote up an implementation of FizzBuzz in Ruby. Instead of the most powerful constructs of the language, he used the ones that were easiest to read. The following output could be “tighter,” by far, but it was designed to be readable by nearly all audiences. The examples for the rest of this book will try to match this style. For a book with a more powerful Ruby style that also includes tests, we like Poodr, or Practical Object Oriented Programming In Ruby, by Sandi Metz. That book also has good coverage of testing concepts. If you aren’t a Rubyist, our GitHub repository contains examples in Python.

GitHub

For now, here’s the heart of the code; the entire program is available on GitHub at https://github.com/PacktPublishing/Software-Testing-Strategies/blob/main/Chapter03/ruby/FizzBuzz01.rb.

This code takes a command-line argument for how high to count, checks to make sure that it is valid, and then spits out numbers. The heart of the program is nine lines of code:

count_to = ARGV[0].to_i;
if !defined?(count_to) or count_to<1
  puts "Use: FizzBuzz01.rb (count)\n"
  puts "Where count is a round number of value one or higher"
  abort("");
end
for count in 1..count_to do
  if count % 3 == 0
    puts "Fizz"
  elsif count % 5 == 0
    puts "Buzz"
  else
    puts count.to_s();
  end
end

It’s worth noting that Matt created four syntax errors just by re-typing the code into this book. Here’s what happens when it runs on a Mac, selecting an output from 1 to 12:

Figure 3.4 – Fizzbuzz output from Matt’s laptop

Figure 3.4 – Fizzbuzz output from Matt’s laptop

If the four errors just to write a dozen lines of code doesn’t give you pause, the program has a significant defect in it. Can you see it?

You won’t see it in the output. I’ll wait. Go read the code.

Okay. Here’s the problem.

The numbers 15, 30, 45, and 60 are all divisible by both three and five. The preceding program will divide 15 by 3, decide it is “Fizz”, and exit. We need something more robust.

The obvious problem with FizzBuzz is that it has problems. The programmer will have to set it up, run it, and check the output. If they find another problem, the programmer will need to make another change and run it again. This change-run-check dynamic is sometimes called debugging. It is the only way to run the program because the business logic to calculate the results, the input, and the printing are all combined into one “unit.”

At the time of writing, the only way to test this is from the customer, or user, perspective. That isn’t the end of the world as it is only nine lines of code – but as programs get larger, debugging becomes more and more painful. It would be better to compose our software of smaller, independent units that work a much larger percentage of the time. That’s where unit testing comes in – but for it to work, we need to create code libraries that can be independently tested.

If a program can’t be independently tested, separate from the user interface, some people call that untestable. We don’t go that far as our hands and fingers seem to work just fine.

Still, unit tests allow us to get into the program so that we can define how it should work. They work as living documentation for the expectations of each component. As discussed earlier, a program that has 20 units that are 95% reliable, for some definition of reliable, is going to be massively more reliable than one where the units are 70% reliable.

Fizzbuzz needs unit tests.

So, let’s define a unit, unit testing, and fix this thing.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image