Python Testing Cookbook

Making your life easier with automated testing of Python is the sole aim of this book. Because it’s a cookbook, you can take things at your own pace, in your own order, and learn practical application rather than nebulous theory.

Python Testing Cookbook

Greg L. Turnquist

May 2011

Making your life easier with automated testing of Python is the sole aim of this book. Because it’s a cookbook, you can take things at your own pace, in your own order, and learn practical application rather than nebulous theory.

Book Details

ISBN 139781849514668

Paperback364 pages

Book Description

Are you looking at new ways to write better, more efficient tests? Are you struggling to add automated testing to your existing system? The Python unit testing framework, originally referred to as "PyUnit" and now known as unittest, is a framework that makes it easier for you to write automated test suites efficiently in Python. This book will show you exactly how to squeeze every ounce of value out of automated testing.

The Python Testing Cookbook will empower you to write tests using lots of Python test tools, code samples, screenshots, and detailed explanations. By learning how and when to write tests at every level, you can vastly improve the quality of your code and your personal skill set. Packed with lots of test examples, this will become your go-to book for writing good tests.

This practical cookbook covers lots of test styles including unit-level, test discovery, doctest, BDD, acceptance, smoke, and load testing. It will guide you to use popular Python tools effectively and discover how to write custom extensions. You will learn how to use popular continuous integration systems like Jenkins (formerly known as Hudson) and TeamCity to automatically test your code upon check in. This book explores Python's built-in ability to run code found embedded in doc strings and also plugging in to popular web testing tools like Selenium. By the end of this book, you will be proficient in many test tactics and be ready to apply them to new applications as well as legacy ones.

Table of Contents

Chapter 1: Using Unittest To Develop Basic Tests

Introduction

Asserting the basics

Setting up and tearing down a test harness

Running test cases from the command line with increased verbosity

Running a subset of test case methods

Chaining together a suite of tests

Defining test suites inside the test module

Retooling old test code to run inside unittest

Breaking down obscure tests into simple ones

Testing the edges

Testing corner cases by iteration

Chapter 2: Running Automated Test Suites with Nose

Introduction

Getting nosy with testing

Embedding nose inside Python

Writing a nose extension to pick tests based on regular expressions

Writing a nose extension to generate a CSV report

Writing a project-level script that lets you run different test suites

Chapter 3: Creating Testable Documentation with doctest

Introduction

Documenting the basics

Catching stack traces

Running doctests from the command line

Coding a test harness for doctest

Filtering out test noise

Printing out all your documentation including a status report

Testing the edges

Testing corner cases by iteration

Getting nosy with doctest

Updating the project-level script to run this chapter's doctests

Chapter 4: Testing Customer Stories with Behavior Driven Development

Introduction

Naming tests that sound like sentences and stories

Testing separate doctest documents

Writing a testable story with doctest

Writing a testable novel with doctest

Writing a testable story with Voidspace Mock and nose

Writing a testable story with mockito and nose

Writing a testable story with Lettuce

Using Should DSL to write succinct assertions with Lettuce

Updating the project-level script to run this chapter's BDD tests

Chapter 5: High Level Customer Scenarios with Acceptance Testing

Introduction

Installing Pyccuracy

Testing the basics with Pyccuracy

Using Pyccuracy to verify web app security

Installing the Robot Framework

Creating a data-driven test suite with Robot

Writing a testable story with Robot

Tagging Robot tests and running a subset

Testing web basics with Robot

Using Robot to verify web app security

Creating a project-level script to verify this chapter's acceptance tests

Chapter 6: Integrating Automated Tests with Continuous Integration

Introduction

Generating a continuous integration report for Jenkins using NoseXUnit

Configuring Jenkins to run Python tests upon commit

Configuring Jenkins to run Python tests when scheduled

Generating a CI report for TeamCity using teamcity-nose

Configuring TeamCity to run Python tests upon commit

Configuring TeamCity to run Python tests when scheduled

Chapter 7: Measuring your Success with Test Coverage

Introduction

Building a network management application

Installing and running coverage on your test suite

Generating an HTML report using coverage

Generating an XML report using coverage

Getting nosy with coverage

Filtering out test noise from coverage

Letting Jenkins get nosy with coverage

Updating the project-level script to provide coverage reports

Chapter 8: Smoke/Load Testing—Testing Major Parts

Introduction

Defining a subset of test cases using import statements

Leaving out integration tests

Targeting end-to-end scenarios

Targeting the test server

Coding a data simulator

Recording and playing back live data in real time

Recording and playing back live data as fast as possible

Automating your management demo

Chapter 9: Good Test Habits for New and Legacy Systems

Introduction

Something is better than nothing

Coverage isn't everything

Be willing to invest in test fixtures

If you aren't convinced on the value of testing, your team won't be either

Harvesting metrics

Capturing a bug in an automated test

Separating algorithms from concurrency

Pause to refactor when test suite takes too long to run

Cash in on your confidence

Be willing to throw away an entire day of changes

Instead of shooting for 100 percent coverage, try to have a steady growth

Randomly breaking your app can lead to better code

What You Will Learn

Get started with the basics of writing automated unit tests and asserting results

Use Nose to discover tests and build suites automatically

Write Nose plugins that control what tests are discovered and how to produce test reports

Write testable stories using lots of tools including doctest, mocks, Lettuce, and Should DSL

Get started with the basics of customer-oriented acceptance testing

Test the web security of your application

Configure Jenkins and TeamCity to run your test suite upon check-in

Capture test coverage reports in lots of formats, and integrate with Jenkins and Nose

Take the pulse of your system with a quick smoke test and overload your system to find its breaking points

Add automated testing to an existing legacy system that isn't test oriented

Authors

Greg L. Turnquist

Greg L. Turnquist has been a software professional since 1997. In 2002, he joined the senior software team that worked on Harris' $3.5 billion FAA telco program, architecting mission-critical enterprise apps while managing a software team. He provided after-hours support to a nation-wide system and is no stranger to midnight failures and software triages. In 2010, he joined the SpringSource division of VMware, which was spun off into Pivotal in 2013.

As a test-bitten script junky, Java geek, and JavaScript Padawan, he is a member of the Spring Data team and the lead for Spring Session MongoDB. He has made key contributions to Spring Boot, Spring HATEOAS, and Spring Data REST while also serving as editor-at-large for Spring's Getting Started Guides.

Greg wrote technical best sellers Python Testing Cookbook and Learning Spring Boot, First Edition, for Packt. When he isn't slinging code, Greg enters the world of magic and cross swords, having written the speculative fiction action and adventure novel, Darklight.

He completed his master's degree in computer engineering at Auburn University and lives in the United States with his family.

Table of Contents

Chapter 1: Using Unittest To Develop Basic Tests

Introduction

Asserting the basics

Setting up and tearing down a test harness

Running test cases from the command line with increased verbosity

Running a subset of test case methods

Chaining together a suite of tests

Defining test suites inside the test module

Retooling old test code to run inside unittest

Breaking down obscure tests into simple ones

Testing the edges

Testing corner cases by iteration

Chapter 2: Running Automated Test Suites with Nose

Introduction

Getting nosy with testing

Embedding nose inside Python

Writing a nose extension to pick tests based on regular expressions

Writing a nose extension to generate a CSV report

Writing a project-level script that lets you run different test suites

Chapter 3: Creating Testable Documentation with doctest

Introduction

Documenting the basics

Catching stack traces

Running doctests from the command line

Coding a test harness for doctest

Filtering out test noise

Printing out all your documentation including a status report

Testing the edges

Testing corner cases by iteration

Getting nosy with doctest

Updating the project-level script to run this chapter's doctests

Chapter 4: Testing Customer Stories with Behavior Driven Development

Introduction

Naming tests that sound like sentences and stories

Testing separate doctest documents

Writing a testable story with doctest

Writing a testable novel with doctest

Writing a testable story with Voidspace Mock and nose

Writing a testable story with mockito and nose

Writing a testable story with Lettuce

Using Should DSL to write succinct assertions with Lettuce

Updating the project-level script to run this chapter's BDD tests

Chapter 5: High Level Customer Scenarios with Acceptance Testing

Introduction

Installing Pyccuracy

Testing the basics with Pyccuracy

Using Pyccuracy to verify web app security

Installing the Robot Framework

Creating a data-driven test suite with Robot

Writing a testable story with Robot

Tagging Robot tests and running a subset

Testing web basics with Robot

Using Robot to verify web app security

Creating a project-level script to verify this chapter's acceptance tests

Chapter 6: Integrating Automated Tests with Continuous Integration

Introduction

Generating a continuous integration report for Jenkins using NoseXUnit

Configuring Jenkins to run Python tests upon commit

Configuring Jenkins to run Python tests when scheduled

Generating a CI report for TeamCity using teamcity-nose

Configuring TeamCity to run Python tests upon commit

Configuring TeamCity to run Python tests when scheduled

Chapter 7: Measuring your Success with Test Coverage

Introduction

Building a network management application

Installing and running coverage on your test suite

Generating an HTML report using coverage

Generating an XML report using coverage

Getting nosy with coverage

Filtering out test noise from coverage

Letting Jenkins get nosy with coverage

Updating the project-level script to provide coverage reports

Chapter 8: Smoke/Load Testing—Testing Major Parts

Introduction

Defining a subset of test cases using import statements

Leaving out integration tests

Targeting end-to-end scenarios

Targeting the test server

Coding a data simulator

Recording and playing back live data in real time

Recording and playing back live data as fast as possible

Automating your management demo

Chapter 9: Good Test Habits for New and Legacy Systems

Introduction

Something is better than nothing

Coverage isn't everything

Be willing to invest in test fixtures

If you aren't convinced on the value of testing, your team won't be either

Harvesting metrics

Capturing a bug in an automated test

Separating algorithms from concurrency

Pause to refactor when test suite takes too long to run

Cash in on your confidence

Be willing to throw away an entire day of changes

Instead of shooting for 100 percent coverage, try to have a steady growth

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.