forked from DrSkippy/Data-Science-45min-Intros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_mathy.py
executable file
·81 lines (70 loc) · 2.63 KB
/
test_mathy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__="Josh Montague"
__license__="MIT License"
import mathy
import random
import string
import unittest
class Test_mathy(unittest.TestCase):
"""
Test module for the practice mathy.py module
"""
def setUp(self):
"""
Create an instance of the Calcs class for use with all test methods
"""
# assign some instance vars so we can use them in subsequent tests
# in lieu of global vars
self.f = random.normalvariate(0, 1) # random float
self.i = int( random.uniform(0, 10) ) # random int
self.s = random.choice( [x for x in string.lowercase] ) # random string char
# create a new object of type Calcs for testing
self.calcs = mathy.Calcs() # use this object for the tests
def tearDown(self):
"""
Clean up anything e.g. database connections that needs to be taken care of
after each test
"""
pass
def test_init(self):
"""
Test that the constructor is behaving as expected
"""
self.assertIsInstance(self.calcs, mathy.Calcs)
self.assertIsInstance(self.calcs.zero, int)
self.assertEqual(self.calcs.zero, 0)
def test_add_one(self):
"""
Test that the add_one method is behaving as expected
"""
# floats and ints should be happy
# check the value
self.assertEqual(self.calcs.add_one(self.f), self.f + 1)
self.assertEqual(self.calcs.add_one(self.i), self.i + 1)
# check the type
self.assertIsInstance(self.calcs.add_one(self.i), int)
self.assertIsInstance(self.calcs.add_one(self.f), float)
# check the exception
self.assertIsInstance(self.calcs.add_one(self.s), str)
def test_square(self):
"""
Test that the square method is behaving as expected
"""
# check the values
self.assertEqual(self.calcs.square(self.f), self.f**2)
self.assertEqual(self.calcs.square(self.i), self.i**2)
# check the type
self.assertIsInstance(self.calcs.square(self.f), float)
self.assertIsInstance(self.calcs.square(self.i), int)
# check the exception
#
# because of the way the expression is evaluated,
# there are two ways to check the exception handling...
# ==> lambda fnc
self.assertRaises(TypeError, lambda: self.calcs.square(self.s))
# ==> contextmanager
with self.assertRaises(TypeError):
self.calcs.square(self.s)
if __name__ == '__main__':
unittest.main()