Testing with Python: Just Makes Sense
Figuring out what language to code in as a tester can be particularly challenging. If you ask the programmers on the team, they’ll say to use whatever they code in. Ask the internet, and, again, you are most likely to hear what your friends are most familiar and comfortable with. Worse, you might hear a variety of options.
Test programming, however, is different than production programming. Test code will probably only run on one machine at a time, so time to write becomes more important than performance. Test code is typically small, so the ability to make something powerful quickly will probably be more important than being able to create large applications. As someone focused primarily on testing, not coding, you want a language that is easy to learn.
I’m going to make the choice easier for you by suggesting Python and then explaining why it might be a good choice. You can also join SmartBear and TreeHouse for a webinar on Why Python Is the Language To Learn For Testing.
Easier To Learn
Making challenging topics fun and putting off frustrating aspects till later helps keep me interested.
Take these examples of a program that does nothing other than outputting the string ‘Hello world!”
Java Example
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello world!”);
}
}
Perl Example
use strict;
use warnings;
print “Hello world!n”;
The example in Java is 5 lines long, we could probably shorten it but that would make is harder to read. There is also a lot of typing involved which means more potential for making errors that result in the program either not compiling or doing bad things once the things actually run. Speaking of compiling, you have to compile the code into an executable before you can run the program. Compiling really doesn’t take very long, but it is just one more step that must be performed before running code when I want to know how I’ve done right this minute.
Our Perl example is a little shorter. The two ‘use’ lines are not really required, so our program could be just one single line of code, but they are nice safety nets that will give a little more information for certain types of failures. There is a little something extra at the end of this program, a backslash and a ‘n’. This just creates a new line after the Hello world! output. Perl relies heavily on symbols like this to get things done.
Unlike Java there is no compilation, but you do have to change permissions on the file to make it executable.
Contrast all of that with this example in Python.
print “Hello world!”
That is all there is to it, just one line. Sure, we could wrap that line up in a function and put the function in a class, but we do not have to. Python helps us to get away with as little code as is necessary.
Scripted
Python is a scripted language. The import difference between scripted language and compiled languages is that you don’t have to worry about running a compiler ahead of time to convert your code from lines of code to something that can be executed and used. Python technically is compiled at run-time, that is, the compiling happens when you run the program. If your program is less than five thousand lines of code, you probably won’t even notice the compile step..
The most common reason to compile as a separate step is performance – pre-compiled code runs much faster than interpreted code. If that’s what you want, you can pre-compile python to bytecode, saving the file as a .pyc file and running that. Compiled code will have a much faster start time, because that conversion to a computer understandable language is already done. One important thing to remember is that pre-compiling doesn’t make your program faster; it just allows you to skip that wait at the start.
The alternative to compiling is what is called just-in-time compilation. This is usually done with the installed language you get from the Python website or a special version of the language like pypy, but there are others you can choose from if you have a special need. With just-in-time compilation, you write your program and then immediately run it, probably by typing python fileName.py on the command line. When you hit the enter button, compilation is run and then the program is executed. It is a little slower to start, but that has never mattered enough for me to care. The actual code will perform the same way whether you compile before running the program, or use just-in-time.
For my use in software testing, I always go with just-in-time compiling to avoid getting bogged down in extra layers of tools and environment dependencies.
Good Support System
I am not sure if this is because of Python’s origins in Monty Python and cute references like spam and eggs instead of foo and bar, or the clever logo and branding, but the language is developed quite a following and a big community around it. Tennessee has two cities that rank in the 100 biggest in the country, but the state isn’t known as a tech hub like California, Massachusetts, and to some degree, Texas. Despite that still developing technical environment, Tennessee has 4 different Python groups that meet regularly to talk about and present on topics related to the language.
These communities work as a cross between the welcome wagon for people new to the language and programming in general, and as a platform for people with more experience and understanding to share what they are currently interested in and what they have learned over the years. I enjoy going to the occasional tech meetup as a way to learn, and also to stay immersed in technology and developer culture.
For something longer and more in depth than a local meetup, there are a few regional and official conferences to choose from.
Another aspect of the Python community that is currently valuable to me is the online community. Sites like learnpythonthehardway and Python subreddits give beginners like me access to some of the best learning materials available in a nice format.
When I come across a problem I’m having a hard time getting past, the larger community is where I go.
Free And Open Source
Microsoft programming languages, technologies, and environments have a direct cash cost. Those tools are completely owned by Microsoft – Microsoft even owns the C# language. Build new language features for C# is mostly something employees of Microsoft can do if they work on the right team.. This restricts how the language is used, and when there are problems that usually means working around the problem instead of fixing it and moving on with life. Setting up a development environment with non-free tools can also be a challenge. Although there are free versions, the full (and a little pricey) version of VisualStudio is needed for real work.
Python on the other hand is a completely free and open source language. Just as you can download the language and work on it freely an most any environment, you can also contribute directly to the Python language development by documenting problems, suggesting new features, and even developing new functionality yourself.
Not only is price not a barrier to this technology, the culture and communities around Python are free software fans – to the most popular editors, webservers, and databases, and operating systems for python, that have the best support, are also typically free software packages.
Lots Of Tooling
Not only is Python simple and easy to learn – there is also a great deal of support and tools available to extend it. Arguably the most widely use testing tool for user interfaces, WebDriver, has bindings for Python. Once you get familair with Python, adding WebDriver as a skill should not be too much of a challenge; it is basically learning a code library. That same code library has been implemented in many languages – the same functions, same rules, same parameters, to call the same low-level code to drive the browser. The result is that a Ruby/Webdriver person a Python/Webdriver person and a C#/Webdriver person can all participate in the same conversation, sharing code that is conceptually the same but is differs in implementation. That means access to two communities – the Python open source community but also anyone using Selenium/Webdriver. That is two layers of safety nets built in to one tool to help you be successful.
There are at least a dozen tools designed for unit testing in Python. The task of building unit tests is typically performed by the person that wrote the code, but understanding the language and having the testers skill set is powerful.
There are many other Python-based tools for API testing, Behavior Driven Development (BDD), and tools to test a browser quickly, or ‘headless’ When in doubt, I will usually select what seems to be the most popular tool. Once you have selected how you want to test, popularity is a decent indicator for the support network, and also for a tool that is still getting new features when needed and is also well maintained it will be.
Those are a few reasons I chose Python as the language to learn and get useful in, and why I think you should too. Maybe you chose Python; tell us how it goes – or suggest something else? Either way, I’d love to hear your thoughts.