Book Image

Python Testing Cookbook

By : Greg L. Turnquist
Book Image

Python Testing Cookbook

By: Greg L. Turnquist

Overview of this book

<p>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.<br /><br />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.<br /><br />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.</p> <p>&nbsp;</p>
Table of Contents (16 chapters)
Python Testing Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using Should DSL to write succinct assertions with Lettuce


Lettuce (http://lettuce.it) is a BDD tool built for Python.

The Should DSL (http://www.should-dsl.info) provides a simpler way to write assertions for Thens.

This recipe shows how to install Lettuce and Should DSL. Then, we will write a test story. Finally, we will wire it into our shopping cart application using the Should DSL to exercise our code.

Getting ready

For this recipe, we will be using the shopping cart application shown at the beginning of this chapter. We also need to install Lettuce and its dependencies:

  • Install lettuce by typing pip install lettuce

  • Install Should DSL by typing pip install should_dsl

How to do it...

With the following steps, we will use the Should DSL to write more succinct assertions in our test stories:

  1. Create a new directory called recipe33 to contain all the files for this recipe.

  2. Create a new file in recipe33 called recipe33.feature to contain our test scenarios.

  3. Create a story in recipe33.feature with...