Introduction

Testing, as we know, is an integral part of software life cycle. And today, due to advance in technology and need for an efficient and faster testing process, automating the testing process has become necessary.

Test automation is a software technique that uses a software apart from the software under test to run test cases. This is very critical for continuous testing and continuous delivery.

Since tests scripts need to be developed to run tests, programming languages play a huge part in the automation process. It is very important for testers to be proficient in any of the programming languages used and their frameworks to automate tests. So it is crucial to choose the right programming language to automate testing of a software.

Most preferred scripting languages used in automation testing are Java, Javascript, C#, Python, Ruby, C++. Generally, all the above-mentioned programming languages are great in technologies and they have support for a good number of automation testing frameworks and a large user community.

In this article, we will see how Python, as a programming language, supports test automation with its many available frameworks and various other useful features.

Introduction to Python

Python is an interpreted, object oriented, high-level programming language. It was created by Guido van Rossum and was first released in 1991.

Python is one of those languages that is simple and yet powerful. It has efficient high level data structures, dynamic typing and dynamic binding. Python’s simple, easy to use and elegant syntax ensures code readability and reduces the cost of program maintenance.

Python has extensive standard libraries and can help perform a lot of operations involving regular expressions, threading, databases, XML and many more. It is often called “batteries included” language due to the availability of this comprehensive standard libraries. Python Interpreter and the libraries are available for all major platforms.

There is no compilation step in Python, making it incredibly fast to run the programs and debugging Python programs is very easy. The debugger is written in Python itself, testifying to Python’s introspective power. All these features make python a very competitive and powerful programming language.

Benefits of using Python in Test Automation

Python has a number of features that can help build powerful test scripts for test automation. Some of those beneficial features are,

  • Packages

Python has a rich library of useful packages for testing: pytest, unittest, doctest,  logging, requests, Selenium WebDriver, Splinter, Hypothesis, and others are available as off-the-shelf ingredients for custom automation recipes. They’re just a “pip install” away.

  • Multi-Paradigm

Python is object-oriented and functional. It lets programmers decide if functions or classes are better for the needs at hand. This is a major advantage for test automation because one, stateless functions avoid side effects and two, simple syntax for those functions make them readable.

  • Typing Your Way

Python’s out-of-the-box dynamic duck typing is great for test automation because most feature tests don’t need to be picky about types. However, when static types are needed, projects like mypy, Pyre, and MonkeyType come to the rescue. Python provides typing both ways.

  • IDEs

Good IDE support a language and its frameworks easy to use. For Python testing, JetBrains PyCharm supports visual testing with pytest, unittest, and doctest out of the box, and its Professional Edition includes support for BDD frameworks (like pytest-bdd, behave and lettuce) and Web development.

  • Command Line Workflow

Python and the command line are like a good match like peanut butter and jelly. The entire test automation workflow can be driven from the command line. Pipenv can manage packages and environments. Every test framework has a console runner to discover and launch tests. Rich command line support makes testing easy to manage manually, with tools, or as part of build scripts / CI pipelines.

  • Ease of Entry

Python has always been friendly to beginners, whether those beginners are programming newbies or expert engineers. This gives Python a big advantage as an automation language choice because tests need to be done quickly and easily. Nobody wants to waste time when the features are in hand and just need to be verified.

  • Strength for Scalability

Even though Python is great for beginners, it’s also no toy language. Python has industrial-grade strength because its design always favours one right way to get a job done. Development can scale thanks to meaningful syntax, good structure, modularity, and a rich ecosystem of tools and packages. Command line versatility enables it to fit into any tool or workflow. The fact that Python may be slower than other languages is not an issue for feature tests because system delays (such as response times for Web pages and REST calls) are orders of magnitude slower than language-level performance hits.

  • Popularity

Python is one of the most popular programming languages in the world today. It is consistently ranked near the top on TIOBE, Stack Overflow, and GitHub. It is a good choice for Web developers, infrastructure engineers, data scientists, and test automation engineers. The Python community also powers it forward. There is no shortage of Python developers, nor is there any lack of support online.

Python Frameworks

Python offers great frameworks that help build a powerful test automation suite. Let us look at some of the most used frameworks.

  • Unittest or PyUnit

Unittest or PyUnit is the Python unit testing framework. It is a Python implementation of JUnit, the standard unit testing framework for Java. So, developers making the switch from Java to Python will find it very easy to use. Unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unittest module provides classes that make it easy to support these qualities for a set of tests.

Advantages:

    • Detecting problems early – Unit tests discloses problems early into the development.
    • Mitigating change – Allows the developer to refactor the source code during the testing stage and later, while still making sure the module works as expected.
    • Simplifying integration – By testing the separate components of an application first and then testing them altogether, integration testing becomes much easier.
    • Source of Documentation – The unit testing with doctest provides a better insight.
    • Test scenarios can be executed independently, or they can be grouped together into a Test Suite. The execution process is simple as tests can be executed only by mentioning their names on the terminal.
    • The default execution output is concise and easy to understand. This Python testing framework also has several command-line options that help in providing more detailed output.
    • The test reports are generated within milliseconds.
  • PyTest

PyTest is a test framework which allows us to write test codes using python. It is a native Python test library with super set of PyUnit’s features.

PyTest also supports parameterized testing that improves code reuse and simplifies code coverage. In addition to fixtures, suites, and test runners, PyTest has its own support for test discovery. You can select sets of tests to run based on their method names, packages, or decorators you add to your test code. Used together, these features make it easier to manage large code bases than with PyUnit.

PyTest includes reporting in plain text, XML or HTML. You can also add code coverage to PyTest reports.

Some of the advantages of PyTest are:

    • Very easy to start with because of its simple and easy syntax.
    • Can run tests in parallel.
    • Can run a specific test or a subset of tests.
    • Automatically detect tests.
    • Skip tests.
  • Behave

Behaviour-driven development is becoming more and more popular in Agile world. Behave is the test framework of python that will help build BDD tests without any complications. The nature of this framework is similar to other BDD frameworks like Cucumber or SpecFlow. The test cases in behave are written in a simple readable language. The behaviour is designed by the behaviour specs and the steps are then reused by other test scenarios.

Advantages of Behave are:

    • System behaviour is expressed by semi-formal language and a domain vocabulary that keeps the behaviour consistent in the organization.
    • Dev teams who are working on different modules with similar features are properly coordinated.
    • Building blocks are always ready for executing all kinds of test cases.
    • Reasoning and thinking are featured in details, resulting in better product specs.
    • Stakeholders or managers have a better clarity regarding the output of QAs and Devs because of the similar format of the specs.

Development of Brainbox Test Automation Framework using Python

At Brainbox, we have developed a test automation framework that can test both web and mobile applications. We have used python as the scripting language to take advantage of its object oriented and platform independent features. Python has so many great features that can help in building a very efficient test automation framework. Below are the few features that we have adopted in Brainbox Test Automation Framework.

  • Pytest

We have used pytest to run test cases. In the framework, test cases and test steps are represented in excel. The test steps are then converted into test scripts. Each test case becomes a pytest function like test_TC1(). We can run these tests and analyze the results.

  • Unittest

We have also implemented Unittest framework to maintain our framework components. We have developed unittests to all possible core library and reusable library functions of the framework, so that we can test the functionality they are intended to perform and continue to do so. This makes our framework robust, as there is no room for manual error.

  • Conftest.py

One more feature that Python programming language provides that we have adopted is Conftest.py. Conftest file allows you to define fixture functions or plugins that can be shared among all the tests in the test suite. In our framework, we have defined setup, teardown and makereport functionalities for each test case in conftest.py.

  • Command line

We have taken advantage of using command line in python to run tests. Tests can be invoked in command line and we are using customised attributes to invoke web related or mobile related tests or both. There is also customised attributes to run the test locally or on an external mobile testing platforms like BrowserStack.

  • Packages

We have widely used packages that are available in python. Apart from Selenium and Appium packages, some of the packages we have made use of are xlrd package to manipulate Excel data, parameterised package to parameterise unit tests, allure-pytest package to generate a detailed HTML report of test cases.

Summary

In summary, python is a versatile programming language that is simple, easy to learn and powerful at the same time. Test automation requires a good scripting language to efficiently build test cases. Python has the right combination of both performance and features that are useful and help in achieving the goals of automation testing.

Contact us at sales@brainbox.consulting for more information and we would be happy to assist you with testing related services.