Skip to content
Advertisement

Unittest is throwing an error Empty suite

Python is throwing an error ‘Empty suite’ when I test my code. The strange thing is when I run

#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)

Then this will work, however when I run the actual test for my code

# 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 ')

This will throw me like :

Ran 0 tests in 0.000s

OK

Process finished with exit code 0

Empty suite

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.

User contributions licensed under: CC BY-SA
13 People found this is helpful
Advertisement