简体   繁体   中英

How do I fix UNITTEST ERROR in python with 'object is not callable'?

#calculater.py

class Calculator1:
    
    def __init__(self):
        self.result = 0

    def add(self, num):
        self.result += num
        return self.result
#calculater_test.py

import unittest
from calculater import Calculator1

cal1 = Calculator1()

class Calculator1Test(unittest.TestCase):

    def test_Calculator1(self):
        print(cal1.add(3))
        self.assertEqual(3, cal1())

if __name__ == '__main__':  
    unittest.main()

Error code is 'TypeError: 'Calculator1' object is not callable'. However, it works fine when I clear the 12th line 'self.assertEqual(3, cal1())' I wrote. However, if erase it, i will see a red dot on the bottom as soon as erase it. what's that meaning of a red dot on the bottom?

For the main purpose If I clear 'self.assertEqual(3, cal1()), I can't change the value to make an error on purpose, so I want to know how do I fix it, how do I change the value to make an error. Finally How to fix errors in 'Calculator1' object is not callable'.

I wrote it with a translator because I am not good at English. sorry.

def test_Calculator1(self):
        print(cal1.add(3))
        self.assertEqual(3, cal1()) # <--- here

You call ' call1() ', but call1 is a Calculator1 instance and not a method. You want to check the result of the ' add ' method. I think you meant to do:

def test_Calculator1(self):
        result = cal1.add(3)
        print(result)
        self.assertEqual(3, result)

And better yet is to not use print in UnitTest. Your UnitTest should be small. self.assertEqual(3, cal1.add(3))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM