Test Data with YAML

What is YAML?

YAML = YAML aint markup language – that’s it ! Similar to GNU = GNU is not unix 🙂

Anyways, YAML was originally intended to write data structures in human readable format. So it has got its little own markup (if I may say). Like “—” (three dashes) represent the start of a YAML document.

How is YAML useful to Test Data?

In short converting between YAML – Hash data structure (key,value pairs) is a piece of cake and also vice versa i.e converting different data structures to yaml is extremely easy. Converting between YAML – json is also relatively easy with json gem. 

Before we go further, let me show you where I found YAML to be extremely helpful and value-add. If I have to represent different types of test data (nested as well), I can easily write up a file as below (its readable too and much less lightweight than excel) and access it in Automation code. This is useful in cases where we don’t want to maintain excel sheets, but at the same time want some readability of test data. In my case, I want to populate a web form with values, the same web form we have been practicing so far with excel and json sections.



  • YAML overview
  • How do I get the functionality
  • YAML serialization and deserialization
  • Cucumber Feature and Code

YAML overview:

The YAML cookbook is an excellent source if you would like to excel at all possibilities with YAML. A few examples of mapping between YAML and Ruby as below:






As we can see there is a YAML equivalent for almost all data structures. As mentioned above, you can read the YAML cook book for more details. Let’s get into how YAML is useful for us in Automation.

How do I get the functionality:

YAML is a module in Ruby runtime, so we have access to the module automatically. It is NOT a gem that we need to add to Gemfile. Simple right!

Some Important YAML code lines:

Convert to YAML:

We can convert an object to yaml by calling to_yaml method. It is built in

Save YAML to file:

Dump yaml to file (aka. serialization). In the below case we are converting a json string to yaml and then saving that to a file “random.yaml” (Dont worry, all of the code is posted below)

Read YAML from file:

Load yaml from file (aka. deserialization). In the below case we are reading a file “practiceform.yaml” and automatically it is read into a Hash (Dont worry, all of the code is posted below).

Accessing a value:

Load YAML from file (as above) and access it using Hash as usual.

Entire Cucumber Feature:

The cucumber feature below contains all scenarios that demonstrate how YAML works. I believe the last scenario, where we read a YAML and use that hash to input values on a web form might be a useful one. This scenario also demonstrates the fact that we can have multiple Hashes (aka. key, value pairs) in one file that can be used as a test data file. And we can load the entire test data file and access the Hashes by giving proper names (In the below example, we have accessed using “Row1”, “Row2” and so on). The file “practicform.yml” can be downloaded here.

  • The first scenario converts an array into yaml and prints it
  • The second scenario shows how we can convert json to yaml and dump to a file (serialization)
  • The third scenario shows reading yaml from file (deserialization)
  • The fourth scenario shows conversion from yaml to json
  • The fifth scenario shows a real time application with a web UI Automation scenario.
  • Download the files and put in /features/support/datafiles directory: practiceform.yaml

Step Definitions:


  1. First scenario prints the yaml
  2. Second scenario prints yaml. Creates a file “random.yaml” and then deletes it. If you like to see the file, comment the line that deletes the file
  3. Third scenario shows how to access values after converting yaml to hash
  4. Fourth scenario shows how to convert to json
  5. Fifth scenario is a practical example

Known Issues:

The browser will pop up even for non-browser scenarios (i.e. the first four scenarios), however that is fine. Ignore it.


So far, we have covered excel, json and yaml. We already have lot of power in our hands now w.r.t test data representation and consuming it from different sources. However to close the loop completely, we are going to talk about another format that has been (and is) popular for many years (trailing behind json ;)) and that is XML. Read on.