Dead Simple Tests in ruby
Why
I love doing tests in Ruby. There are so many great testing frameworks: Rspec, minitest, test-unit, and more. I like to code with confidence!
The biggest barrier for me to start up coding in Ruby now is that I want to have tests with me. Getting Rspec setup outside of rails is a whole article which I never remember each time. (There’s nothing wrong with other test frameworks, I just love my Rspec.)
Setting up Rspec on a new project time has taken me 30 min sometimes. I never feel productive doing it, even though I know the result.I can setup a skeleton project on github and clone it each time, but how much ‘boilerplate’ do I want in my code??
I want something so simple, that even when I’m hacking through some algorithms, I can just start having tests, and I think I have found a way to do Dead Simple Tests in Ruby. (No, this will not be another gem.)
How
The way I do Dead Simple Tests in Ruby is:
puts method_under_test(value_to_test_for) == intended_result
That’s it! I have to do it in the same file, but for anything less than a rails project, this will suffice. :-D No need to import a file or gem. No need to make sure the tests associate with the right method in a different file. I just use it on the spot.
Honestly, I’m surprised I didn’t do this sooner. It would have saved a lot of frustration in setting up Rspec, or getting lost in documentation online, or getting lost looking for that article or github repo.
Quick example
How I use these tests on a short project, like say writing a new division method:
First line in division.rb file:
puts divide(nil, nil) == nil
Run the file with command: ruby division.rb
:
divide.rb:1:in `<main>': undefined method `divide' for main:Object
(NoMethodError)
An error will be there since there’s no method named divide
, so
let’s fix that. Now the file should look like:
division.rb:
def divide(num, den)
end
puts divide(nil, nil) == nil
Now run ruby division.rb
again and the output will be:
true
Yes, there’s the first test, dead simple, right?
Now, let’s add on with a test that will require a result:
division.rb:
def divide(num, den)
end
puts divide(nil, nil) == nil
puts divide(1, 1) == 1
Now running ruby division.rb
produces output:
true
false
Boom. The first test passed, now the second one is failing, so let’s fix it:
division.rb:
def divide(num, den)
num/den
end
puts divide(nil, nil) == nil
puts divide(1, 1) == 1
run ruby divide.rb
again and get output:
true
true
Now just keep repeating with more tests, code for those tests, refactor, and repeat.
I love using this when testing sorting algorithms, my tests become so easy to write:
puts sort(Array(1..10)) == Array(1..10)
and:
puts sort(Array(1..10).reverse!) == Array(1..10)
This makes generating the tests so easy and simple to verify.
After a handful of tests, it gets tricky to keep track of all the
true
, so I usually comment out old ones and keep the current one I
am working on. To run them all again, I just uncomment the tests.
What
I learned to make super simple tests in Ruby so that I can basically
start any small project with it just by using puts
and ==
. When I
can get started on a project fast, I expect to get my tests
fast… that means I can go fast, stay clean, have fun programming.
This method is so simple, it allows me to go fast with tests. It also keeps my file clean and simple. I can also start coding, get this: without the Internet! I just need Ruby running and I’m good to go!
Stay testing, my friend. ;-)