I am creating unit tests by using the unittest module, and simply asserting Equal a result that is correct when calculating the Fibonacci sequence. Below is the working code- the 5th number is 2+3 = 5.
num1 = 0
num2 = 1
find = 2
fib_num = 0
while find <= N:
fib_num = num1 + num2
num1 = num2
num2 = fib_num
find = find + 1
return(fib_num)
print(fibFind(5))
This is mu Unittest
from fibfind import fibFind
class TestFibonnaciResult(unittest.TestCase):
def test_number(self):
self.assertEqual(fibFind(5)== 5),
self.assertEqual(fibFind(6)==8),
self.assertEqual(fibFind(7)== 13)
5, 8, 13
def test_input_type(self):
self.assertTrue(fibFind("some-string") == error)
if __name__ == "__main__":
unittest.main()
In the terminal however, I get this?
$ python -m unittest test_fibFind.py
EE
======================================================================
ERROR: test_input_type (test_fibFind.TestFibonnaciResult)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\XYZ\dev\repos\testing_python\test_fibFind.py", line 14, in test_input_type
self.assertTrue(fibFind("some-string") == error)
File "C:\Users\XYZ\dev\repos\testing_python\fibfind.py", line 7, in fibFind
while find <= N:
TypeError: '<=' not supported between instances of 'int' and 'str'
======================================================================
ERROR: test_number (test_fibFind.TestFibonnaciResult)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\XYZ\dev\repos\testing_python\test_fibFind.py", line 8, in test_number
self.assertEqual(fibFind(5)== 5),
TypeError: assertEqual() missing 1 required positional argument: 'second'
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (errors=2)
5
The second test is wanting to make sure only an integer is parsed through but clearly is having issues with the comparator <=
Any help here before I move onto pytest would be great.
Other answers are mostly correct, but none touches on the other test that expects an exception to be raised.
unittest
assert...
methods have very specific signatures, which you should check before using. In the first test, you basicly did the following:
self.assertTrue(True)
Doesn't make much sense, right? Instead, you should pass 2 objects as a first
and second
arguments, and let unittest handle their comparison.
self.assertEqual(fibFind(5), 5)
Similiarly, with a bad input test you can't simply call a function with bad arguments and compare to undefined error
.
There exists assertRaises
method exactly for that purpose. It accepts an exception type, callable and arguments that will be passed to the callable. Which means you should call it like that:
self.assertRaises(TypeError, fibFind, "some-string")
All in all, your test class should look like that:
class TestFibonnaciResult(unittest.TestCase):
def test_number(self):
self.assertEqual(fibFind(5), 5)
self.assertEqual(fibFind(6), 8)
self.assertEqual(fibFind(7), 13)
def test_input_type(self):
self.assertRaises(TypeError, fibFind, "some-string")
Output:
5
..
----------------------------------------------------------------------
Ran 2 tests in 0.024s
OK