I know a majority of testers would say Java is their go-to language for creating automated tests.
But my favorite is Python.
Here are 22 libraries you'll find in this guide for why Python is so awesome:
- Robot Framework
- Beautiful Soup
Well, Al Sweigart, author of Automate the Boring Stuff, explained it best in episode 54 of my weekly TestGuild automation podcast.
Python has always been his language of choice because:
- It has a gentle learning curve.
- It works on Windows, Linux, and MAC.
- It's still a serious programming language that professional developers use.
- It says “Hello world” in one line, unlike Java, where you sort of just have to remember the public, static, void, string, ban, bracket, etc.
- Easy-to-remember syntax
- It doesn't force you to learn object-oriented programming.
Python code is very concise. Kind of like Perl, except readable—unlike Perl.
So what are some of the best Python libraries you can use to help you with your testing and automation efforts?
The following are a few of my past interviewees’ favorite Python automation libraries, as well as a few that I've personally found helpful.
For Functional Python Test Automation
Of course, you have the Selenium-Python binding for browser UI automation.
This Selenium package is used to automate web browser interaction from Python.
Selenium is the industry standard when it comes to browser-based automation.
If your team is made up of mostly test engineers that have development skills or SDET‘s, this is a perfect option.
A cool wrapper type library for Selenium in Python is Splinter.
Splinter is an open-source tool for testing web applications using Python. It lets you automate browser actions, such as visiting URLs and interacting with their items.
It makes writing python Selenium tests easier because it has a high-level API that makes it easy to develop automation scripts for your browser applications.
If you want to use Python for test automation, you can't go wrong with the Robot Framework. This mature product, created for testers, uses a keyword-driven approach to make tests readable and easy to create.
It also includes a bunch of test libraries and other tools. The Robot Framework is Python-based, but you can also use Jython (Java) or IronPython (.NET).
While the Selenium WebDriver library might be the most-used external test library, Robot Framework can also test other things like:
- Much more
If your team is mostly made up of testers, Robot Framework is a great option for your automation framework.
How about behavior-driven development libraries?
Python has a bunch of Cucumber–like BDD frameworks to choose from. The most popular one is probably Behave. It's almost identical to cucumber, but it's in Python.
Need to do REST API testing or any sort of HTTP activity?
Requests is an Apache2 Licensed HTTP library written in Python. This library, I believe, is one of the most popular python libraries by the sheer number of downloads. It is indispensable for your test automation efforts.
Tavern describes itself as a command-line tool and Python library and Pytest plugin for automated testing of RESTful APIs, with a simple, concise, and flexible YAML-based syntax.
A popular testing topic I've been hearing more about is property-based testing. If you've been wanting to try it yourself and you're into python, take a look at Hypothesis.
Hypothesis for property-based testing where you can test integers in a certain range and hypothesis we'll just crank through all sorts of variations for you automatically. So it helps to find edge cases in your code you probably wouldn’t have thought to look for.
For non-browser-based functional automation, give pywinauto a try.
pywinauto is a set of Python modules you can use to automate Microsoft Window GUIs.
You can use it to send keyboard and mouse actions to Windows dialogs and controls. It also has some support for more complex operations, like procuring text data.
Automagica is an open-source, Smart Robotic Process Automation (SRPA) platform based on the Python language. What's cool about Automagica is you can quickly automate a bunch of different cross-platform processes like:
- Browser automation
- PDF manipulation
- File and folder manipulation
- Email operations
- Word and Excel automation
- Tons more
How about automating other activities that are not functional testing related?
When I say “automation,” I mean not only functional test automation, but any activity that can help you speed up your software development process.
One of the Python libraries I use for pulling data out of HTML and XML files is Beautiful Soup.
Although the main framework I use is Java-based, I have a directory in my framework that includes all kinds of Python helper scripts—most of which leverage Beautiful Soup.
For example, I have some scripts that help me parse my Serenity automation framework reports quickly.
It's also great for web scrapping. If you have a simple static webpage where you need to find some small bit of information buried somewhere in the HTML, Beautiful Soup is your library of choice.
You can use the requests library to help you download the webpage itself, and Beautiful Soup will help you parse it.
NumPy is the fundamental package for scientific computing with Python.
One reason is that it is so concise and focused on the tests. In pytest, you write test functions– not test classes– test functions. And if you want to do set and cleanup, you write other functions that are tagged as fixtures, and you can declare them in a dependency injection way.
So the fixtures will be automatically called based on their scope, and whatever they return gets injected into your test functions. This makes setup and cleanup much more scalable and much more reusable.
Brian also mentioned that although it might take you a while to wrap your head around pytest fixtures, it's well worth the effort because they're extremely powerful. If you are new to Python testing, pytest is a must-learn library.
*Make sure to check out Brian's book Python Testing with pytest: Simple, Rapid, Effective, and Scalable to learn all about it.
If you've been following the latest news in the automation space for the past few years, you know how much of a hot topic artificial intelligence is.
TensorFlow is a Python library for fast numerical computing created and released by Google. It’s a foundation library that can be used to create Deep Learning models directly or by using wrapper libraries that simplify the process built on top of TensorFlow.
TensorFlow was actually one of the libraries that Tariq King used to create Ultimate Software’s Python-based, open-source AI generation and exploratory testing tool Agent and AgentX.
How many times have you found yourself needing to test the content of a PDF?
I was actually surprised to find out how many Python modules there are for interacting with a number of different technologies, like Excel files or parsing PDF files.
If you need to test or validate PDF text content, you may find PDFMiner helpful. This library is basically a text extraction tool for PDF documents.
Have you heard of the unit-level, automated testing framework called Jest?
Created by Facebook, it has features that make it easier to perform test-driven development; for instance, its interactive watch mode, which you can use to improve workflows.
It's also intelligent enough to know which tests to run that are relevant to the files you've changed since your last commit to your code repository.
This feature is super helpful if you have hundreds (or even thousands) of tests that take a long time to run, since it will run only those that are relevant to your changes.
Kent C. Dodds turned me on to a Python-based implementation of Jest called pyjest. Although it's still considered to be in the experimental stage, it has all of the same watch mode features that Kent favors.
Looking to add some performance tests to your Python scripts?
Locust is a simple-to-use, Python-based load testing tool.
Locust can also help you figure out how many concurrent users a system can handle.
Some of the cool features you get with Locust are:
- The ability to create your test scenarios using straight Python
- The ability to quickly scale the number of users you need to emulate
- A friendly, Web-based UI Extensible
- Good for testing APIs
PyBuilder is a software build automation tool written in Python that mainly targets Python programs.
Its website says that it's based on the concept of dependency-based programming, but it also comes with a powerful plugin mechanism that allows the construction of build life cycles similar to those found in build tools like Apache Maven.
Ravi Salunkhe, a Test Architect connection of mine on LinkedIn, highly recommends it.
The problem pandas solves based on information on their website is “Python has long been great for data munging and preparation, but less so for data analysis and modeling. pandas helps fill this gap, enabling you to carry out your entire data analysis workflow in Python without switching to a more domain-specific language like R.”
Coverage.py is a tool for measuring code coverage of Python programs.
Kyle Tice recommended this library in the comments: “Coverage.py is one of the most popular code coverage tools for Python. It uses code analysis tools and tracing hooks provided in the Python standard library to measure coverage. It runs on major versions of CPython, PyPy, Jython, and IronPython. You can use Coverage.py with both unit test and Pytest”
Their website mentioned that it monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not. Coverage measurement is a good way to gauge the effectiveness of your tests. This tool will show which parts of your code are being exercised by tests, and more importantly, which are not.
PyUnit is an easy way to create unit testing programs and UnitTests with Python.
Ok not sure how I missed adding my favorite IDEs to this list but Daryl Flowers on LinkedIn reminded me it would be good to add.
If you are used to using Java for automation testing, you are probably familiar with JetBrain's IntelliJ. For Python, they also make an awesome Python-specific IDE for professional developers called PyCharm.
Have you ever had to automate an application that required unique data for every test run?
It can be a pain. That's where Faker comes in.
A faker allows you to dynamically create real-looking data like names, phone numbers, addresses, SSN — all kinds of stuff. This is really handy when you need to create automated tests.
Most language bindings have one but for Python check out Joke2k's Faker on GitHub. This Python Faker package can be used to generate fake data for you.
What Python Automation Testing Tools Did I Miss
These are just a few of the Python based-libraries I’ve used or have had recommended to me.
What did I miss?
Let me know, and I’ll add it/them to the list.
Also, to learn more about Python automation testing, and if you're looking for a quick way to learn how to get started with Python, you should check out Andrew Knight's session at Automation Guild on How to Start Testing with Python. This would be a great jump start for you before checking out the following python testing libraries.