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 Рdict (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.

python_yaml_data

 

Agenda:

  • YAML overview
  • How do I get the functionality
  • YAML serialization and deserialization
  • Behave¬†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:

yaml_simple_sequence

 

 

yaml_simple_mapping

yaml_nested_mappings

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.

YAML started in the Ruby world, however python supports YAML too with pyyaml library.

pyyaml

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 “document.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 dict¬†(Dont worry, all of the code is posted below).

Accessing a value:

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

Entire behave 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:

Output:

  1. First scenario prints the yaml
  2. Second scenario prints yaml. Creates a file “document.yaml”¬†
  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.