background preloader

Test

Facebook Twitter

Test-Driven Development with Python. Making good use of random in your python unit tests. Writing efficient UnitTest to validate that your code performs as expected is a difficult endeavor.

Making good use of random in your python unit tests

Lot has been written about the benefits of Test Driven development and on how to best approach testing, and lot can be learned reading the available litterature. One thing however that we don’t see often mentionned is that architecting efficient UnitTest is pretty hard and that no tools or testing framework are of much value without a fair understanding of the code base that needs to be tested. Dynamically generating Python test cases. Testing is crucial.

Dynamically generating Python test cases

While many different kinds and levels of testing exist, there’s good library support only for unit tests (the Python unittest package and its moral equivalents in other languages). However, unit testing does not cover all kinds of testing we may want to do – for example, all kinds of whole program tests and integration tests. This is where we usually end up with a custom "test runner" script. Having written my share of such custom test runners, I’ve recently gravitated towards a very convenient approach which I want to share here. In short, I’m actually using Python’s unittest, combined with the dynamic nature of the language, to run all kinds of tests. Let’s assume my tests are some sort of data files which have to be fed to a program.

So you write a "test runner". Why not employ Python’s existing "test runner" capabilities to do the same? Here’s a very short code snippet that can serve as a template to achieve this: What happens here: Related posts: Mocking in Python for Better Unit Tests. How to Run Unit Tests Without Testing Your Patience More often than not, the software we write directly interacts with what we would label as “dirty” services.

Mocking in Python for Better Unit Tests

In layman’s terms: services that are crucial to our application, but whose interactions have intended but undesired side-effects—that is, undesired in the context of an autonomous test run. Coverage.py. Created 24 May 2009, last updated 12 December 2013 Coverage.py is a tool for measuring code coverage of Python programs.

coverage.py

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 typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not. The latest version is coverage.py 3.7.1, released 13 December 2013. Getting started is easy: Install coverage.py from the coverage page on the Python Package Index, or by using “pip install coverage”. There are a few different ways to use coverage.py.

Some test runners provide coverage integration to make it easy to use coverage while running tests. You can fine-tune coverage’s view of your code by directing it to ignore parts that you know aren’t interesting. Bug reports are gladly accepted at the Bitbucket issue tracker. Next: Installation » Diff-cover. Getting Started Testing. Created 18 March 2014, last updated 13 April 2014 This is a presentation I gave at PyCon 2014.

Getting Started Testing

You can read the slides and text on this page, open the actual presentation in your browser (use right and left arrows to advance the slides), or watch the video: There is also a zipfile of all the code examples. Writing correct code is complicated, it's hard. How do you know when you've gotten it right, and how do you know your code has stayed right even after you've changed it? The best we know to do this is with automated testing. In this talk, I will show you how to write automated tests to test your Python code. I'll include some pointers off to more advanced or exotic techniques, but you will have a good set of tools if you follow the methods shown here. The concepts covered here work the same in Python 2 or 3, though the code is Python 2. Good Integration Practises. Work with virtual environments We recommend to use virtualenv environments and use pip (or easy_install) for installing your application and any dependencies as well as the pytest package itself.

Good Integration Practises

This way you will get an isolated and reproducible environment. Given you have installed virtualenv and execute it from the command line, here is an example session for unix or windows: virtualenv . # create a virtualenv directory in the current directory source bin/activate # on unix scripts/activate # on Windows.

Pytest documentation.