V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsds
V2EX  ›  Python

unittest 调用姿势不对?

  •  
  •   wsds · 2018-05-11 12:44:48 +08:00 · 1632 次点击
    这是一个创建于 2148 天前的主题,其中的信息可能已经有所发展或是发生改变。

    怎么会提示给类的实例 __init__ 传递了 2 个参数?why 啊?

    import unittest
    
    
    class Test(unittest.TestCase):
        def __init__(self):
            self.a = 1
    
        def test1(self):
            self.assertEqual(self.a, 1)
    
    
    if __name__ == '__main__':
        unittest.main()
    
    
    

    output:

    ....省略没用的
    TypeError: __init__() takes 1 positional argument but 2 were given
    [Finished in 0.2s with exit code 1]
    
    
    9 条回复    2018-05-11 16:10:02 +08:00
    ericls
        1
    ericls  
       2018-05-11 12:46:16 +08:00 via iPhone
    这个你得看 长辈的 __init__ 怎么写的

    另外 你这个需求 用 setUp?
    wsds
        2
    wsds  
    OP
       2018-05-11 12:55:11 +08:00
    @ericls 长辈的 __init__ 怎么写的 ===这句话怎么理解
    changrui0608
        3
    changrui0608  
       2018-05-11 12:55:46 +08:00 via Android
    用 setUp 不要用__init__
    wsds
        4
    wsds  
    OP
       2018-05-11 13:03:51 +08:00
    @changrui0608 不能用__init__还是咋地啊?
    changrui0608
        5
    changrui0608  
       2018-05-11 13:06:26 +08:00 via Android
    @wsds 这种框架测试类的生命周期管理是框架自身来做的,就按文档说的来吧
    ericls
        6
    ericls  
       2018-05-11 13:10:00 +08:00 via iPhone
    @wsds 就是长辈的 __init__ 你看看 mro 呗
    nullcc
        7
    nullcc  
       2018-05-11 13:27:35 +08:00
    用 setUp/tearDown,对你这个例子而言,如果非要写__init__,可以这样:
    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.a = 1
    mec
        8
    mec  
       2018-05-11 13:41:16 +08:00
    python 继承要考虑父类__init__的实现,必要时需要显式调用
    wsds
        9
    wsds  
    OP
       2018-05-11 16:10:02 +08:00
    @mec @nullcc 弃用 init 了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1012 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:41 · PVG 03:41 · LAX 12:41 · JFK 15:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.