Last Updated: February 25, 2016
·
1.826K
· Marko Klemetti

Serene testing with simplicity

If you have ever tried Unit Testing or even Test-driven Development, you already know it’s not going easy to maintain the same velocity and rhythm you’re used to. With testing it’s just very easy to get lost spending your time on everything else than what’s important.

Read the full article at mrako.com

Think Simple

Simplicity is very rarely a weakness in software code. The only important thing is to meet your customers needs, and the simpler you can achieve it - the better.

Avoid spending time on complex test preparation or writing unnecessary boilerplate code to get your tests working.

Fail Early

To get the most out of unit testing, it is very important to execute your tests all the time. If you don't, or can't, run the tests after each change - it is very probable you don't have enough motivation to write them hand-in-hand with the actual program code.

Fail Fast

If your tests take time to run, you will quickly run into another major obstacle: waiting. The purpose of unit tests is to write the verification so that the feedback could be received as quickly as possible. If you don't limit the scope of your unit tests, and stub or mock your time-consuming interfaces, the total execution time will soon get out of hands. And then you will not run the tests anymore.

Navigate Quickly Between Code and Tests

It is natural to switch between your tests and the code all the time. If switching is complicated or takes time, you have once again created an unnecessary obstacle for smooth testing.

Try to avoid:

  • Using mouse to switch between the code and the tests
  • Continuously searching for the tests or the code file
  • Writing all tests (or all code) first and only until then switching to the code (or tests)

Auto-test

I've found Guard to be the best tool so far. It is very easy to add into your project and almost as easily configured. I've added Guard to the Roman Numerals Example.

To use Guard, you need to make sure you have installed RVM (see Introduction) and Bundler. Then add Guard to a Gemfile in your project root:

group :development do
  gem 'guard'
end

Now you have installed Guard, and you can create your Guardfile. Guardfile for the example project is making Guard run RSpec when spec/*_spec.rb or any other `*.rb file is updated:

guard "rspec", :version => 2 do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^(.+)\.rb$})   { |m| "spec/#{m[1]}_spec.rb" }
end