Book Image

Python Testing: Beginner's Guide

By :
Book Image

Python Testing: Beginner's Guide

By:

Overview of this book

<p>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.<br /><br />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. <br /><br />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.<br /><br />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.</p>
Table of Contents (17 chapters)
Python Testing
Credits
About the Author
About the Reviewers
Preface
Index

Time for action – expecting an exception


This is yet another test that you can add to test.txt, this time testing some code that ought to raise an exception.

  1. Insert the following text into your doctest file (Please note that the last line of this text has been wrapped due to the constraints of the book's format, and should be a single line):

    Here we use doctest's exception syntax to check that Python is correctly enforcing its grammar.
    
    >>> def faulty():
    ...     yield 5
    ...     return 7
    Traceback (most recent call last):
    SyntaxError: 'return' with argument inside generator (<doctest test.txt[5]>, line 3)
  2. The test is supposed to raise an exception, so it will fail if it doesn't raise the exception, or if it raises the wrong exception. Make sure you have your mind wrapped around that: if the test code executes successfully, the test fails, because it expected an exception.

  3. Run the tests using doctest and the following screen will be displayed:

What just happened?

Since Python doesn...