Want this title & more?

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781847198846

Paperback256 pages

About This Book

Covers everything you need to test your code in Python

Easiest and enjoyable approach to learn Python testing

Write, execute, and understand the result of tests in the unit test framework

Packed with step-by-step examples and clear explanations

Who This Book Is For

If you are a Python developer and want to write tests for your applications, this book will get you started and show you the easiest way to learn testing.

You need to have sound Python programming knowledge to follow along. An awareness of software testing would be good, but no formal knowledge of testing is expected nor do you need to have any knowledge of the libraries discussed in the book.

Table of Contents

Chapter 1: Testing for Fun and Profit

How can testing help?

Types of testing

You've got Python, right?

Summary

Chapter 2: Doctest: The Easiest Testing Tool

Basic doctest

Time for action – creating and running your first doctest

Time for action – writing a more complex test

Time for action – expecting an exception

Time for action – using ellipsis in tests

Time for action – normalizing whitespace

Time for action – skipping tests

Embedding doctests in Python docstrings

Time for action – embedding a doctest in a docstring

Putting it in practice: an AVL tree

Summary

Chapter 3: Unit Testing with Doctest

What is Unit testing and what it is not?

Time for action – identifying units

Unit testing throughout the development process

Time for action – unit testing during design

Time for action – unit testing during development

Time for action – unit testing during feedback

Time for action – unit testing during development... again

Time for action – unit testing during maintenance

Time for action – unit testing during reuse

Summary

Chapter 4: Breaking Tight Coupling by using Mock Objects

Installing Python Mocker

Time for action – installing Python Mocker

The idea of a mock object

Python Mocker

Time for action – exploring the basics of Mocker

Mocking self

Time for action – passing a mock object as self

Summary

Chapter 5: When Doctest isn't Enough: Unittest to the Rescue

Basic unittest

Time for action – testing PID with unittest

Test fixtures

Time for action – testing database-backed units

Integrating with Python Mocker

Summary

Chapter 6: Running Your Tests: Follow Your Nose

What is Nose?

Installing Nose

Organizing tests

Time for action – organizing tests from previous chapters

Nose and doctest

Time for action – creating a fixture for a doctest

Nose and unittest

Time
for action – creating a module fixture

Time for action – creating a package fixture

Nose's own testing framework

Time for action – using Nose-specific tests

Summary

Chapter 7: Developing a Test-Driven Project

Writing the specification

Time for action – what are you going to do?

Writing initial unit tests

Time for action – nailing down the specification with unit tests

Coding planner.data

Using the tests to get the code right

Fixing the code

Time for action – writing and debugging code

Writing persistence tests

Writing persistence code

Finishing up

Summary

Chapter 8: Testing Web Application Frontends using Twill

Installing Twill

Exploring the Twill language

Time for action – browsing the web with Twill

Time for action – Twill scripting

Calling Twill scripts from tests

Time for action – running Twill script files

Time for action – running Twill script strings

Integrating Twill operations into unittest tests

Time for action – using Twill's browser object

Summary

Chapter 9: Integration Testing and System Testing

Integration tests and system tests

Time for action – figuring out the order of integration

Automation with doctest, unittest, and Nose

Time for action – writing integration tests for the time planner

Summary

Chapter 10: Other Testing Tools and Techniques

Code coverage

Time for action – using coverage.py

Version control hooks

Time for action – installing Nose as a Bazaar post-commit hook

Time for action – installing Nose as a Mercurial post-commit hook

Time for action – installing Nose as a Git post-commit hook

Time for action – installing Nose as a Darcs post-record hook

Time for action – installing Nose as a Subversion post-commit hook

Automated continuous integration

Time for action – using Buildbot with Bazaar

Summary

What You Will Learn

Make testing easy and convenient with Python's doctest module, which lets you put your tests right into your doc strings

Drive development using unit testing and learn how it can make the process quicker

Explore the assorted features Mocker provides to help you keep units separate from each other and customize it to suit your needs

Embed your tests in a controlled and temporary environment using test fixtures

Simplify the integration of unittest and Mocker using Python Mocker's MockerTestCase

Chapter 9 teaches how to build up from unit tests to tests of a complete software system.

Chapter 10 introduces code coverage and continuous integration, and teaches how to tie automated testing into version control systems.

In Detail

Automated testing moves much of the labor of testing off the developer and makes it easier as well as quicker to find bugs and fix them. Automated tests run faster, increase test coverage, and lower costs. However, testing is neither an easy process nor remotely exciting for most developers. But with the right techniques and tools, testing can become a simple and gratifying part of the development process.

With this helpful guide – from an expert – in your hand, testing will not be a tiresome topic for you anymore. You will learn how to test your Python projects in the easiest way, making other parts of the development process easier and more enjoyable.

This book covers the tools and techniques of automated testing and test-driven development. Starting from the very basics, the chapters introduce new tools and techniques in simple, accessible language with step-by-step examples. You will explore how to make testing easier and more accurate with Python's doctest module and learn test-driven development using the unittest framework. You will also learn how to keep your units separate from each other and discover a simple and easy way to integrate Mocker and unittest. Next, we cover integration testing and web application testing.

Automated testing gives developers better feedback, faster and more often. Bugs get found sooner and fixed better, with less effort. By the end of this book, you will have all of the skills needed to benefit from automated testing.

Authors

Daniel Arbuckle

Daniel Arbuckle received his PhD. degree in Computer Science from the University of Southern California in 2007. He is an active member of the Python community and an avid unit tester.

Alerts & Offers

Series & Level

We understand your time is important. Uniquely amongst the major publishers, we seek to develop and publish the broadest range of learning and information products on each technology. Every Packt product delivers a specific learning pathway, broadly defined by the Series type. This structured approach enables you to select the pathway which best suits your knowledge level, learning style and task objectives.

Learning

As a new user, these step-by-step tutorial guides will give you all the practical skills necessary to become competent and efficient.

Beginner's Guide

Friendly, informal tutorials that provide a practical introduction using examples, activities, and challenges.

Essentials

Fast paced, concentrated introductions showing the quickest way to put the tool to work in the real world.

Cookbook

A collection of practical self-contained recipes that all users of the technology will find useful for building more powerful and reliable systems.

Blueprints

Guides you through the most common types of project you'll encounter, giving you end-to-end guidance on how to build your specific solution quickly and reliably.

Mastering

Take your skills to the next level with advanced tutorials that will give you confidence to master the tool's most powerful features.

Starting

Accessible to readers adopting the topic, these titles get you into the tool or technology so that you can become an effective user.

Progressing

Building on core skills you already have, these titles share solutions and expertise so you become a highly productive power user.