Blog>>Quality assurance>>Testing>>The popularity of Python for network test automation and more

The popularity of Python for network test automation and more

Test automation is a rapidly and dynamically evolving element of software development. Automation saves time and resources in large-scale systems by transferring repetitive activities from human hands to a machine. Companies that want to achieve both qualitative and economic benefits are faced with selecting tools that can be used to fulfill those goals. Choosing the proper framework can be challenging, but the importance of the selection of the programming language itself cannot be overlooked. 

This article will present the advantages of Python for testing automation - one of the most recognizable languages used for automation. Let's start with taking a look at testing itself.

Why is test automation so important?

Testing is a critical software development process that measures quality and reduces the risk of failures. This process includes planning tests (test management, defect tracking), designing and executing tests, and reporting the results to various teams or customers to inform them of the current quality of the product. With technological evolution, the speed of software development has rapidly increased, so testing needs to keep up. 

Automation is more than just a reduction in costs and the number of people engaged in a particular project. It also reduces test execution time, provides better test reproducibility, and the ability to test functions or product features that manual testing would (almost) not be possible (for example, performance testing, scale testing, etc.). The increase in competition requires automation to reduce costs while improving quality. With short iteration cycles there is no room for lengthy manual regression campaigns. Modern approaches to software development require fast feedback from QA, ideally as part of the CI/CD process for each code change. The required speed and scale of testing can only be achieved with automation.

Many people perform tedious, time-consuming, repetitive and error-prone tasks manually, despite the existing tools that allow you to improve such work. The entire testing process can be automated, but the most common automation option is the creation of test execution tools. 

There are many programming languages ​​on the market, but Python stands out as the most widely used language in the automation field. Thanks to the wide range of libraries, quick deployment, and the extensive community, Python has gained recognition across IT society.

Services Network professional services

Why can Python be helpful in the test automation world?

When it comes to automation, typically we are looking for a light solution which is at the same time easy to apply. Basically anything that would allow us to get the expected results fast. Python as a scripting language lets you write handy code without the need to compile, which feels like an answer to the above needs.

The main advantage of Python is its simplicity. Python works on any operating system and is very easy to learn and deploy. That is why the Python community is vast, creating and maintaining so many libraries for almost everything - from automation or testing to machine learning or web development.

Python has all the tools you might need. While other languages are good in specific fields, PHP might be good for web applications, Go for web services, C++ for embedded devices, Python has many libraries available and covers a wide variety of applications, from web applications to mathematical computations and bitwise operations. If you use the Go programming language for more serious server-side services then Python is more convenient for scripting, automation, and testing tasks. Golang is a strongly typed language, in Python you can skip the overhead and boilerplate of static typing which allows you to move faster with simple tasks. Python scripts are often called glue code due to their capacity for connecting different components or systems through external interfaces.

Naturally, nothing comes without a cost and the same is true of Python. First of all, multithreading is limited in Python. Nonetheless there are ongoing activities to improve it. Among the other disadvantages there is a shortage of strong typing. Naturally, on one hand one can return any type they want, but on the other this may lead to errors, often hard to troubleshoot. To work around that, libraries like ‘typings’ and mypy can be used for static analysis for such issues. 

Do you want to know how Python does compared to other programming languages? On our blog, you will find a few programming language ‘battles’ – check them out to find out the winners: 

Python in test automation – case studies 

Configuration management with Python

There might be cases where automation will require applying predefined configurations to physical or virtual machines. Python offers plenty of libraries designed for such tasks.

If you have one office router, you can configure it manually via a web interface. But what if you have many network devices, let's say 3, 10 or 100? You would definitely want to configure them in an automated fashion. There’s no difference whether the devices are physical bare metal routers, virtual machines or containers. Sometimes hardware gets broken, what if you need to replace it with a new device? Configuration covers the initial bring up of a new device, setting parameters to a defined state and testing if everything works as expected.

Paramiko 

All of the network devices expose a SSH interface, whether it be a vendor like Cisco, Juniper, Arista, a Linux server, or even a Mikrotik home router. Python has you covered. There are tools like Paramiko      link-icon which make it possible to execute remote commands and get their output.

Netmiko

Netmiko      link-icon uses Paramiko under the hood but simplifies working with network vendor-specific devices like routers and switches.

Napalm

And then there is Napalm      link-icon which offers a common interface to different systems, like Arista EOS, Cisco IOS, Cisco IOS-XR, Cisco NX-OS, and Juniper JunOS. Napalm parses the responses and gives back native Python objects - not only text but also diff and merge configurations, etc.

ncclient

Some network devices expose the NetConf interface that allows manipulating their configurations. NetConf uses SSH on port 830 and allows sending XML messages back and forth instead of providing a CLI interface. Again, Python has tools for this. There is a library ncclient      link-icon which allows for reading and updating the configurations of network devices.

NetConf

If you happen to work with a NetConf      link-icon-enabled device, netconf-console is a nice tool to have, it is a command line tool which allows you to execute NetConf RPC calls quickly without writing any Python code, which is good for quick prototyping and validating assumptions.

Working with web pages

HTTP protocol is omnipresent everywhere. It's necessary for automation to be able to make requests and get responses from an API or services. So, the device configuration tends to move towards RestConf: it's the same as NetConf but configuration updates happen via the HTTPS instead of SSH. Python has a native library to deal with HTTP requests      link-icon.

Another more popular and user-friendly library is "requests      link-icon". But that is adding an external dependency to your project and we experienced problems with multi-process workloads.

We could not skip Selenium      link-icon in the list of libraries, which is very popular when it comes to web services automation. Just a few lines of code lets you automate the login, account creation or product purchase processes.

Virtualization in automated testing 

Besides configuration of virtual machines, there might be a need to establish a virtual environment hosting applications we use. 

It's very convenient to use virtual machines for testing. Linux has virtualization capabilities for example through qemu or KVM. There is the Libvirt      link-icon hypervisor which allows managing virtual networks and machines, and it has a Python library so you do not have to do everything from the command line and can automate the creation and destruction of virtual machines natively in Python.

You can also manage containers natively in Python. Python and Go are two officially supported languages for Docker      link-icon.

More about automated tests with Python libraries 

Python lets you write tests with libraries such as pytest, unittest, doctest or the previously mentioned Selenium. The first one is a popular testing framework letting you create simple and readable tests that can also be applied to advanced cases. Pytest tests are based on the ‘assert’ statement. The framework uses plenty of decorators, allowing e.g. for passing test parameters to function rather than creating multiple tests differing only in parameters.

Summary

Python is the right choice if you are looking for a simple tool that will allow you to create, configure or test a target environment. Thanks to its many libraries, simple syntax, and clear documentation, you can quickly write scripts that will automate even more complex cases. 

Due to a wide range of options, this article could highlight only a few chosen libraries that help us in everyday work in CodiLime. 

On our blog you can find more Python-related articles. Check them out, and Python will hold no secrets from you! 

>> Here, you can learn more about our testing services.

Gilert Piotr

Piotr Gilert

Project Manager

Piotr Gilert is a project manager and author on CodiLime's blog. Check out the author's articles on the blog.Read about author >

Read also

Get your project estimate

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

For businesses that need support in their software or network engineering projects, please fill in the form and we’ll get back to you within one business day.

We guarantee 100% privacy.

Trusted by leaders:

Cisco Systems
Palo Alto Services
Equinix
Jupiter Networks
Nutanix