Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Test Driven Python Development

You're reading from   Test Driven Python Development Develop high-quality and maintainable Python applications using the principles of test-driven development

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781783987924
Length 264 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Siddharta Govindaraj Siddharta Govindaraj
Author Profile Icon Siddharta Govindaraj
Siddharta Govindaraj
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Test-Driven Python Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with Test-Driven Development FREE CHAPTER 2. Red-Green-Refactor – The TDD Cycle 3. Code Smells and Refactoring 4. Using Mock Objects to Test Interactions 5. Working with Legacy Code 6. Maintaining Your Test Suite 7. Executable Documentation with doctest 8. Extending unittest with nose2 9. Unit Testing Patterns 10. Tools to Improve Test-Driven Development Answers to Exercises Working with Older Python Versions Index

Writing code that is compatible across versions


Many Python modules these days are designed to run under multiple Python versions, especially supporting Python 2.x as well as Python 3.x versions. We will want to run the same tests in both versions, and to do this, we will need to write our code in such a way that the tests are compatible with both the versions.

Python's import mechanism gives us the flexibility we need to do this. At the top of the file, we import unittest like the following:

try:
   import unittest2 as unittest
except ImportError:
   import unittest

What this does is to first try and import unittest2. If we are running Python 2.x, then we should have installed this already. If it succeeds, then the module is imported and the module reference is renamed to unittest.

If we get an ImportError, then we are running Python 3.x, in which case we can import the unittest module bundled in the standard library.

Later in the code, we can just reference the unittest module and it will work normally.

This mechanism depends on the unittest2 module being always installed when using Python 2.x version. This is easily achieved by putting the unittest2 module as a dependency for only Python 2.x in our pip requirements file.

A similar approach works for mocks as follows:

try:
   from unittest import mock
except ImportError:
   import mock

Here we first try to import the mock library provided as a part of the unittest standard library module. This is available in Python 3.3 onward. If the import succeeds, then the mock library is imported. If it fails, it means that we are running an older Python version, so we directly import the mock library that we installed from PyPi.

Note how we use the line from unittest import mock instead of import unittest.mock. This is so that we end up with the same module reference name in both the cases. Once the import is done, we can reference the mock module in our code and it will work across Python versions.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images