Testing 101 with py.test
Praveen Shirali (~praveen2) |
A workshop on testing python code using the py.test framework.
I plan to cover the following aspects:
- Overview of basic tools in python & some packages which a python developer must be aware of: pdb, unittest, mock, coverage, virtualenv etc. [10 min]
- Why py.test? What problems does it aim to solve? [5 min]
- Hands-on: writing tests for success and failure. Asserting results and handling exceptions. [20 min]
- Mocking: How to mock. Where to patch. What to assert on. When not to mock. How to refactor code to make it testable. Monkey-patching & dependency-injection [20 min]
- py.test fixtures: How to modularize setup and teardown. Usage of: finalizers, fixture scopes, autouse fixtures etc. [20 min]
- conftest.py: How to segregate fixtures into conftest. Usage of py.test hooks and assertion helpers [10 min]
- Managing test data and execution: parametrization, marking tests, skipping tests etc [10 min]
- Testing test-code/framework code. Utilizing pdb to debug tests. Using coverage as a tool identify areas that need to be tested. [15 min]
- An overview of other advanced features of py.test -- plugins, tox, etc. [10 min]
Points 3-8 will be hands-on. Code examples will cover unittests and functional tests (ReST API tests against a web-server).
This workshop is for you if:
- You are a developer who has written some test code, but you are keen to more info on py.test and testing in general.
- You are a developer who has never written any test code before. You are new to concepts of mocking, patching, dependency-injection, refactoring code for testability etc. You are looking for a place to start.
- You are a QA Engineer who uses python for automation and you'd like to see how py.test could help you write tests.
- You must know variable scopes, modules and packages in python. You must be comfortable with magic methods and reflection.
- You must be able to write object-oriented code in Python.
- You must be comfortable inspecting objects. Given an object, you know how to identify attributes, bound methods, type, instance checking, mro, etc.
- You must know decorators.
I am a Test Architect at RiptideIO (http://www.riptideio.com) with around 12+ years of QA experience. I have been using Python for the last 5+ years and py.test for a little over a year. Over the last one year I have been involved in designing and developing test solutions, frameworks and test cases to test our company's products; all of which utilize py.test as the underlying test framework. In the past, I have worked on BDD and functional frameworks (like RobotFramework).
I gave a lightning talk on py.test in PyCon India 2014.