unittest.mock small gotcha - a humbling tale of failure

Developing a small web application I recently had reason to upgrade from Python 3.4 to Python 3.6. The reason for the upgrade was regarding ordering of keyword arguments and not related to the bug in my test code that I then found. I should have been more careful writing my test code in the first place, so I am writing this down as some penance for not testing my tests robustly enough. ...

February 25, 2017

In what ways are dynamically typed languages more productive?

Introduction I do not aim to answer the question in the title more raise the question. The question kind of implies that dynamically typed languages are more productive in at least some ways. It does not imply that statically typed languages are less productive in general, or the opposite. Before going any further, I’m talking about the distinction between static and dynamic typing which is not the same as strong vs weak typing. Static means the type checking is done at compilation before the program is run, whilst dynamic means types are checked whilst the program is running. Not the same as weak vs strong typing and also not the same as explicit vs implicit typing. ...

May 4, 2016

Selenium and Javascript Events

Selenium is a great way to test web applications and it has Python bindings. I explained in a previous post how to set this up with coverage analysis. However, writing tests is non-trivial, in particular it is easy enough to write tests that suffer from race conditions. Suppose you write a test that includes a check for the existence of a particular DOM element. Here is a convenient method to make doing so a one-liner. It assumes that you are within a class that has the web driver as a member and that you’re using ‘pytest’ but you can easily adapt this for your own needs. ...

March 16, 2016

Test First and Mutation Testing

Test First and Mutation Testing I’m going to argue that mutation testing has a strong use in a test first development environment and I’ll conclude by proposing a mechanism to link mutation testing to the source code control mechanism to further aid test first development. Test First Just to be clear, when I say ’test first’ I mean development in which before writing a feature, or fixing a bug, you first write a test which should only pass once you have completed that feature. For the purposes of this post you needn’t be doing that for every line of code you write. The idea here applies whether you are writing the odd feature by first writing a test for it, or whether you have a strict policy of writing no code until there is a test for it. ...

February 19, 2016

Update Flask and Coverage

Update: Flask+Coverage Analysis In a previous post I demonstrated how to get coverage analysis working for a Flask web application in a relatively simple manner. In the section “At then end of your tests” I stated that you needed your tests to clean-up by telling the server to shutdown. The end of your test code would look something like this: finally: driver.get(get_url('shutdown')) ... This could have made things a little fiddly since your test code would have to make sure to access the shutdown route exactly once, regardless of how many tests were run. ...

January 26, 2016

Flask and Coverage Analysis

Flask + Coverage Analysis This post demonstrates a simple web application written in Flask with coverage analysis for the tests. The main idea should be pretty translatable into most Python web application frameworks. Update I’ve updated this scheme and described the update here. tl;dr If you’re having difficulty getting coverage analysis to work with Flask then have a look at my example repository. The main take away is that you simply start the server in a process of its own using coverage to start it. However, in order for this to work you have to make sure you can shut the server process down from the test process. To do this we simply add a new “shutdown” route which is only available under testing. Your test code, whether written in Python or, say Javascript, can then make a request to this “shutdown” route once it completes its tests. This allows the server process to shutdown naturally and therefore allow ‘coverage’ to complete. ...

January 25, 2016

Selenium vs CasperJS

Suppose you have a web service using a Python web framework such as Flask. So you wish to do a full user test by automating the browser. Should you use the Python bindings to Selenium or CasperJS? In this post I wish to detail some of the advantages and drawbacks of both. Python + Selenium Setup This is fairly straightforward. You are simply writing some Python code that happens to call a library which binds to Selenium. In order for this to work you will need to install phantomJS but using npm this is pretty trivial. ...

January 8, 2016