当我测试代码时,Python抛出了一个错误“Empty suite”。 奇怪的是当我跑的时候
#this code on source code file
def func(num):
return num
# test code file
from unittest import TestCase, main
from Source_code import isPinValid, withdrawMoney, func
class funcTest(TestCase):
def test(self):
expected = 'string'
result = func('string')
self.assertEqual(expected, result)
然而,当我运行代码的实际测试时,这将起作用
# Test file
from unittest import TestCase, main
from Source_code import isPinValid, withdrawMoney
#class funcTest(TestCase):
# def test(self):
# expected = 'string'
# result = func('string')
# self.assertEqual(expected, result)
class testIsPinValid(TestCase):
def numLessthan999(self):
expected = 'pin invalid'
result = isPinValid(123)
self.assertEqual(expected, result)
class testWithdrawMoney(TestCase):
def withdrawOverBalance(self):
expected = '❌ Balance is not enough '
result = withdrawMoney(150)
self.assertEqual(expected, result)
if __name__ == '__main__':
main()
# Source code
def isPinValid(pin):
attemptsCount = 2
if pin == correctPin:
print('Pin number correct ✅')
withdrawMoney()
elif pin < 999 or pin > 9999:
return 'pin invalid'
else:
while (attemptsCount):
# try:
print('❌ Wrong pin number, you have {} more times to attempt'.format(attemptsCount))
userPin = int(input('💁 Please input your 4 digits pin code '))
attemptsCount -= 1
if isPinValid(userPin) == 'true':
withdrawMoney()
break
if attemptsCount == 0:
print('You have attempted wrong password 3 times, please visit the nearest branch 😢')
def withdrawMoney(withdrawalAmount):
balance = 100
# withdrawalAmount = int(input('💁 Please input your withdrawal amount '))
try:
if balance >= withdrawalAmount:
balance -= withdrawalAmount
print('Your balance after withdrawal is', balance, '💵')
else:
raise TypeError
except TypeError:
print('❌ Balance is not enough ')
这会让我觉得:
Ran 0 tests in 0.000s
OK
Process finished with exit code 0
Empty suite
我想这不是unittest的问题,而是我的代码。 当我单独运行时,源代码本身运行良好。。 我不明白为什么它不测试实际的代码。。有什么想法吗?请分享
您的方法应以“测试”开头: https://docs.python.org/3/library/unittest.html#unittest-test-discovery
testMethodPrefix
字符串,给出将被解释为测试方法的方法名称的前缀。默认值为“test”
这会影响getTestCaseNames()和所有loadTestsFrom*()方法
相关问题 更多 >
编程相关推荐