Creating random test data with Ruby and Faker

Nov 12, 2012 00:00 · 182 words · 1 minute read

To test an application properly, eventually you’re going to need some data to test with. Creating this can be pretty tedious, so there’s a few solutions out there for creating bulk, semi-random data with utilities like FactoryGirl.

As far as I know, there isn’t a FactoryGirl-esque solution that will directly inject data into a CoreData stack - and in any case, I needed the data as JSON. So here’s a quick rolled-my-own solution that uses Ruby and a couple of gems - Faker and Random_data.

Both of these will need to be installed prior to using the Ruby script:

gem install faker

gem install random_data

This gives you the ability to create various randomly-generated values, such as names, UK addresses and post codes. This Gist shows a quick example of how to generate some random values, turn that into JSON and then save to a file:

[gist id=4260564]

(The environment setup at the top is to fix an encoding issue that occurred when embedding £ symbols in the dictionary - this may or may not be a local problem on my system.)