Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Essentials

You're reading from   Python Essentials Modernize existing Python code and plan code migrations to Python using this definitive guide

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784390341
Length 298 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Steven F. Lott Steven F. Lott
Author Profile Icon Steven F. Lott
Steven F. Lott
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Simple Data Types 3. Expressions and Output 4. Variables, Assignment and Scoping Rules 5. Logic, Comparisons, and Conditions 6. More Complex Data Types 7. Basic Function Definitions 8. More Advanced Functions 9. Exceptions 10. Files, Databases, Networks, and Contexts 11. Class Definitions 12. Scripts, Modules, Packages, Libraries, and Applications 13. Metaprogramming and Decorators 14. Fit and Finish – Unit Testing, Packaging, and Documentation 15. Next Steps Index

Using the unittest library for testing

For more complex testing, the doctest examples may not provide enough depth or flexibility. A docstring with a large number of cases would become too long to be effective as documentation. A docstring with complex test setup, teardown, or mock objects may not be useful as documentation either.

For these cases, we'll use the unittest module to define test cases and test suites. When using unittest, we'll generally create separate modules. These test modules will contain TestCase classes that contain test methods.

Here's a quick overview of a typical test case class definition:

import unittest

from Chapter_7.ch07_ex1 import FtoC

class Test_FtoC(unittest.TestCase):
    def setUp(self):
        self.temps= [50, 60, 72]
    def test_single(self):
        self.assertAlmostEqual(0.0, FtoC(32))
        self.assertAlmostEqual(100.0, FtoC(212))
    def test_map(self):
        temps_c = list(map(FtoC, self.temps))
        self.assertEqual(3, len...
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 $19.99/month. Cancel anytime
Banner background image