Tag Archives: Testing

Python Testing with doctest

Python includes a couple of modules for testing in its standard library: doctest and unittest. We will be looking at doctest in this article. The doctest module will search for pieces of text in your code that resemble interactive Python sessions. It will then execute those sessions to verify that they work exactly as written. This means that if you wrote an example in a docstring that showed the output with a trailing space or tab, then the actual output of the function has to have that trailing whitespace too. Most of the time, the docstring is where you will want to put your tests. The following aspects of doctest will be covered:

  • How to run doctest from the terminal
  • How to use doctest inside a module
  • How to run a doctest from a separate file

Let’s get started! Continue reading Python Testing with doctest

pyflakes – the passive checker of Python programs

There are several code analysis tools for Python. The most well known is pylint. Then there’s pychecker and now we’re moving on to pyflakes. The pyflakes project is a part of something known as the Divmod Project. Pyflakes doesn’t actually execute the code it checks, unlike pychecker. Of course, pylint also doesn’t execute the code. Regardless, we’ll take a quick look at it and see how pyflakes works and if it’s better than the competition. Continue reading pyflakes – the passive checker of Python programs

PyLint: Analyzing Python Code

Python code analysis can be a heavy subject, but it can be very helpful in making your programs better. There are several Python code analyzers that you can use to check your code and see if they conform to standards. pylint is probably the most popular. It’s very configurable, customizable and pluggable too. It also checks your code to see if it conforms to PEP8, the official style guide of Python Core and it looks for programming errors too. We’re going to spend a few minutes looking at some of the things you can do with this handy tool. Continue reading PyLint: Analyzing Python Code

Website Automation with Python, Firefox and Selenium

An appropriate alternate title would be: How to control a web page or test your website with Python. Recently, I was given the following assignment:

1) Login to a website
2) click on a toolbar and load a specific search form
3) enter some data into one of the fields on the form and search
4) if found, click another button
5) repeat

Of course, I was supposed to do all this with Python, my favorite programming language. My first thought was to use the Mechanize package, but while I could login to the website using that, I was unable to get it to load the form. Why? Well, unfortunately the aforementioned toolbar was made using javascript and it seemed to be generating the form too. For whatever reason, the Python port doesn’t support loading javascript elements, although I did find out that the project it’s based on has a javascript plugin, so there is hope that it might eventually. Thus, I went looking for another solution and recalled that Selenium might fit the bill. In the end, it worked quite well. Since I won’t be able to show you what I did exactly because it was for an internal project, we’ll be automating Gmail instead. Let’s get cracking! Continue reading Website Automation with Python, Firefox and Selenium

Python 102: An Intro to TDD and unittest

Python code testing is something new to me. It’s not required where I work, so I haven’t spent much time looking into it, besides reading a book on the subject and reading a few blogs. However, I decided it was high time I check this out and see what all the excitement is about. In this article, you will learn about Test Driven Development (TDD) with Python using Python’s builtin unittest module. This is actually based on my one experience of TDD and pair programming (thanks Matt and Aaron!). In this article, we will be learning how to score bowling with Python! Continue reading Python 102: An Intro to TDD and unittest

Book Review: Python Testing

Before PyCon, I was approached by a representative from Packt Publishing to review one of their books. They wanted me to read Daniel Arbuckle’s Python Testing: Beginner’s Guide. I’m not really into testing frameworks or test driven development and thought this would be a good excuse to learn the methodology and see if it was something that was actually valuable or just a lot of hype. The book is only 256 pages long, so I told the Packt contact that it would probably take me a week or so to review. After a week, she seemed kind of anxious that I wasn’t done. Thus, this is a partial review. I don’t like being pressured to review something fast. I want my reviews to be thorough and of the best quality I can make them. You will get a thorough review of the chapters that I did manage to finish. If you want a full review, I’m pretty sure someone else on Python Planet said they were doing one. Continue reading Book Review: Python Testing