This workshop covers all aspects of automated testing in Python. It focuses a lot on two methodologies: Test Driven Development and Behavior Driven Development. It covers test isolation, mocking, a wide range of testing frameworks and tools (unittest, doctest, nose2, behave, nose2.tools.such, webtest among others) as well as code coverage and an overview of unconventional testing approaches.
What will you learn?
- How to start writing tests?
- How to isolate your tests from external resources, like databases, sockets or even external APIs?
- How to start testing systems consisting of multiple modules?
- How to refactor code?
- How to test web applications?
- What tools, libraries and framework are available in Python and how to choose the correct one?
- How outside-in development style helps you create better code.
- How to write plain-text stories in Gherkin, so that non-developers can read your tests?
- And much, much more.
We start from a discussion on SOLID principles that makes code easier to read and maintain. This part is supplemented with exercises showing how to apply (and how not to do this) SOLID in Python. This will be helpful in the second step of each iteration in TDD (as well as BDD), that is writing production code.
The third step of each iteration in both TDD and BDD is refactoring, that is making code easier to read and maintain without changing its functionality.
In this part, we cover all building blocks (or actions) of refactoring. We introduce the concept of code bad smells that help you find opportunities for correct refactoring and avoid “endless refactoring back and forth”.
Builtin Testing Frameworks
First step of each iteration in TDD is writing a test.
We start from a discussion if and why writing tests is beneficial. Contrary to intuition, less bugs is only one of the advantages of covering code with tests.
We usually use a framework that makes writing tests easier. We show how to use the most popular testing framework – unittest.
We also introduce very agile doctest library which help you test, and at the same time, document small utility functions without all the burden and long-winded unittest.
Test Driven Development
After short theoretical introduction to TDD, we solve Katas – programming challenges like RecentlyUsedList or BowlingGame. Katas are simple exercises that let you focus on the TDD methodology rather than fighting with language, IDE or business logic.
We start from a live coding – the instructor shows how you can solve a Kata using TDD. Participants ask questions and discuss with the instructor and other participants. Later on, we move to another Kata. This time participants do the exercise in pairs (pair programming).
Test Isolation and Mocking
Test isolation and mocking is covered during second day of the training. We introduce great unittest.mock builtin library (or mock backport if you work on Python 2). It helps you isolate your tests from system calls, filesystem, databases, external APIs or any other resources. We study the most common cases.
Mocking is also an essential part of outside-in development which is covered during the last day.
Testing Web Applications
Testing Web Application let you focus on one of common cases when you need to test a web application written using a framework. For simplicity, this framework is Flask unless you work in Django, in which case we focus on Django’s own challenges.
A number of tools are discussed, Selenium and builtin Flask client being two of them. However, we introduce and focus on WebTest which is other client and is much faster than Selenium.
Overview of Other Testing Approaches and Code Coverage
At the end of the second day, the instructor introduces other testing approaches. We discuss property-based testing (with hypothesis-python library) and mutation testing (with MutPy). We also introduce a concept of code coverage and show how to configure it.
Most of the time, programmers develop their code inside-out. They start from the simplest functions and tools (inner or lower modules), maybe even they test them, and built outer (or higher) modules upon them. For example, they first implement backend, and then frontend. This is dictated by the fact that the outer modules depend on the lower ones, for example frontend depends on backend. Therefore, you cannot test outer ones without inner ones.
However, this results in bad interfaces, because outer modules are written to match inner modules instead of required behavior. Outside-in development forces you to first implement higher modules, test them mocking the lower modules, and then focus on the inner modules. This results in better interfaces between modules.
Plain-Text Stories in Gherkin.
We introduce a special domain specific-language based on English natural language that helps you write acceptance tests understandable for non-programmers. If you have ever heard about Cucumber, you’ll be astonished that there is a very similar framework in Python called behave.
We introduce another style of BDD – executable specification written with nose2.tools.such plugin, a DSL in Python.
We also introduce nose2 tool to run unit tests and show how you configure it and enable nose2.tools.such plugin.
Behaviour Driven Development
Behave Driven Development is an extension of TDD. After a short theoretical introduction, we show on a case study how you can use behave and nose2.tools.such with BDD. We also show how professional edition of PyCharm supports BDD and behave framework.