Quantcast
Viewing all articles
Browse latest Browse all 9596

Python Testing 101: Introduction

python is an amazing programming language. Loved by beginners and experts alike, it is consistently ranked as one of the mostin-demand languages today. At PyData Carolinas 2016 , Josh Howes, a senior data science manager at MaxPoint , described Python like this (in rough paraphrase):

Python is a magical tool that easily lets you solve the world’s toughest problems.

I first touched Python back in high school more than a decade ago, but I really started using it and loving it in recent years for test automation . This 101 series will teach how to do testing in Python. This introductory post will give basic orientation, and each subsequent post will focus on adifferent Python test framework in depth.

Why Use Python for Testing?

As mentioned in another post, The Best Programming Language for Test Automation , Python is concise, elegant, and readable the precise attributes needed to effectively turn test cases into test scripts . It has richly-supportedtest packages to deftly handle both white-box and black-box testing.Engineers who have never used Python tend to learn it quickly.

The following examples illustrate ways to use Python for test automation:

A developer embeddingquick checks into function docstrings . A developer writingunit tests for a module or package . A tester writing integration tests for REST APIs. A tester writing end-to-end web tests using Selenium . A data scientist verifying functions in a Jupyter notebook. The Three Amigos writing Given-When-Then scenarios forBDD testing.

Remember, Python can be used for any black-box testing, even if the software product under test isn’t written in Python!

Python Version

Choosing the right Python installation itself is no small decision. For an in-depth analysis, please refer to Which Version of Python Should I Use? Tl;dr:

For white-box testing, use the matching Python version. For black-box testing, use CPython version 3 if not otherwise constrained. Picking a Framework

There are so many Python test frameworks that choosing one may seem daunting just look at the Python wiki , The Hitchhiker’s Guide to Python , and pythontesting.net . Despite choice overload , there are a fewimportant things to consider:

Consider the type of testing . Basic unit tests could be handled by unittest or even doctest, but higher-level testing would do better with other frameworks like pytest. BDD testing would require behave, lettuce, or radish. Consider the supported Python version .Python 2 and 3 are two different languages, with Python 2’s end-of-life slated for 2020 . Different frameworks have different levels of version support, which could become especially problematic for white-box testing. Furthermore, some (such as unittest) may have different features between Python versions. Consider support and development . Typically, it is best to choose mature, actively-developed frameworks for future sustainability. For example, the once-popular nose is now deprecated.

Future posts in this series will document many frameworks in detail to empower you, as the reader, to pick the best one for your needs.

Virtual Environments

A virtual environment (VE) is like a local Python installation with a specific package set. Tools like venv (Python 3.3+), virtualenv (Python 2 and 3), and Conda (Python 2 and 3; for data scientists) make it easy to create virtual environments from the command line. Creating at least one separate VE for each Python project is typically a good practice. VEs are extremely useful for test automation because:

VEsallow engineers to maintain multiple Python environments simultaneously. Engineers can developand test packages for both versions of Python. Engineers can separate projects that rely on different package versions. VEsallow users to installPython packages locally without changing global installations. Users may not have permissions to install packages globally. Global changes may disrupt other dependent Python software. VEs can import and exportpackage lists for easy reconstruction.

VEs become especially valuable in continuous integration and deployment because they can easily provide Python consistency. For example, a Jenkins build job can create a VE, install dependencies from PyPI in the VE, run Python tests, and safely tear down. Once the product under test is ready to be deployed, the same VE configuration can be used.

Recommended IDEs Any serious test automation work needs an equally serious IDE. My favorite is JetBrains PyCharm . I really like itsslick interface and intuitive nature, and it provides out-of-the-box support for a number of Python test framework

Viewing all articles
Browse latest Browse all 9596

Trending Articles