Simplifying Test Automation with PyYAML in Python

Test automation plays a crucial role in ensuring software quality. As part of this process, managing test data and configurations becomes essential. This is where PyYAML, a powerful Python library for parsing and manipulating YAML files, comes into play. In this short article, I would like to introduce PyYAML – a library I found useful during my last refactors in Python+Selenium based test framework. I will discuss advantages, provide an example use case, and guide you through the installation and usage of PyYAML.

Introduction to PyYAML

PyYAML is a Python library that enables the parsing and serialisation of YAML files. YAML is a human-readable data serialisation format commonly used for configuration files, making it well-suited for test automation scenarios.
For example, in my case, I need YAMLs, to be able to run my tests via GitLab and to execute them on BrowserStack.
PyYAML provides a simple and intuitive API to work with YAML files in Python, allowing you to easily read, write, and manipulate YAML data.

Advantages of PyYAML in Test Automation

Simplified Data Management:

  • PyYAML simplifies the management of test data and configurations by utilising YAML’s clear and concise syntax. YAML files are easy to read and understand, making it convenient for both technical and non-technical team members to collaborate on test automation projects.

Integration with Third-Party Tools:

  • PyYAML seamlessly integrates with various third-party tools, such as cross-browser testing frameworks, CI/CD or other external systems. This allows you to generate YAML files dynamically during test execution, providing flexibility in configuring and customising your tests based on specific requirements.

Example Use Case: Dynamic YAML File Creation

But why do we need to create dynamic YAMLs?

Let’s consider a scenario where you need to integrate your test automation framework with cross-browser testing tool. You may want to generate a new YAML configuration file for each test execution, specifying the desired browser, operating system, number of parallel runs, specific project and build names, build identifier and other parameters. PyYAML simplifies this process by allowing you to programmatically create YAML files based on your test execution requirements before each execution, so nothing is hardcoded (like sometimes it is suggested in documentation of some tools). One of examples: I needed to define 2 parallels if execution is related to the test environment and 4 if the test suite is being run on production.

Installation and Usage

Installation:

  • To install PyYAML, you can use the pip package manager by running the following command:

    pip install pyyaml

Usage:

  • Import the PyYAML module in your Python script:

    import yaml

  • Reading YAML:
    with open('config.yaml', 'r') as file:
    data = yaml.load(file, Loader=yaml.FullLoader)
  • Writing YAML (this is what I used in my solution):
    data = {'key': 'value'}
    with open('config.yaml', 'w') as file:
    yaml.dump(data, file)

Case study

My goal: to create fresh, customised YAML before each test execution (local or via CI/CD)

What was needed?

  • a Python file with desired data and PyYAML used
  • I named this file as create_yaml.py –> this file is run before each execution (added manually in command line when triggering locally or added as a separate command in GitLab YAML file before the test scripts, example: python3 create_yaml.py )
  • In this file I am fetching some data from CI/CD: a job name and a branch name. This way I can include this information in my dynamic YAML and customise my executions in the target tool (in this case: cross-browser tool). Note: screenshot is simplified, it doesn’t contain the code for local executions.
  • At the end, all data are being written in the execution.yml file. Note that with every execution, a new YAML file will be created.

 

Conclusion

PyYAML is a valuable tool for test automation in Python, enabling seamless integration with YAML files. Its simplicity, flexibility, and support for dynamic file creation make it a powerful choice for managing test data, configurations, and integration with third-party tools. Especially I found it usable in creation of dynamic YAML files.


Posted

in

,