Title: Test If Output Is Close To A Value
Slug: test_if_an_output_is_close_to_a_value
Summary: Test if an output is close to a value in Python.
Date: 2016-01-23 12:00
Category: Python
Tags: Testing
Authors: Chris Albon
Interesting in learning more? Here are some good books on unit testing in Python: Python Testing: Beginner's Guide and Python Testing Cookbook.
In [1]:
import unittest
import sys
In [2]:
def add(x, y):
return x + y
In [3]:
# Create a test case
class TestAdd(unittest.TestCase):
# Create the unit test
def test_add_two_floats_roughly_equals_11(self):
# Test if add(4.48293848, 6.5023845) return roughly (to 1 place) 11 (actual product: 10.98532298)
self.assertAlmostEqual(11, add(4.48293848, 6.5023845), places=1)
In [4]:
# Run the unit test (and don't shut down the Jupyter Notebook)
unittest.main(argv=['ignored', '-v'], exit=False)
Out[4]: