/
test_fibonacci.py
75 lines (60 loc) · 2.11 KB
/
test_fibonacci.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
# Name: Max Radke Date: Febuary 25, 2021
# College: Oregon State University
# Class: CS 362 Assignment: Activity unittest, pytest
# Description: Tests file "fibonacci.py"
#
# How to run: Open the directory that this file is in with command prompt.
# Make sure that fibonacci.py is in the same directory.
# Type "python ./test_fibonacci.py"
import unittest
import fibonacci
class Testfib(unittest.TestCase):
# !! Start by testing the .fib function !!
# Test the first 20 numbers of the fibonacci sequence
def test1(self):
fibnumbers = [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181]
result = True
i = 0
for x in fibnumbers:
if fibonacci.fib(i) != x:
result = False
i = i + 1
self.assertTrue(result)
# Anything other than ints should throw TypeError
def test2(self):
with self.assertRaises(TypeError):
fibonacci.fib("Test")
# Negative numbers should throw ValueError
def test3(self):
with self.assertRaises(ValueError):
fibonacci.fib(-1)
# Floats should through type error
def test4(self):
with self.assertRaises(TypeError):
fibonacci.fib(0.5)
# !! End by testing the .fact function !!
# Test the first 10 factorial numbers
def test5(self):
factnumbers = [1,1,2,6,24,120,720,5040,40320,362880,3628800]
result = True
i = 0
for x in factnumbers:
if fibonacci.fact(i) != x:
result = False
i = i + 1
self.assertTrue(result)
# Anything other than ints should throw TypeError
def test6(self):
with self.assertRaises(TypeError):
fibonacci.fact("Test")
# Negative numbers should through ValueError
def test7(self):
with self.assertRaises(ValueError):
fibonacci.fact(-1)
# Floats should throw type error
def test8(self):
with self.assertRaises(TypeError):
fibonacci.fact(0.5)
# call the tests
if __name__ == "__main__":
unittest.main(verbosity=2)