Book Image

Learning Python Testing

By :
Book Image

Learning Python Testing

By:

Overview of this book

<p>Automated testing is the best way to increase efficiency and decrease the defects of software testing. It takes away much of the effort on your part so that you can find bugs early and easily. The Python unit testing framework makes it easy to write efficient automated test cases in Python. Applications involving many paradigms and styles can be very complicated to test, but with the right tools, testing becomes the simplest part of the development process.</p> <p>This book starts with a short introduction to testing, and then introduces the doctest tool, both in terms of practicalities and how it fits into the testing environment. From there, the discussion proceeds to unittest.mock and mock objects, and to unittest. Next, Nose is introduced and discussed. Later on, focus turns from the tools themselves toward best practices and disciplines of testing. Finally, the integration of testing with existing build processes and toolchains is covered. By the end of this book, you will know how to use automated testing quickly and easily and in a way that helps catch bugs early and fix them.</p>
Table of Contents (16 chapters)
Learning Python Testing
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Nose and unittest tests


Nose enhances unittest by providing test fixtures at the package and module levels. The package setup function is run before any of the tests in any of the modules in a package, while the teardown function is run after all of the tests in all of the modules in the package have completed. Similarly, the module setup function is run before any of the tests in a given module have been executed, and the module teardown function is executed after all of the tests in the module have been executed.

Module fixture practice

We're going to build a test module with a module-level fixture. In the fixture, we'll replace the datetime.date.today function, which normally returns an object representing the current date. We want it to return a specific value, so that our tests can know what to expect. Perform the following steps:

  1. Create a directory called tests.

  2. Within the tests directory, create a file called module_fixture_tests.py containing the following code:

    from unittest import...