v Test If Output Is Close To A Value - Python

Test If Output Is Close To A Value

Interesting in learning more? Here are some good books on unit testing in Python: Python Testing: Beginner's Guide and Python Testing Cookbook.


import unittest
import sys

Create Function To Be Tested

def add(x, y):
    return x + y

Create Test

# 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)

Run Test

# Run the unit test (and don't shut down the Jupyter Notebook)
unittest.main(argv=['ignored', '-v'], exit=False)
test_add_two_floats_roughly_equals_11 (__main__.TestAdd) ... ok

Ran 1 test in 0.001s


<unittest.main.TestProgram at 0x1049191d0>