﻿<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Coding Diet (testing)</title><link>https://allanderek.github.io/</link><description></description><atom:link href="https://allanderek.github.io/categories/testing.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Sat, 25 Feb 2017 19:21:25 GMT</lastBuildDate><generator>https://getnikola.com/</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>unittest.mock small gotcha - a humbling tale of failure</title><link>https://allanderek.github.io/posts/unittestmock-small-gotcha/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;p&gt;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.&lt;/p&gt;
&lt;h3&gt;A Simple example program&lt;/h3&gt;
&lt;p&gt;So here I have a version of the problem reduced down to the minimum required to demonstrate the issue:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;unittest.mock&lt;/span&gt; &lt;span class="kn"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;mock&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'__main__.MyClass'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Of course in reality the line &lt;code&gt;MyClass().my_method()&lt;/code&gt; was some test code that indirectly caused the target method to be called.&lt;/p&gt;
&lt;h4&gt;Output in Python 3.4&lt;/h4&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;$ python3.4 mock_example.py
$
&lt;/pre&gt;


&lt;p&gt;No output, leading me to believe my assertions passed, so I was happy that my code and my tests were working. As it turned out, my code was fine but my test was faulty. Here's the output in two later versions of Python of the exact same program given above.&lt;/p&gt;
&lt;h4&gt;Output in Python 3.5&lt;/h4&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;$ python3.5 mock_example.py
Traceback &lt;span class="o"&gt;(&lt;/span&gt;most recent call last&lt;span class="o"&gt;)&lt;/span&gt;:
  File &lt;span class="s2"&gt;"mock_example.py"&lt;/span&gt;, line 12, in &amp;lt;module&amp;gt;
    MockMyClass.my_method.assert_called_once&lt;span class="o"&gt;()&lt;/span&gt;
  File &lt;span class="s2"&gt;"/usr/lib/python3.5/unittest/mock.py"&lt;/span&gt;, line 583, in __getattr__
    raise AttributeError&lt;span class="o"&gt;(&lt;/span&gt;name&lt;span class="o"&gt;)&lt;/span&gt;
AttributeError: assert_called_once
&lt;/pre&gt;


&lt;p&gt;Assertion error, test failing.&lt;/p&gt;
&lt;h4&gt;Output in Python 3.6&lt;/h4&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;$ python3.6 mock_example.py
Traceback &lt;span class="o"&gt;(&lt;/span&gt;most recent call last&lt;span class="o"&gt;)&lt;/span&gt;:
  File &lt;span class="s2"&gt;"mock_example.py"&lt;/span&gt;, line 12, in &amp;lt;module&amp;gt;
    MockMyClass.my_method.assert_called_once&lt;span class="o"&gt;()&lt;/span&gt;
  File &lt;span class="s2"&gt;"/usr/lib/python3.6/unittest/mock.py"&lt;/span&gt;, line 795, in assert_called_once
    raise AssertionError&lt;span class="o"&gt;(&lt;/span&gt;msg&lt;span class="o"&gt;)&lt;/span&gt;
AssertionError: Expected &lt;span class="s1"&gt;'my_method'&lt;/span&gt; to have been called once. Called &lt;span class="m"&gt;0&lt;/span&gt; times.
&lt;/pre&gt;


&lt;p&gt;Test also failing with a different error message. Anyone who is (pretty) familiar with the &lt;code&gt;unittest.mock&lt;/code&gt; standard library module will know &lt;code&gt;assert_called_once&lt;/code&gt; was introduced in version 3.6, which is my version 3.5 is failing with an attribute error.&lt;/p&gt;
&lt;h3&gt;My test was wrong&lt;/h3&gt;
&lt;p&gt;The problem was, my original test was not testing anything at all. The 3.4 version of the &lt;code&gt;unittest.mock&lt;/code&gt; standard library module did not have a &lt;code&gt;assert_called_once&lt;/code&gt;. The mock, just allows you to call any method on it, to see this you can try changing the line:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;        &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;to&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;        &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blahblah&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;With &lt;code&gt;python3.4&lt;/code&gt;, &lt;code&gt;python3.5&lt;/code&gt;, and &lt;code&gt;python3.6&lt;/code&gt; this yields no error. So in the original program you can avoid the calling &lt;code&gt;MyClass.my_method&lt;/code&gt; at all:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'__main__.MyClass'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Missing call to `MyClass().my_method()`&lt;/span&gt;
        &lt;span class="n"&gt;MockMyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# In 3.4 this still passes.&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;This does not change the (original) results, &lt;code&gt;python3.4&lt;/code&gt; still raises &lt;strong&gt;no error&lt;/strong&gt;, whereas &lt;code&gt;python3.5&lt;/code&gt; and &lt;code&gt;python3.6&lt;/code&gt; are raising the original errors.&lt;/p&gt;
&lt;p&gt;So although my code turned out to be correct (at least in as much as the desired method was called), had it been faulty (or changed to be faulty) my test would not have complained.&lt;/p&gt;
&lt;h3&gt;The Actual Problem&lt;/h3&gt;
&lt;p&gt;My mock was wrong. I should instead have been patching the actual method within the class, like so:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'__main__.MyClass.my_method'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;mock_my_method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;mock_my_method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;assert_called_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Now if we try this in all version 3.4, 3.5, and 3.6 of python we get:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;$ python3.4 mock_example.py 
$ python3.5 mock_example.py 
Traceback &lt;span class="o"&gt;(&lt;/span&gt;most recent call last&lt;span class="o"&gt;)&lt;/span&gt;:
  File &lt;span class="s2"&gt;"mock_example.py"&lt;/span&gt;, line 12, in &amp;lt;module&amp;gt;
    mock_my_method.assert_called_once&lt;span class="o"&gt;()&lt;/span&gt;
  File &lt;span class="s2"&gt;"/usr/lib/python3.5/unittest/mock.py"&lt;/span&gt;, line 583, in __getattr__
    raise AttributeError&lt;span class="o"&gt;(&lt;/span&gt;name&lt;span class="o"&gt;)&lt;/span&gt;
AttributeError: assert_called_once
$ python3.6 mock_example.py 
$ 
&lt;/pre&gt;


&lt;p&gt;So Python 3.4 and 3.6 pass as we expect. But Python3.5 gives an error stating that there is no &lt;code&gt;assert_called_once&lt;/code&gt; method on the mock object, which is true since that method was not added until version 3.6. This is arguably what Python3.4 should have done.&lt;/p&gt;
&lt;p&gt;It remains to check that the updated test fails in Python3.6, so we comment out the call to &lt;code&gt;MyClass().my_method&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;$ python3.6 mock_example.py 
Traceback &lt;span class="o"&gt;(&lt;/span&gt;most recent call last&lt;span class="o"&gt;)&lt;/span&gt;:
  File &lt;span class="s2"&gt;"mock_example.py"&lt;/span&gt;, line 12, in &amp;lt;module&amp;gt;
    mock_my_method.assert_called_once&lt;span class="o"&gt;()&lt;/span&gt;
  File &lt;span class="s2"&gt;"/usr/lib/python3.6/unittest/mock.py"&lt;/span&gt;, line 795, in assert_called_once
    raise AssertionError&lt;span class="o"&gt;(&lt;/span&gt;msg&lt;span class="o"&gt;)&lt;/span&gt;
AssertionError: Expected &lt;span class="s1"&gt;'my_method'&lt;/span&gt; to have been called once. Called &lt;span class="m"&gt;0&lt;/span&gt; times.
&lt;/pre&gt;


&lt;p&gt;This is the test I &lt;strong&gt;should&lt;/strong&gt; have performed with my original test. Had I done this I would have seen that the test passed in Python3.4 regardless of whether the method in question was actually called or not.&lt;/p&gt;
&lt;p&gt;So now my test works in &lt;code&gt;python3.6&lt;/code&gt;, fails in &lt;code&gt;python3.5&lt;/code&gt; because I'm using the method &lt;code&gt;assert_called_once&lt;/code&gt; which was introduced in &lt;code&gt;python3.6&lt;/code&gt;. Unfortunately it incorrectly passes in &lt;code&gt;python3.4&lt;/code&gt;. So if I want my code to work properly for python versions earlier than 3.6, then I can essentially implement &lt;code&gt;assert_called_once()&lt;/code&gt; with &lt;code&gt;assert len(mock_my_method.mock_calls) == 1&lt;/code&gt;. If we do this then my test passes in all three version of python and fails in all three if we comment out the call &lt;code&gt;MyClass().my_method()&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Conclusions&lt;/h3&gt;
&lt;p&gt;I made an error in writing my original test, but my real sin was that I was a bit lazy in that I did not make sure that my tests would fail, when the code was incorrect. In this instance there was no problem with the code only the test, but that was luck. So for me, this served as a reminder to check that your tests can fail. It may be that mutation testing would have caught this error.&lt;/p&gt;&lt;/div&gt;</description><category>3.4</category><category>3.5</category><category>3.6</category><category>mock</category><category>python</category><category>standard library</category><category>testing</category><guid>https://allanderek.github.io/posts/unittestmock-small-gotcha/</guid><pubDate>Sat, 25 Feb 2017 12:36:24 GMT</pubDate></item><item><title>In what ways are dynamically typed languages more productive?</title><link>https://allanderek.github.io/posts/dynamically-typed-languages-why/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;Background&lt;/h2&gt;
&lt;p&gt;My PhD involved the development of a novel static type
system. When I begun my PhD I was firmly of the opinion that
statically typed languages were better than dynamically typed
languages. My basic contention was that all the guarantees you get
from static types you get largely for free, so throwing those away is a
stupefying act of self harm. I believe that prior to the turn of the
century (if not a bit later), this the majority view in programming language
research. It is still a position commonly held but I'm unsure whether it may
still be a majority view or not.&lt;/p&gt;
&lt;p&gt;New data, should force us to seek out new theories which explain
all of the available data. In the past 20 years, one thing is obvious.
Dynamically typed languages have seen significant growth and success.
Some researchers choose to ignore this. Some explain the success as a
fluke, that such languages have become popular &lt;em&gt;despite&lt;/em&gt; being
dynamically typed. This is a recurrence of an argument made by
functional programmers/researchers when C++ and then Java became wildly popular.&lt;/p&gt;
&lt;p&gt;The prevailing view amongst researchers was that functional languages were inherently
better than Java, but Java got a lot of financial support which meant
that a lot of support was added to, in particular, the standard
library. This meant that programmers were particularly productive
using Java, but that that increase in productivity was mis-attributed
by the programmers to the language, when it should have been placed
firmly in the excellent standard library support. Much of this support
is work that open source programmers are not quite so keen on, because
frankly, it's a bit boring and unrewarding. I personally find this
argument at least lightly compelling.&lt;/p&gt;
&lt;p&gt;However, in the first decade of this century, as I said, dynamically typed
languages have had at least significant success. Python, PHP,
and Ruby are the most obvious examples. None of these were backed
financially by any large corporation, at least not prior to their success.
I again suspect that much of the productivity gained with the use of such
languages can be placed in the library support. But that does not explain
where the library support has come from. If dynamically typed languages were so
obviously counter-productive, then why did anyone waste their time
writing library support code in-and-for them?&lt;/p&gt;
&lt;h2&gt;Some Wild Hypotheses Appear&lt;/h2&gt;
&lt;p&gt;I am now going to state some hypotheses to explain this. This does not mean
I endorse any of these.&lt;/p&gt;
&lt;h3&gt;Short term vs Long Term&lt;/h3&gt;
&lt;p&gt;One possible answer. Dynamically typed languages increase &lt;em&gt;short term&lt;/em&gt;
productivity at the cost of &lt;em&gt;long term&lt;/em&gt; productivity. I don't personally
believe this but I do find it plausible. However, I do not know of any
evidence for or against this position. I'm not even sure there is much
of a logical argument for it.&lt;/p&gt;
&lt;p&gt;The kinds of bugs that functional programming languages help prevent
are the kind of bug that is hard to demonstrate. It is easy enough to show
a bug in an imperative program that would not occur in a functional program
because you do not have mutable state. However, such demonstration bugs
tend to be a bit contrived, and it is hard to show that such bugs come up in
real code frequently. On top of that, to show that functional languages are
more productive one would have to show that by restricting mutable state you
do not lose more productivity than you gain by avoiding such bugs. If you did
manage to show this, you would have a reasonable argument that functional
languages are bad for short-term productivity, due to the restrictions on
mutable state changes, but compensate in greater long-term productivity.&lt;/p&gt;
&lt;p&gt;So, a similar kind of argument could be made for statically typed languages.
If you could show that statically typed languages prevent a certain class of
bugs and that the long-term productivity gained from that is more than enough
to compensate for any short-term loss in productivity brought on by restrictions
due to the type-system.&lt;/p&gt;
&lt;p&gt;So I will leave my verdict on this hypothesis as, I believe it to be false
but it is plausible. Just to note, there is no great evidence that &lt;em&gt;either&lt;/em&gt;
statically typed languages have greater long-term productivity,
&lt;strong&gt;or&lt;/strong&gt; that dynamically typed languages have greater short-term productivity.&lt;/p&gt;
&lt;h3&gt;Testing Tools&lt;/h3&gt;
&lt;p&gt;A trend that seems to have tracked (ie. correlates with, either via
causation in either direction or by coincidence) the trend in use of
(and success of) dynamically typed languages is the trend towards more
rigorous testing, or rather the rise in popularity of more rigorous testing.
In particular test-driven development style methodologies have gained
significant support.&lt;/p&gt;
&lt;p&gt;I believe that having a comprehensive test suite, somewhat dilutes the
benefits gained from a static type system. Here is a good challenge,
try to find a bug that is caught by the type checker, that would not be
caught by a test suite with 100% code coverage.&lt;/p&gt;
&lt;p&gt;One possibility is an exhaustive pattern match, however, if your test suite
is not catching this, it's not a great test suite. Still, exhaustive pattern
match tests is something you get more or less for free with a static type checker,
whilst a test suite has to work at it.&lt;/p&gt;
&lt;p&gt;It is certainly possible to come up with a bug that is caught by static type checking
and not by a test suite that has full coverage. Here is an example:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;This is a bug, because &lt;code&gt;b&lt;/code&gt; might be true, whilst &lt;code&gt;a&lt;/code&gt; is false. Which
would mean that &lt;code&gt;x&lt;/code&gt; is set to a string, but later treated as an integer,
because &lt;code&gt;b&lt;/code&gt; is true. A good test suite, will of course catch
this bug. But it is still possible to achieve 100% code coverage (at
the statement) level, and &lt;em&gt;not&lt;/em&gt; catch this bug. Still, you have to try
quite hard to arrange this.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Mutation_testing"&gt;Mutation testing&lt;/a&gt;,
which tests your tests, rather than your implementation code, should
catch this simple example (because it will mutate the condition
&lt;code&gt;(a and b)&lt;/code&gt; to be &lt;code&gt;(a or b)&lt;/code&gt; which won't make any difference if your tests
never caught the bug initially. This will mean that the mutant will
pass all tests, and you should at that point realise your tests are
not comprehensive enough.&lt;/p&gt;
&lt;h3&gt;Dynamically Typed Language Benefits&lt;/h3&gt;
&lt;p&gt;So you should have a comprehensive test suite for all code whether you are
using a statically or dynamically typed language. We may then accept the
theory that a comprehensive test suite somewhat dilutes any benefits of using
a statically typed language. However, that theory does not give any reasaon why
a static type system is detrimental to productivity.&lt;/p&gt;
&lt;p&gt;This was always my main contention, that whatever might
be the benefits of static typing, you are getting them for free, so
why not? I honestly do not know what, if any, benefit there is from
having a dynamic type system. I can think of some plausible
candidates, but have no evidence that any of these are true:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Freeing the &lt;em&gt;language&lt;/em&gt; from a type system, allows the language
designers to include some productivity boosting features that are not
available in a statically typed language. I find this suggestion a little weak,
no one has ever been able to point me to such a language feature.&lt;/li&gt;
&lt;li&gt;Knowing there is no safety net of the type system encourages
developers to test (and document) more. I find this theory more compelling.&lt;/li&gt;
&lt;li&gt;One can try simple modifications without the need to make many
fiddly changes, such as, for example, adding an exception to a method
signature, often in many places.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I suspect, that if there are significant benefits to using a dynamically
typed language, then it is a combination of 2 and 3, or some other reason.&lt;/p&gt;
&lt;p&gt;For the third, a rebuttal often mentions automatic refactoring tools.
Which may well in theory be something of a good rebuttal, but in practice developers simply
don't use such tools often. I'm not sure why not, I myself have never
taken to them. So perhaps there is a productivity gain from using a
dynamically typed language which &lt;em&gt;would&lt;/em&gt; be all but negated if only
the developers would use automatic refactoring tools, but they don't.
So it &lt;em&gt;shouldn't&lt;/em&gt; be a productivity win for dynamically typed
languages, but in practice it is (this is all still conjecture).&lt;/p&gt;
&lt;p&gt;The second one has some evidence from psychology. There is a lot of
evidence to suggest that safety mechanisms often do not increase
overall safety but simply allow more risky behaviour. A very famous
example is a seat-belt study done in Germany. Wherein mandating the
wearing of seat-belts caused drivers to drive faster. This means that
you are more likely to have a crash, but less likely to be seriously
injured in one. This has similarly been done with anti-lock braking
systems, where the brakes being significantly better did not reduce
accidents but rather increased risky driving so that the number of
accidents remained largely constant.&lt;/p&gt;
&lt;p&gt;I mentioned documentation because it's an important one. There are
plenty of libraries for statically typed languages for which the only
documentation for many of the functions/methods is the type signature.
This is often seen as "good enough", or at least good enough to mean
that API documentation is not at the top of the todo stack.
A dynamically-typed language does not typically have signatures for
methods/functions. As a result, they tend to have fewer undocumented
libraries, simply because the developer of the library knows that
their methods will otherwise not be used. If that is the case, what is
the point in the library? So they tend to write &lt;em&gt;something&lt;/em&gt;, and once
you are writing &lt;em&gt;something&lt;/em&gt; it isn't so hard to write something
useful.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;This is getting too long. So I'll stop there for now. The main points are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Dynamically typed languages have had a lot of success this
century, which remains largely unexplained&lt;/li&gt;
&lt;li&gt;I think that with comprehensive testing, the gains from a static
type system are diluted significantly&lt;/li&gt;
&lt;li&gt;It might be that dynamically typed language encourage more/better
testing (or have some other non-obvious advantage)&lt;/li&gt;
&lt;li&gt;Otherwise, there is scant evidence for anything a dynamically
typed language actually does &lt;em&gt;better&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;I think an obvious win for statically typed languages would be to
make the type checking phase optional.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I am not sure why we do not really see any examples of languages
that have optional static type checking. In other words languages in which
the user decided when type checking should be done.&lt;/p&gt;
&lt;p&gt;As a final point. For dynamically typed languages, it is common to
deploy some form of static analyser. Often these static analysers fall short of
the kind of guarantees afforded by a static type system, but they have
two significant advantages. Firstly, you can run the program without
running the static analyser. In particular, you can run your test
suite, which may well give you more information about the correctness
of your code than a type checker would. Especially in the case that
the type checker fails. It tells you about one specific problem in
your code, but not how the rest of your code does or does not pass
your tests. Secondly you can deploy different static analysers for
different problems. For example a statically typed language has to
decide whether or not to include exceptions in types. A dynamically
typed language can easily offer both. I suppose a statically typed
language &lt;em&gt;could&lt;/em&gt; offer both as well.&lt;/p&gt;&lt;/div&gt;</description><category>dynamically typed languages</category><category>python</category><category>statically typed languages</category><category>testing</category><category>typing</category><guid>https://allanderek.github.io/posts/dynamically-typed-languages-why/</guid><pubDate>Wed, 04 May 2016 19:41:57 GMT</pubDate></item><item><title>Selenium and Javascript Events</title><link>https://allanderek.github.io/posts/selenium-and-javascript-events/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;p&gt;Selenium is a great way to test web applications and it has Python bindings.
I explained in a &lt;a href="https://allanderek.github.io/posts/flask-%2B-coverage-analysis"&gt;previous post&lt;/a&gt; how to
set this up with coverage analysis.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;assertCssSelectorExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;css_selector&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sd"&gt;""" Asserts that there is an element that matches the given&lt;/span&gt;
&lt;span class="sd"&gt;    css selector."""&lt;/span&gt;
    &lt;span class="c1"&gt;# We do not actually need to do anything special here, if the&lt;/span&gt;
    &lt;span class="c1"&gt;# element does not exist we fill fail with a NoSuchElementException&lt;/span&gt;
    &lt;span class="c1"&gt;# however we wrap this up in a pytest.fail because the error message&lt;/span&gt;
    &lt;span class="c1"&gt;# is then a bit nicer to read.&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_element_by_css_selector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;css_selector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;NoSuchElementException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;pytest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Element {0} not found!"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;css_selector&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;The problem is that this test might fail if it is performed too early. If you
are merely testing after loading a page, this should work, however you may be
testing after some click by a user which invokes a Javascript method.&lt;/p&gt;
&lt;p&gt;Suppose you have an application which loads a page, and then loads all comments
made on that page (perhaps it is a blog engine). Now suppose you wish to allow
re-loading the list of comments without re-loading the entire page. You might
have an Ajax call.&lt;/p&gt;
&lt;p&gt;As before I tend to write my Javascript in Coffeescript, so suppose I have a
Coffeescript function which is called when the user clicks on a
&lt;code&gt;#refresh-comment-feed-button&lt;/code&gt; button:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;refresh_comments = &lt;/span&gt;&lt;span class="nf"&gt;(page_id) -&amp;gt;&lt;/span&gt;
  &lt;span class="nv"&gt;posting = &lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="s"&gt;'/grabcomments'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;page_id: &lt;/span&gt;&lt;span class="nx"&gt;page_id&lt;/span&gt;
  &lt;span class="nx"&gt;posting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt; &lt;span class="nx"&gt;receive_comments&lt;/span&gt;
  &lt;span class="nx"&gt;posting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fail&lt;/span&gt; &lt;span class="nf"&gt;(data) -&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;So this makes an Ajax call which will call the function &lt;code&gt;receive_comments&lt;/code&gt;
when the Ajax call returns (successfully). We write the &lt;code&gt;receive_comments&lt;/code&gt; as:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;receive_comments = &lt;/span&gt;&lt;span class="nf"&gt;(data) -&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="nx"&gt;comments&lt;/span&gt; &lt;span class="o"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;replace&lt;/span&gt; &lt;span class="nx"&gt;them&lt;/span&gt; &lt;span class="nx"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;those&lt;/span&gt; &lt;span class="nx"&gt;returned&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Typically &lt;code&gt;data&lt;/code&gt; will be some JSON data, perhaps the comments associated with
the &lt;code&gt;page_id&lt;/code&gt; we gave as an argument to our Ajax call.&lt;/p&gt;
&lt;p&gt;To test this you would navigate to the page in question and check
that there are no comments, then open a new browser window and make two
comments (or alternatively directly adding the comments to the database),
followed by switching back to the first browser window and then
performing the following steps:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="n"&gt;refresh_comment_feed_css&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'#refresh-comment-feed-button'&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;click_element_with_css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refresh_comment_feed_css&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;check_comments&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;first_comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second_comment&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Where &lt;code&gt;self.check_comments&lt;/code&gt; is a method that checks the particular comments
exist on the current page. This could be done by using
&lt;code&gt;find_elements_by_css_selector&lt;/code&gt; and then looking at the &lt;code&gt;text&lt;/code&gt; attributes of
each returned element.&lt;/p&gt;
&lt;p&gt;The problem is, that the final line is likely to be run before the results of
the Ajax call invoked from the click on the &lt;code&gt;#refresh-comment-feed-button&lt;/code&gt; are
returned to the page.&lt;/p&gt;
&lt;p&gt;A quick trick to get around this is to simply change the Javascript to somehow
record when the Ajax results are returned and then use Selenium to wait until
the relevant Javascript evaluates to true.&lt;/p&gt;
&lt;p&gt;So we change our &lt;code&gt;receive_comments&lt;/code&gt; method to be:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;comments_successfully_updated = &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nv"&gt;receive_comments = &lt;/span&gt;&lt;span class="nf"&gt;(data) -&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="nx"&gt;comments&lt;/span&gt; &lt;span class="o"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;replace&lt;/span&gt; &lt;span class="nx"&gt;them&lt;/span&gt; &lt;span class="nx"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;those&lt;/span&gt; &lt;span class="nx"&gt;returned&lt;/span&gt;
  &lt;span class="nx"&gt;comments_successfully_updated&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Note that we only increment the counter after we have updated the page.&lt;/p&gt;
&lt;p&gt;Now, we can update our Selenium test to be:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="n"&gt;refresh_comment_feed_css&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'#refresh-comment-feed-button'&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;click_element_with_css&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refresh_comment_feed_css&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_for_comment_refresh_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;check_comments&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;first_comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second_comment&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;The &lt;code&gt;1&lt;/code&gt; argument assumes that this will be the first time the comments are
updated during your test. Of course as you run down your test you can increase
this argument as required. The code for the &lt;code&gt;wait_for_comment_refresh_count&lt;/code&gt;
is given by:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;selenium.webdriver.support.ui&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;WebDriverWait&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;selenium.webdriver.support&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;expected_conditions&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;selenium.webdriver.common.by&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;By&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyTest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;# assume that 'self.driver' is set appropriately.&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wait_for_comment_refresh_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_refresh_count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'return comments_successfully_updated;'&lt;/span&gt;
            &lt;span class="n"&gt;feed_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;execute_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;feed_count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
        &lt;span class="n"&gt;WebDriverWait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;until&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;check_refresh_count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;The key point is executing the Javascript to check the
&lt;code&gt;comments_successfully_updated&lt;/code&gt; variable with &lt;code&gt;driver.execute_script&lt;/code&gt;.
We then use a &lt;code&gt;WebDriverWait&lt;/code&gt; to wait for a maximum of 5 seconds until the
our condition is satisfied.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Updating a Javascript counter to record when Javascript events have occurred
can allow your Selenium tests to synchronise, that is, wait for the correct time
to check the results of a Javascript event.&lt;/p&gt;
&lt;p&gt;This can solve problems of getting a &lt;code&gt;StaleElementReferenceException&lt;/code&gt; or a
&lt;code&gt;NoSuchElementException&lt;/code&gt; because your Selenium test is running a check on an
element too early before your page has been updated.&lt;/p&gt;&lt;/div&gt;</description><category>python</category><category>selenium</category><category>testing</category><guid>https://allanderek.github.io/posts/selenium-and-javascript-events/</guid><pubDate>Wed, 16 Mar 2016 18:02:32 GMT</pubDate></item><item><title>Test First and Mutation Testing</title><link>https://allanderek.github.io/posts/test-first-mutation-testing/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;h2&gt;Test First and Mutation Testing&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h3&gt;Test First&lt;/h3&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h3&gt;Mutation Testing&lt;/h3&gt;
&lt;p&gt;Mutation testing is the process of automatically changing some parts of your
source code generally to check that your test suite is not indifferent to the
change. For example, your source code may contain a conditional statement
such as the following:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Now if we suppose that the current condition is correct, then changing it to
a similar but different condition, for example &lt;code&gt;x &amp;gt;= 0&lt;/code&gt; or &lt;code&gt;x &amp;gt; 1&lt;/code&gt; then
presumably this would turn correct code into incorrect code. If your tests are
comprehensive then at least one of them should fail due to the now incorrect
code.&lt;/p&gt;
&lt;h3&gt;Fail First&lt;/h3&gt;
&lt;p&gt;It's easy enough to unintentionally write a test that always passes, or perhaps
passes too easily. One of the reasons for writing the test first is to make sure
that it fails when the feature has not yet been implemented (or fixed). However,
often such a test can fail for trivial reasons. For example you may write a unit
test that fails simple because the method it tests is not yet defined. Similarly
a web test may fail because the route is not yet defined. Unless you continue to
run the test during development of your feature you won't necessarily know that
your test is particularly effective at catching when your feature is broken.&lt;/p&gt;
&lt;h3&gt;Fail After&lt;/h3&gt;
&lt;p&gt;Whether you write the test before your new feature or after the feature is
ready, mutation testing can assist with the problem of non-stringent tests.
Mutation testing can assist in reassuring you that your new test is effective at
catching errors, whether those errors are introduced when the feature is
developed or through later changes. If you apply lots of mutations to your code
and your new test never fails then there is a strong likelihood that you have an
ineffective test that passes too easily.&lt;/p&gt;
&lt;h3&gt;Source Code control&lt;/h3&gt;
&lt;p&gt;A feature I would like to add to a mutation test package is to integrate with
a source code control mechanism such as Git. The mutation tester must choose
lines of the program to mutate. However, your new test is presumably aimed at
testing the new code that you write. Hence we could use the source code control
mechanism to mutate lines of code that are newer than the test or some specified
commit. That way we would focus our mutation testing to testing the efficacy of
the new test(s) with respect to the new or changed lines of code.&lt;/p&gt;
&lt;p&gt;This does not preclude doing general mutation testing for features that, for
example, depend upon a lot of existing code. Perhaps your new feature is simply
a display of existing calculations.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;In summary:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Mutation testing helps find tests that are ineffective.&lt;/li&gt;
&lt;li&gt;This plays particularly well with a test first development process in which
    the test often fails the first time for trivial reasons, thus giving you
    false assurance that your test can fail.&lt;/li&gt;
&lt;li&gt;Integrating source code control to target the mutations towards new code
    could improve this significantly, or at least make it a bit more convenient.&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</description><category>mutation testing</category><category>python</category><category>scc</category><category>testing</category><guid>https://allanderek.github.io/posts/test-first-mutation-testing/</guid><pubDate>Fri, 19 Feb 2016 10:14:43 GMT</pubDate></item><item><title>Update: Flask+Coverage</title><link>https://allanderek.github.io/posts/update-flask%2Bcoverage/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;h2&gt;Update: Flask+Coverage Analysis&lt;/h2&gt;
&lt;p&gt;In a &lt;a href="https://allanderek.github.io/posts/flask-%2B-coverage-analysis"&gt;previous post&lt;/a&gt;
I demonstrated how to get &lt;code&gt;coverage&lt;/code&gt; analysis working for a Flask web application
in a relatively simple manner. In the section &lt;em&gt;"At then end of your tests"&lt;/em&gt; 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:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shutdown'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;This could have made things a little fiddly since your test code would have to
make sure to access the &lt;code&gt;shutdown&lt;/code&gt; route exactly once, regardless of how many
tests were run.&lt;/p&gt;
&lt;p&gt;However, I realised that we could remove the burden from the test code by
simply doing this in &lt;code&gt;manage.py&lt;/code&gt; file.&lt;/p&gt;
&lt;h3&gt;Updated &lt;code&gt;manage.py&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Previously, we had the following code within our &lt;code&gt;manage.py&lt;/code&gt; script within the
&lt;code&gt;run_with_test_server&lt;/code&gt; method:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;test_process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;test_command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;server_return_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;We now update this to be:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;test_process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;test_command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;test_process&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TEST_SERVER_PORT'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;shutdown_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:{}/shutdown'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urlopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shutdown_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;span class="n"&gt;server_return_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;Doing so means you can just write your tests without any need to worry about
shutting down the server.
The &lt;a href="https://github.com/allanderek/flask-coverage-example"&gt;example repository&lt;/a&gt;
has been appropriately updated.&lt;/p&gt;&lt;/div&gt;</description><category>coverage</category><category>python</category><category>selenium</category><category>testing</category><guid>https://allanderek.github.io/posts/update-flask%2Bcoverage/</guid><pubDate>Tue, 26 Jan 2016 14:59:44 GMT</pubDate></item><item><title>Flask + Coverage Analysis</title><link>https://allanderek.github.io/posts/flask-%2B-coverage-analysis/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;h2&gt;Flask + Coverage Analysis&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h4&gt;Update&lt;/h4&gt;
&lt;p&gt;I've updated this scheme and described the update &lt;a href="https://allanderek.github.io/posts/update-flask%2Bcoverage"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;tl;dr&lt;/h3&gt;
&lt;p&gt;If you're having difficulty getting coverage analysis to work with Flask then
have a look at my &lt;a href="https://github.com/allanderek/flask-coverage-example"&gt;example repository&lt;/a&gt;.
The main take away is that you simply start the server in a process of its own
using &lt;code&gt;coverage&lt;/code&gt; 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.&lt;/p&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;It's a good idea to test the web applications you author. Most web application
frameworks provide relatively solid means to do this. However, if you're doing
browser automated functional tests against a live server I've found that getting
&lt;a href="https://pypi.python.org/pypi/coverage"&gt;coverage&lt;/a&gt; to work to be non-trivial. A quick search will reveal similar
difficulties such as &lt;a href="http://stackoverflow.com/questions/23745370/setting-up-coverage-py-with-flask"&gt;this&lt;/a&gt;
stack overflow question, which ultimately points to the &lt;a href="http://coverage.readthedocs.org/en/latest/subprocess.html"&gt;coverage documentation
on sub-processes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Part of the reason for this might be that the Flask-Testing extension provides
live server testing class that starts your server in testing mode as part of
the start-up of the test. It then also shuts the server process down, but in
so doing does not allow coverage to complete.&lt;/p&gt;
&lt;p&gt;A simpler method is to start the server process yourself under coverage. You
then only need a means to shutdown the server programatically. I do this by
adding a &lt;code&gt;shutdown&lt;/code&gt; route.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://allanderek.github.io/posts/flask-%2B-coverage-analysis/"&gt;Read more…&lt;/a&gt; (3 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>coverage</category><category>python</category><category>selenium</category><category>testing</category><guid>https://allanderek.github.io/posts/flask-%2B-coverage-analysis/</guid><pubDate>Mon, 25 Jan 2016 15:20:50 GMT</pubDate></item><item><title>Selenium vs CasperJS</title><link>https://allanderek.github.io/posts/selenium-and-casper/</link><dc:creator>Allan Clark</dc:creator><description>&lt;div&gt;&lt;p&gt;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.&lt;/p&gt;
&lt;h3&gt;Python + Selenium Setup&lt;/h3&gt;
&lt;p&gt;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 &lt;code&gt;npm&lt;/code&gt; this is pretty trivial.&lt;/p&gt;
&lt;h3&gt;Javascript + CasperJS Setup&lt;/h3&gt;
&lt;p&gt;I say Javascript, but at least when I'm using casperJS it tends to be from
Coffeescript, but whichever is your fancy works well enough. Similarly to the
above you will need to install casperJS through the &lt;code&gt;npm&lt;/code&gt; but again this is
pretty trivial.&lt;/p&gt;
&lt;h3&gt;Speed&lt;/h3&gt;
&lt;p&gt;For my sample project the casperJS tests are faster, by quite a bit. This
certainly warrants some more investigation as to exactly why this is the case,
but for now my sample project runs the casperJS tests in around 3 seconds whilst
the Selenium ones take around 12 seconds to do the same amount of work. I'm not
sure whether this is a constant factor or whether it will get worse with more
tests.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://allanderek.github.io/posts/selenium-and-casper/"&gt;Read more…&lt;/a&gt; (3 min remaining to read)&lt;/p&gt;&lt;/div&gt;</description><category>casperjs</category><category>python</category><category>selenium</category><category>testing</category><guid>https://allanderek.github.io/posts/selenium-and-casper/</guid><pubDate>Fri, 08 Jan 2016 11:11:40 GMT</pubDate></item></channel></rss>