<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Posts tagged with “Testing” on Mark van Lent’s weblog</title>
  <updated>2012-10-12T09:45:00+00:00</updated>
  <link rel="self" type="application/atom+xml" href="https://markvanlent.dev/tags/testing/index.xml" hreflang="en"/>
  <id>tag:markvanlent.dev,2010-04-02:/tags/testing/index.xml</id>
  <link rel="alternate" type="text/html" href="https://markvanlent.dev/tags/testing/" hreflang="en"/>
  <author>
      <name>Mark van Lent</name>
      <uri>https://markvanlent.dev/about/</uri>
    </author>
  <rights>Copyright (c) Mark van Lent, Creative Commons Attribution 4.0 International License.</rights>
  <icon>https://markvanlent.dev/favicon.ico</icon>
  <entry>
    <title type="html"><![CDATA[Travis CI: easy and fun CI for your Plone packages (Nejc Zupan)]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2012/10/12/travis-ci-easy-and-fun-ci-for-your-plone-packages-nejc-zupan/" type="text/html" />
    <id>https://markvanlent.dev/2012/10/12/travis-ci-easy-and-fun-ci-for-your-plone-packages-nejc-zupan/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="plone" />
    <category term="ploneconf" />
    <category term="testing" />
    
    <updated>2021-08-20T19:50:51Z</updated>
    <published>2012-10-12T09:45:00Z</published>
    <content type="html"><![CDATA[<p>A talk about Travis CI: hosted continuous integration for the open
source community.</p>
<p>Ideally you run the tests of a project often (on every commit) in a clean
environment. This way you can make sure that not only the product works
properly, but also that the buildout of the product works on a clean system.
That last part is really helpful during sprints: if someone cannot get the
buildout running, it&rsquo;s most likely the problem of the machine of the developer,
not the product itself.</p>
<p>If you haven&rsquo;t seen the
<a href="https://github.com/collective/tutorial.todoapp">tutorial.todoapp</a> then
definitely check it out. It shows some great best practises, including
tests and CI.</p>
<p>Travis is free for any public GitHub repository. You can also have paid
subscriptions for private builds.</p>
<div class="note update">
  <div class="note_header">
    Update (2021-08-02)<span class="hidden">:</span>
  </div>
  <div class="note_body">
    Unfortunately Travis CI
<a href="https://blog.travis-ci.com/2020-11-02-travis-ci-new-billing">changed their terms</a>
so please do your own research to determine whether Travis CI is indeed free for
your use-case.
  </div>
</div>

<p>Travis already ran 757k tests for +10k open source projects. They are crowd
funded: +120k USD from +700 people.  They support a lot of programming languages
and preinstalled DBs (you only have to enable them).</p>
<p>Travis is very easy to setup. To get started you:</p>
<ul>
<li>Sign in to <a href="https://travis-ci.org/">travis-ci.org</a> with your GitHub account.</li>
<li>Enable a repository.</li>
<li>Add a <code>.travis.yml</code> file.</li>
<li>Push the <code>.travis.yml</code> file.</li>
</ul>
<p>Example of a very simple <code>.travis.yml</code> file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">language</span><span class="p">:</span><span class="w"> </span><span class="l">python</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">python</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="m">2.7</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">install</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">python bootstrap.py</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">bin/buildout</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">script</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">bin/test</span><span class="w">
</span></span></span></code></pre></div><p>(Note that because the tests are run from a clean Ubuntu install, you
have to have a buildout configured for you project.)</p>
<p>With <a href="https://www.jenkins.io/">Jenkins</a> it is harder to get
started. With Travis the minimal configuration is quite simple. But
you can get more complex if you want to. The drawback is that you only
get the console output. With Jenkins you can get more information
(e.g. coverage reports).</p>
<p>There are a number of services on the default Ubuntu image that is
used. You just have to enable them. For instance, the X Virtual Frame
Buffer.</p>
<p>You can whitelist or blacklist branches of your repository. This
prevents errors when you are just trying things out. If you want to
skip a specific build from being tested, you can add &ldquo;<code>[ci skip]</code>&rdquo; to
the commit message.</p>
<p>Build notifications can be sent. Mail and IRC are the most common
ones, but many more options are available.</p>
<p>There is
<a href="https://docs.travis-ci.com/user/browser-extensions/">Travis browser extensions</a>
that will show you the Travis status of a project when you go to the
page. If you want to, you can also create a status image
(<code>https://travis-ci.org/[YOUR_GITHUB_USERNAME]/[YOUR_PROJECT_NAME].png</code>),
e.g for on your PyPI page.</p>
<p>You can also have pull request testing. If someone issues a pull
request from a branch, Travis merges the code and you get a
notification whether the pull request is good to merge because the
tests succeed (or not). You can even see the status per commit (if
there is some back-and-forth on an issue for example).</p>
<p>Limitations of Travis: there&rsquo;s a 15 minute build runtime limit.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup> The
limit is for the entire build&mdash;from start to the end of the test. And
since the build starts from scratch every time, this may be an
issue. And again, the reporting is still limited.</p>
<p>Tips to speed up the build:</p>
<ul>
<li>Use a non-ancient zc.buildout version (1.6+)</li>
<li>Proper configuration (add &ldquo;<code>socket-timeout = 3</code>&rdquo; and
&ldquo;<code>allow-hosts = ...</code>&rdquo; to the <code>[buildout]</code> section)</li>
<li>Use Asko Soukka&rsquo;s trick to
<a href="https://datakurre.pandala.org/2012/09/speed-up-your-plone-add-on-tests-on.html/">speed up your Plone add-on tests on Travis CI</a>:
download the unified installer and extract that to your buildout
cache.</li>
</ul>
<p>Nejc would like to see Travis and Jenkins go hand in hand in Plone.</p>
<p>Advantages of Travis CI:</p>
<ul>
<li>New developers with broken builds.</li>
<li>Can also be used for non-Plone packages (simple Python libraries).</li>
<li>It&rsquo;s cool to do public CI for company owned public packages. This
way others can contribute and see if their build succeeds.</li>
</ul>
<p><a href="https://www.slideshare.net/zupo/travis-ci-fun-and-easy-ci-for-your-plone-packages">View the slides</a>
or <a href="https://www.youtube.com/watch?v=HsGLLGeXFOU">watch the video</a>.</p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>Update (2021-08-02): This information also looks outdated. Please refer to
the <a href="https://docs.travis-ci.com/user/customizing-the-build/#build-timeouts">Travis CI documentation</a>&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>]]></content>
  </entry>
  <entry>
    <title type="html"><![CDATA[Book review: Django 1.1 Testing and Debugging]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2010/06/20/book-review-django-11-testing-and-debugging/" type="text/html" />
    <id>https://markvanlent.dev/2010/06/20/book-review-django-11-testing-and-debugging/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="book" />
    <category term="development" />
    <category term="django" />
    <category term="testing" />
    
    <updated>2021-07-18T14:38:11Z</updated>
    <published>2010-06-20T15:05:00Z</published>
    <content type="html"><![CDATA[<p>In April Packt published
<a href="https://www.packtpub.com/product/django-1-1-testing-and-debugging/9781847197566">Django 1.1 Testing and Debugging</a>
by Karen M. Tracey. After reading it I figured I might as well write a
review.</p>
<p>The book is written for Django application developers and requires
basic knowledge of Python and Django. And although the code samples
are indeed explained, the context is not always expanded on. This
makes sense for this book and the author recommends to at least do the
<a href="https://docs.djangoproject.com/en/dev/intro/tutorial01/">Django tutorial</a>. I
think you&rsquo;ll get even more out of the book if you&rsquo;ve got a bit more
experience with building Django applications.</p>
<p>As the title already gives away, the book focusses on two subjects:
testing and debugging. These topics are discussed while developing a
survey application. Because this application is used consistently in
the book, the examples require less explanation which means that the
focus in more on the subject at hand (e.g. writing a unit test)
instead of developing this specific application.</p>
<h2 id="testing">Testing</h2>
<p>The first five chapters dive into the world of testing. Karen starts
with explaining how to run the tests and what success and failure
looks like. The second chapter has a focus on
<a href="https://docs.python.org/3/library/doctest.html">doctests</a>. Not only does
she show examples of tests, she also lists pros and cons of using
doctests. Meanwhile she also discusses whether a test is even useful
(example: you should not test Django itself). The examples in the book
consist of doctests that are placed in the docstring of functions. She
does not show how to run a doctest in a separate file
(e.g. <code>README.txt</code>) which can be more useful for explaining or
demonstrating the API of the application as a whole, or doing
integration tests.</p>
<p><a href="https://docs.python.org/3/library/unittest.html">Unit tests</a> are
discusses more extensively in chapters three to five. Karen starts by
reimplementing the doctests as unit tests and continues by showing
things like the <code>setUp</code> and <code>tearDown</code> methods and also
<code>django.test.Client</code> to test views. In the last chapter about testing
she introduces the reader to test runners, test coverage and using
<a href="https://pypi.org/project/twill/">twill</a> instead of
<code>django.test.Client</code>. The only thing I want to nag about is the style
of some of the examples. Occasionally she writes things like:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">testHome</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span></code></pre></div><p>Where I would write:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
</span></span><span class="line"><span class="cl"><span class="k">def</span> <span class="nf">testHome</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span></code></pre></div><p>In my opinion every programming book should pay attention to
programming style. Although it does not have to be addressed
explicitly, the style of the examples should be consistent and follow
the appropriate coding standards/guides (like
<a href="https://www.python.org/dev/peps/pep-0008/">PEP8</a> for example which
states that imports must be at the top of the file).</p>
<h2 id="debugging">Debugging</h2>
<p>Anyway, on with the book review&hellip; The remainder of the book is about
debugging and troubleshooting. Karen first describes the functionality
Django offers to debug your application. For instance, the debug error
pages are explained in detail. The style Karen chose for this part of
the book resembles watching a programmer at work: a mistake is made
and results in an error page, the error reported by Django is
investigated to discover the cause of the problem and the code is
fixed.</p>
<p>Next subject is how to find errors that are less obvious: Django does
not show an error page, but the application does not work as it
should. Initially some custom code is created to show the SQL queries
involved in a view. Although this code is replaced by the
<a href="https://pypi.org/project/django-debug-toolbar/">Django Debug Toolbar</a>
later on, it is also used to show how to package the custom code for
reuse. (Personally, I would have kept the custom query display section
a bit shorter and skip the packaging altogether. It&rsquo;s beyond the scope
of the book and I think there are other resources that describe
developing for reuse much better.) Karen does a great job describing
the features offered by the Django Debug Toolbar. She also explains
why using print statements is evil and using logging is good.</p>
<p>The next subject (chapter nine) is the Python debugger (<code>pdb</code>). Not only
is this chapter a thorough description of the debugger, there&rsquo;s also a
number of pages devoted to
<a href="https://en.wikipedia.org/wiki/Race_condition#Computing">race conditions</a>.
Although this is an important subject, I personally sometimes forget about it
when developing an application. So I think it&rsquo;s great that it the subject is
brought up in this book!</p>
<p>Chapter ten (&ldquo;When All Else Fails: Getting Outside Help&rdquo;) is a nice description
of what you should do when you encounter a problem in Django. Karen points the
reader to the <a href="https://docs.djangoproject.com/">Django documentation</a> (to
confirm you are using the API as it was intended) and
<a href="https://code.djangoproject.com/">Django&rsquo;s bug tracker</a> (where you might already
find a fix for your problem). She also gives clear instructions and tips on what
to do when the problem isn&rsquo;t already fixed or perhaps hasn&rsquo;t even been reported
yet.</p>
<p>The last chapter of the book is about moving your code to production
and making it accessible for the world. It gives an example for the
required WSGI script and the Apache configuration. Furthermore, it
also describes debugging the production environment and load testing
with <a href="https://www.joedog.org/siege-home/">Siege</a>.</p>
<p>Finally Karen discusses the use of Apache and mod_wsgi during development.
Although she claims that the Apache/mod_wsgi setup can be made nearly as
convenient as using the Django development server I think it really depends on
the situation. I agree that it may be a good way to tackle some problems and
setup issues early on. But if you are working with a number of developers on the
project and each developer is working on several different projects (sometimes
even switching projects throughout the day), I&rsquo;m not sure whether it&rsquo;s worth the
effort.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I&rsquo;ve been developing in Python a couple of years, mainly working on
Plone projects since 2007 and doing Django applications since 2009. I
was already more than familiar with the topics in the book (doctests,
unit test, logging, debugging, et cetera) and have also done a bit of
test driven development. As a result, the book did not open a whole
new world for me. However, I do think that the book was worth the
read. I picked up a few new tricks and gained an even better insight
in testing and debugging Django applications.</p>
<p>One last complaint though: the title of the book could have been a bit
better: by putting the Django version number in the title, the book
seems to target Django 1.1 specifically. Sure, Django 1.1 and 1.1.1
have been used in the book. And sure, some examples might not work in
earlier or later versions of Django. However, the principles remain
the same! In my opinion the version should have been left out of the
title.</p>
<p>Although I may not agree with everything, I think
<a href="https://www.packtpub.com/product/django-1-1-testing-and-debugging/9781847197566">Django 1.1 Testing and Debugging</a>
is a good book. So I&rsquo;d like to thanks Karen and Packt for writing and
publishing it.</p>]]></content>
  </entry>
  <entry>
    <title type="html"><![CDATA[Unit testing: useful?]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2009/11/05/unit-testing-useful/" type="text/html" />
    <id>https://markvanlent.dev/2009/11/05/unit-testing-useful/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="development" />
    <category term="opinion" />
    <category term="testing" />
    
    <updated>2021-07-16T07:25:56Z</updated>
    <published>2009-11-05T11:31:00Z</published>
    <content type="html"><![CDATA[<p>Today I read two articles about the usefulness of unit testing. Here
are my thoughts.</p>
<p>The first article I read is called <a href="https://web.archive.org/web/20091108121255/http://blogs.msdn.com/cashto/archive/2009/03/31/it-s-ok-not-to-write-unit-tests.aspx">It&rsquo;s OK Not to Write Unit Tests</a>
and is written by Chris &ldquo;cashto&rdquo; Ashton). Although Chris isn&rsquo;t saying that all
unit tests are worthless, he attributes less value to them than they deserve, at
least in my opinion. For the projects I&rsquo;ve been working on, unit tests can
actually be very useful.</p>
<p>For example, I&rsquo;ve frequently caught bugs after refactoring because a
unit test failed. Does this make me a bad programmer? Unit tests can
also be used to define the API of a certain piece of code. By writing
unit tests in the form of
<a href="https://en.wikipedia.org/wiki/Doctest">doctests</a> you are forced to
think about the way something should work and at the same time
document it.</p>
<p>The other article I&rsquo;ve read is <a href="https://web.archive.org/web/20091108145607/http://andreyf.tumblr.com:80/post/224053080/the-problem-with-unit-testing">The Problem with Unit Testing</a>.
Although I like <a href="https://en.wikipedia.org/wiki/Test-driven_development">test driven development</a>,
I wholeheartedly agree to the view that 100% unit test coverage is
probably overkill in a project. You&rsquo;ll constantly have to evaluate
whether writing a unit test for a piece of code is worth it. For code
that is more or less isolated (only used in a tiny part of the
project), very straight forward and easy to debug (worst case
scenario), I&rsquo;m likely to skip writing tests for instance.</p>
<p>Note that I&rsquo;m only talking about unit tests here. There are many more
ways to make sure the quality of your code is what it should be.</p>]]></content>
  </entry>
  <entry>
    <title type="html"><![CDATA[Test sending emails while developing]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2009/10/03/test-sending-emails-while-developing/" type="text/html" />
    <id>https://markvanlent.dev/2009/10/03/test-sending-emails-while-developing/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="development" />
    <category term="django" />
    <category term="plone" />
    <category term="python" />
    <category term="testing" />
    <category term="tools" />
    
    <updated>2021-08-20T19:50:51Z</updated>
    <published>2009-10-03T12:14:00Z</published>
    <content type="html"><![CDATA[<p>I frequently have to send emails from web applications. But before I
deploy to a production environment, I want to make sure the mechanism
works and the right mails are constructed. Here&rsquo;s two ways to do that.</p>
<h2 id="monkey-patching-the-zope-mailhost">Monkey patching the Zope MailHost</h2>
<p>When developing a Zope based application, the
<a href="https://pypi.org/project/Products.PrintingMailHost/">Products.PrintingMailHost</a>
package can really help you out. By including this package in your
setup, the Zope <code>MailHost</code> class is patched so no actual emails are
sent. Instead the content of email is printed to the standard output.</p>
<h2 id="smtp-server">SMTP server</h2>
<p>But when working on a Django application (or any other non Zope
project) there is no <code>MailHost</code> class that can be monkey
patched. Python&rsquo;s
<a href="https://docs.python.org/2.7/library/smtpd.html">smtpd module</a> to the
rescue. The first step is to configure the application to use
localhost as the SMTP server on a random port (say: 1025). Next, go to
the command line and type:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">python -m smtpd -n -c DebuggingServer localhost:1025
</span></span></code></pre></div><p>Just like the PrintingMailHost, this SMTP server prints the emails to
standard output. For more information see the
<a href="https://docs.djangoproject.com/en/dev//topics/email/#testing-e-mail-sending">&ldquo;Testing e-mail sending&rdquo; section</a>
in the Django documentation.</p>
<div class="note update">
  <div class="note_header">
    Update (2010-08-09)<span class="hidden">:</span>
  </div>
  <div class="note_body">
    For the developer with a deadline: install
<a href="https://pypi.org/project/django-extensions/">django-extensions</a>
which has a couple of useful extra features. One of them is the
<code>mail_debug</code> management command. This commands starts the same SMTP
debugging server, but you don&rsquo;t have to remember the right
incantation.
  </div>
</div>

<h2 id="http-server">HTTP server</h2>
<p>Somewhat related to this: if you want to test your application against
an HTTP server, you can use this command:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">python -m SimpleHTTPServer
</span></span></code></pre></div><p>The
<a href="https://docs.python.org/2.7/library/simplehttpserver.html#module-SimpleHTTPServer">SimpleHTTPServer module</a>
can be used to get server up-and-running quickly. It can also be a
simple way to, for instance, copy files from one machine to
another. By running the HTTP server in the directory containing the
files, you can access the files via your browser on another
machine. Safe? No. Convenient? Yes.</p>]]></content>
  </entry>
</feed>
