Unit Testing with Python

Vijay Kumar (~bravegnu)





Emphasize the need for unit testing Python code, and provide the necessary tools and techniques to get started with unit testing.


The workshop is aimed at people who are familiar with Python basics, and would like to improve their skills, by learning how to write professional quality Python code.

Modifying Python code without unit test cases is like walking through a field of landmines. Python's dynamic nature allows typos in variable / attribute names, and incorrect indentation to go unnoticed, until a specific input drives it through the erroneous code path. Integration testing be it manual or automated is insufficient and can at times be frustating for Python code.

Fortunately Python's own standard library makes it easy to write unit test cases, by providing the needed infrastructure. Through this workshop we would like to make a strong case for unit testing, and then show how to use the available frameworks for writing test cases. We will also cover how dependencies can be isolated using mocking and faking. We then move beyond writing test cases and cover reporting, code coverage and CI integration.


  • Introduction to Unit Testing - 10 min

    • Need for Unit Testing in Python
    • Unit Testing Basics
    • Integration Testing vs Unit Testing
  • Getting Started - 35 min

    • Unit Testing Framework
    • Writing Simple Unit Tests
    • Arrange - Act - Assert
    • Setup and Tear Down
  • Mocking and Patching - 40 min

    • Isolating Dependencies with Mocks
      • How Mock Objects Works
      • Unit Testing with Mocks
    • Patching Internal Dependencies
      • How Patching Works
      • Unit Testing with Patching
  • Faking - 20 min

    • Faking vs Mocking
    • Case Study: Fake filesystem - pyfakefs
    • Unit testing with Faking
  • Managing Test Cases - 20 min

    • Organizing Test Cases
    • Test Discovery
    • Test Reports
    • Code Coverage Reports
    • Integrating with CI
  • Pointers and Resources - 5 min

  • Questions - 10 min


Should be familiar with basic object oriented programming with Python.

Content URLs:

The draft slides are available from https://www.dropbox.com/s/50kbcpg1ee60xjg/slides.html?dl=1 All the code dependencies are embedded in the slides, as data-uris, including the complete game sokoban.

I had shared my own experiences with Python and the importance of unit testing in the lightning talk "Bitten by Python" [Video Recording]

Speaker Info:

I am an embedded software developer and trainer by profession. As part of my work at Zilogic Systems, I do training in various topics related to Embedded Systems, Linux and Python. I coordinate the activities of the Chennai Python User Group http://chennaipy.org In the past 5 years I have done over 50 talks at Chennaipy. My work was recognized by the Python Software Society of India with the Kenneth Gonsalves Award for the year 2015. I help promote usage of Linux in embedded systems through the meetup group http://meetup.com/embedded-linux by organizing workshops and coding dojos.

Links to recordings of previous talks at conferences / meetups:

  • I did a lightning talk at PyCon India 2015, titled "Bitten by Python". [Video Recording]
  • I did a full length talk at PyCon India 2010, titled "Device Interfacing with Python and ZIO": [Proposal]
  • Talk at Chennaipy, titled Managing Desktops with Ansible [Screencast]
  • Talk at Chennaipy, titled Hangman Design and Implementation [Screencast]

I have made contributions to the Linux Kernel, U-Boot, Qemu and the Linux Testing Project.

Speaker Links:

  • GitHub: http://github.org/bravegnu
  • LinkedIn: http://linkedin.com/in/bravegnu
  • Blog:
    • Personal: http://bravegnu.org
    • Company: http://www.zilogic.com/author/vijaykumar.html
  • Twitter: http://twitter.com/bravegnu
  • SlideShare: http://www.slideshare.net/VijayKumarBagavathSi/

Section: Testing
Type: Workshops
Target Audience: Beginner
Last Updated:

No comments added so far.

Login to add a new comment.