Free Sample
+ Collection

Python Testing Cookbook

Greg L. Turnquist

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.
RRP $26.99
RRP $44.99
Print + eBook

Want this title & more?

$12.99 p/month

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 139781849514668
Paperback364 pages

About This Book

  • Learn to write tests at every level using a variety of Python testing tools
  • The first book to include detailed screenshots and recipes for using Jenkins continuous integration server (formerly known as Hudson)
  • Explore innovative ways to introduce automated testing to legacy systems
  • Written by Greg L. Turnquist – senior software engineer and author of Spring Python 1.1
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible

Who This Book Is For

Python developers and programmers with a basic understanding of Python and Python testing will find this cookbook beneficial. It will build on that basic knowledge equipping you with the intermediate and advanced skills required to fully utilize the Python testing tools. Broken up into lots of small code recipes, you can read this book at your own pace, whatever your experience. No prior experience of automated testing is required.

Table of Contents

Chapter 1: Using Unittest To Develop Basic Tests
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
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
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
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
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
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
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
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
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
  • Add testable documentation to your code
  • Filter out test noise, customize test reports, and tweak doctest's to meet your needs
  • 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


In Detail

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.



Read More

Recommended for You

Learning Python Data Visualization
$ 28.99