python - HTMLTestRunner报错:raise TypeError("{} is not callable".

 nuabolalalala5_760 发布于 2022-10-29 16:14

Python3 在使用HTMLTestRunner时,报错:raise TypeError("{} is not callable".format(repr(test)))

代码如下:

import pymysql
import unittest
import time
import unittest.suite
import HTMLTestRunner
import sys
def hell(a):
    print(a)
    return a


testunit = unittest.TestSuite()
testunit.addTest(hell('ad'))

filename = '/Users/vivi/Downloads/aa.html'
fp = open(filename, 'wb')  # 之前写的是file(filename,'wb'),但是无法识别file方法,改成open,OK!
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'print', description=u'简单')
runner.run(testunit)

运行后报错:

Traceback (most recent call last):
  File "/Applications/Python 3.5/……/B.py", line 30, in 
    testunit.addTest(hell('ad'))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/suite.py", line 47, in addTest
    raise TypeError("{} is not callable".format(repr(test)))
TypeError: 'ad' is not callable
1 个回答
  • unittest.TestSuite 实例的 addTest() 方法使用不正确,传参有误,建议好好看看unittest模块的文档。

    给出一个代码示例:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import unittest
    import HTMLTestRunner
    
    
    def hell(a):
        print(a)
        return a
    
    
    class HellTest(unittest.TestCase):
        def setUp(self):
            self.hell = hell
    
        def tearDown(self):
            pass
    
        def testHell(self):
            self.assertEqual(self.hell('ad'), 'ad')
    
    
    if __name__ == '__main__':
        testunit = unittest.TestSuite()
        testunit.addTest(HellTest('testHell'))
    
        filename = 'D:\\aa.html'
        fp = open(filename, 'wb')
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'print', description=u'简单')
        runner.run(testunit)
        fp.close()
    

    2022-10-31 18:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有