25
loading...
This website collects cookies to deliver better user experience
count()
function to do that:# countkey.py
def count(k,l):
# count occurrences of k in l
num = 0
for i in l:
if i == k:
num += 1
return num # return the count
1
, [1,2,3,1]
2
1
appears in the array twice, the expected return value is 2
.unittest
that automates unit test cases. Given the countkey.py
program that we are testing, here is another program called tester.py
that automates the unit test:# tester.py
import countkey # the program to test
import unittest # Python test framework
class TesterClass(unittest.TestCase):
# test cases
def test1(self):
self.assertEqual(countkey.count(1,[1,2,3,1]), 2)
if __name__ == '__main__':
unittest.main()
test1()
method. This method asserts that the count()
function returns the value 2
, when given the input parameters: 1
, [1,2,3,1]
. When you execute the tester.py
program, the output is:$ python tester.py
.
---------------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$
None
, and it is in the arrayNone
, and it is not in the arrayNone
instead of an arraytester.py
program that automates all of the above unit tests:# tester.py
import countkey # the program to test
import unittest # Python test framework
class TesterClass(unittest.TestCase):
# test cases
def test1(self):
# find positive key
self.assertEqual(countkey.count(6,[6,7,8]), 1)
def test2(self):
# find negative key
self.assertEqual(countkey.count(-7,[6,-7,8]), 1)
def test3(self):
# key is found at the beginning
self.assertEqual(countkey.count(6,[6,7,8]), 1)
def test4(self):
# key is found in the middle
self.assertEqual(countkey.count(7,[6,7,8]), 1)
def test5(self):
# key is found at the end
self.assertEqual(countkey.count(8,[6,7,8]), 1)
def test6(self):
# multiple keys found
self.assertEqual(countkey.count(1,[1,6,1,7,8,1]), 3)
def test7(self):
# key is not found
self.assertEqual(countkey.count(9,[1,2,3,1]), 0)
def test8(self):
# empty array
self.assertEqual(countkey.count(3,[]), 0)
def test9(self):
# Find None when it is there
self.assertEqual(countkey.count(None,[1,2,None,3]), 1)
def test10(self):
# Find None when it is not there
self.assertEqual(countkey.count(None,[1,2,3]), 0)
def test11(self):
# Find in None?
self.assertEqual(countkey.count(3,None), 0)
if __name__ == '__main__':
unittest.main()
tester.py
, the output is:$ python tester.py
..E........
======================================================================
ERROR: test11 (__main__.TesterClass)
---------------------------------------------------------------------------
Traceback (most recent call last):
File "tester.py", line 51, in test11
self.assertEqual(countkey.count(3,None), 0)
File "/Users/user/Desktop/countkey.py", line 5, in count
for i in l:
TypeError: 'NoneType' object is not iterable
---------------------------------------------------------------------------
Ran 11 tests in 0.001s
FAILED (errors=1)
$
test11()
failed. That is because the program does not properly handle the case when the count()
function tries to search None
instead of an array. We can modify countkey.py
to make it more robust:# countkey.py
def count(k,l):
# count occurrences of k in l
# NEW
if l == None:
return 0
num = 0
for i in l:
if i == k:
num += 1
return num # return the count
$ python tester.py
...........
---------------------------------------------------------------------------
Ran 11 tests in 0.000s
OK
$
countkey.py
whenever someone modifies it. All you have to do is execute the tester.py
program. That is why it is important to write good, comprehensive unit test cases for your programs. And remember to update your tester programs as needed.unittest
. This will improve your program and ensure that the program is correct, now and in the future. @realEdwinTorres
for more programming tips and help.