Three ways to generate test data for your ruby automated tests

I like generating test data that is varied, but still is realistic looking and fun. These are my three favourite ways to generate test data for my ruby automated tests.

Faker

When ever I need some form of fake data, whether it be names, company names or email addresses etc, I use the brilliant faker gem. This gem makes it super easy to generate random fake data that still looks realistic (unlike a randomly generated word you can generate yourself like ‘HSKHJKUWG’). My favourite method is Faker::Company.bs, which as the name implies, generates some great BS!

require 'faker'

puts Faker::Name.name
# Nathanael Botsford
puts Faker::Company.name
# Labadie, Marvin and Kassulke
puts Faker::Company.catch_phrase
# Self-enabling bottom-line project
puts Faker::Company.bs
# grow B2C platforms

UUIDs

When ever I need to input a piece of data that needs to be uniquely identifiable, I use the UUID (universally unique identifer) capability in built in ruby 1.9.3 to generate a UUID. I prefer this to using the current date/time as it requires less formatting to get it to unique.

Ruby 1.9.3 has this in built, otherwise there’s the UUID gem.

require 'securerandom'

puts SecureRandom.uuid
# ffe71bd2-2650-4135-b366-f8da08b4b708

Quoth

A relatively newcomer (it was released last week) is my quoth gem to generate random wikiquotes. I used this in the Wikimedia example tests to append interesting content to my test user page. You could use this in tests that need to insert blocks of content where you may like something with variety and that is interesting.

require 'quoth'

puts Quoth.get
# If I have ventured wrongly, very well, life then helps me with its penalty.
# But if I haven't ventured at all, who helps me then? ~ Søren Kierkegaard

Summary

I find these three methods useful. What do you use to generate test data? Or do you use hard coded test data?