Testing Is Easy

2014-12-20 03:03:00 UTC

Many new developers have trouble wrapping their heads around the concept of unit testing. There are a lot of reasons for this, and I think most of it comes from the overhead of learning a test framework.

An example of a powerful (but daunting) test framework is Rspec. It’s a test framework that let’s you write very readable, easy to maintain tests. The downside of its power is that it can take a while time to learn. In the case of Rspec, there are even books and websites that showcase all the things you need to learn and all the ways you can do it wrong.

When you’re struggling to just learn the syntax of a new language, all of this testing stuff can sound pretty confusing.

Why Bother Writing Tests?

When writing small apps it is common to write code, run it locally and manually test that it is working. It only takes a few seconds and you can be sure that the code works. When you have an app with hundreds of features and thousands of lines of code, the task becomes more time consuming or impossible.

Somewhere along the way, developers came up with the idea to write “unit tests”. Unit tests make sure that every method in your code works as expected. The expectation is that when all the units are pieced together, they will work properly because they all worked fine in isolation.

There are some other issues that testing solves, too. Some examples are:

  • Being able to know if you’ve introduced a bug to a once-working method.
  • Find failing code before it crashes in the wild
  • Give other developers the ability to know if they’ve installed the app correctly (just run the tests after installation)

That Sounds Hard!

Learning a new framework can be hard, but the concept doesn’t have to be. Let’s write a small method that we will write tests for later:

def add(first, second)
  first + second
end

It’s a simple addition method that adds two numbers together. Here’s an example of how you might run it:

add(2, 2)
# => 4

Great. Now let’s test it. Instead of using a scary test framework, let’s write our own library to test this code.

Your Own Testing Library

Our test “library” only has one method: test. It is a method that takes two arguments and tells you if your method is acting as expected.

# test.rb
def test(expected, actual)
  if expected == actual
    puts "PASS. #{actual} is #{expected}."
  else
    puts "FAIL. Expected #{actual} to equal #{expected}."
  end
end

Then we can write a test file in tests.rb

# test.rb
test 4, add(2, 2)

Now we can test the method (and any future methods we decide to write) by running a single file.

> ruby test.rb

PASS. 2 is 2.

Next Steps

That’s really all there is to testing. If you still are too scared to learn a full flavor test framework like Rspec, perhaps you can try an alternative like MiniTest, Riot or Wrong.

There’s no reason to not to write tests for your projects.

Author’s Note: This article is a reconstruction from memory of a great TDD article I once read but failed to find again. If you know the author, please let me know.