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

Preface

Testing has always been a part of software development. For decades, comprehensive testing was defined by complex manual test procedures backed by big budgets; but something revolutionary happened in 1998. In his Guide to Better Smalltalk, Smalltalk guru Kent Beck introduced an automated test framework called SUnit. This triggered an avalanche of test frameworks including JUnit, PyUnit, and many others for different languages and various platforms, dubbed the xUnit movement. Automated testing was made a cornerstone of the agile movement when 17 top software experts signed the Agile Manifesto in 2001.

Testing includes many different styles including unit testing, integration testing, acceptance testing, smoke testing, load testing, and countless others. This book digs in and explores testing at all the important levels while using the nimble power of Python. It also shows many tools.

This book is meant to expand your knowledge of testing from something you either heard about or have practiced a little into something you can apply at any level to meet your needs in improving software quality. I hope to give you the tools to reap huge rewards in better software development and customer satisfaction.

What this book covers

Chapter 1, Using Unittest to Develop Basic Tests, gives you a quick introduction to the most commonly used test framework in the Python community.

Chapter 2, Running Automated Tests with Nose, introduces the most ubiquitous Python test tool and gets busy by showing how to write specialized plugins.

Chapter 3, Creating Testable Documentation with doctest, shows many different ways to use Python's docstrings to build runnable doctests as well as writing custom test runners.

Chapter 4, Testing Customer Stories with Behavior Driven Development, dives into writing easy-to-read testable customer stories using doctest, mocking, and Lettuce/Should DSL.

Chapter 5, High Level Customer Scenarios with Acceptance Testing, helps you get into the mindset of the customer and write tests from their perspective using Pyccuracy and the Robot Framework.

Chapter 6, Integrating Automated Tests with Continuous Integration, shows how to add continuous integration to your development process with Jenkins and TeamCity.

Chapter 7, Measuring your Success with Test Coverage, explores how to create coverage reports and interpret them correctly. It also digs in to see how to tie them in with your continuous integration system.

Chapter 8, Smoke/Load TestingTesting Major Parts, shows how to create smoke test suites to get a pulse from the system. It also shows how to put the system under load to make sure it can handle the current load as well as finding the next breaking point for future loads.

Chapter 9, Good Test Habits for New and Legacy Systems, shows many different lessons learned from the author about what works when it comes to software testing.

What you need for this book

You will need Python 2.6 or above. The recipes in this book have NOT been tested against Python 3+. This book uses many other Python test tools, but includes detailed steps to show how to install and use them.

Who this book is for

This book is for Python developers who want to take testing to the next level. It covers different styles of testing, giving any developer an expanded set of testing skills to help write better systems. It also captures lessons learned from the author, explaining not only how to write better tests but why.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Create a new file called recipe1.py to store all of this recipe's code."

A block of code is set as follows:

  def test_parsing_millenia(self):
    value = RomanNumeralConverter("M")
    self.assertEquals(1000, value.convert_to_decimal())

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

if __name__ == "__main__":
  unittest.main()

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The unittest module provides a convenient way to find all the test methods in a TestClass".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.1