kracekumar ramaraju

kracekumar ramaraju

Audience level:
2 p.m.–6 p.m.

Writing unit tests and integration test in Python


Lot of programmers use Python on daily basis, hardly few write tests. Writing tests can be daunting task. Tests gives lot of confidence for shipping the code. Automated tests saves lot of developer time during refactor.

This tutorial will teach audience how to write unit test, functional tests using unittest, py.test, mock and coverage library.


Objective of the tutorial is to make audience write better and useful tests. If something is hard to test, the abstraction is broken. In general testable code is good code.

Tutorial will use simple command line phonebook application which can be found here. During the course of the tutorial tests will be added to the application. These tests will be unit tests, functional tests.

The tutorial will use python standard library unittest, pytest, mock, coverage. Tutorial will not cover testing web applications with selenium.

While creating large application like web application the external service are generally mocked like database, email server etc ... Mock libraries provides set functionality to test complicated cases.

Audience should be comfortable using Command Line, virtualenv, pip and running tests from command line.

The code for the workshop will be available in . We will using Python 2.7.

Topics covered

  • How to use unittest module
  • How to use py.test and why py.test
  • Writing simple functions as testcases
  • Using different py.test command line features
  • Capturing stdout with py.test
  • Setting fixtures with py.test
  • Writing Test classes which py.test can pick
  • Using pytest.fixture
  • Running specific test module/class/function etc ...
  • Writing setup and teardown in pytest style
  • How to mock expensive resources like network call, System Call.
  • Patching modules/functions/classes.
  • How to use Stubs/Fakes.

Platinum Sponsors:

Silver Sponsors: