unittest 调用姿势不对?

2018 年 5 月 11 日
 wsds

怎么会提示给类的实例 __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]

2542 次点击
所在节点    Python
9 条回复
ericls
2018 年 5 月 11 日
这个你得看 长辈的 __init__ 怎么写的

另外 你这个需求 用 setUp?
wsds
2018 年 5 月 11 日
@ericls 长辈的 __init__ 怎么写的 ===这句话怎么理解
changrui0608
2018 年 5 月 11 日
用 setUp 不要用__init__
wsds
2018 年 5 月 11 日
@changrui0608 不能用__init__还是咋地啊?
changrui0608
2018 年 5 月 11 日
@wsds 这种框架测试类的生命周期管理是框架自身来做的,就按文档说的来吧
ericls
2018 年 5 月 11 日
@wsds 就是长辈的 __init__ 你看看 mro 呗
nullcc
2018 年 5 月 11 日
用 setUp/tearDown,对你这个例子而言,如果非要写__init__,可以这样:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.a = 1
mec
2018 年 5 月 11 日
python 继承要考虑父类__init__的实现,必要时需要显式调用
wsds
2018 年 5 月 11 日
@mec @nullcc 弃用 init 了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/454027

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX