Python is throwing an error ‘Empty suite’ when I test my code. The strange thing is when I run
JavaScript
x
13
13
1
#this code on source code file
2
def func(num):
3
return num
4
5
# test code file
6
from unittest import TestCase, main
7
from Source_code import isPinValid, withdrawMoney, func
8
class funcTest(TestCase):
9
def test(self):
10
expected = 'string'
11
result = func('string')
12
self.assertEqual(expected, result)
13
Then this will work, however when I run the actual test for my code
JavaScript
1
56
56
1
# Test file
2
from unittest import TestCase, main
3
from Source_code import isPinValid, withdrawMoney
4
#class funcTest(TestCase):
5
# def test(self):
6
# expected = 'string'
7
# result = func('string')
8
# self.assertEqual(expected, result)
9
10
class testIsPinValid(TestCase):
11
def numLessthan999(self):
12
expected = 'pin invalid'
13
result = isPinValid(123)
14
self.assertEqual(expected, result)
15
16
class testWithdrawMoney(TestCase):
17
def withdrawOverBalance(self):
18
expected = '❌ Balance is not enough '
19
result = withdrawMoney(150)
20
self.assertEqual(expected, result)
21
22
if __name__ == '__main__':
23
main()
24
25
# Source code
26
def isPinValid(pin):
27
attemptsCount = 2
28
if pin == correctPin:
29
print('Pin number correct ✅')
30
withdrawMoney()
31
elif pin < 999 or pin > 9999:
32
return 'pin invalid'
33
else:
34
while (attemptsCount):
35
# try:
36
print('❌ Wrong pin number, you have {} more times to attempt'.format(attemptsCount))
37
userPin = int(input('💁 Please input your 4 digits pin code '))
38
attemptsCount -= 1
39
if isPinValid(userPin) == 'true':
40
withdrawMoney()
41
break
42
if attemptsCount == 0:
43
print('You have attempted wrong password 3 times, please visit the nearest branch 😢')
44
45
def withdrawMoney(withdrawalAmount):
46
balance = 100
47
# withdrawalAmount = int(input('💁 Please input your withdrawal amount '))
48
try:
49
if balance >= withdrawalAmount:
50
balance -= withdrawalAmount
51
print('Your balance after withdrawal is', balance, '💵')
52
else:
53
raise TypeError
54
except TypeError:
55
print('❌ Balance is not enough ')
56
This will throw me like :
JavaScript
1
8
1
Ran 0 tests in 0.000s
2
3
OK
4
5
Process finished with exit code 0
6
7
Empty suite
8
I guess it’s not an issue with unittest but only my code. Source code itself is working fine when I run it separately.. I don’t understand why it isn’t testing the actual code.. Any idea? please share..
Advertisement
Answer
Your method should start with ‘test’: https://docs.python.org/3/library/unittest.html#unittest-test-discovery
testMethodPrefix
String giving the prefix of method names which will be interpreted as test methods. The default value is ‘test’.
This affects getTestCaseNames() and all the loadTestsFrom*() methods.