python - 在 python 单元测试中初始化记录器

标签 python unit-testing

import logging

class TestMyClass(unittest.TestCase):
       def __init__(self):
            self.logger = logging.getLogger('MyClassLog')

       def setUp(self):

我正在尝试在构造函数中实例化记录器。但我收到此错误: ... TypeError: init() 恰好接受 1 个参数(给定 2 个)

这是为什么?如何正确实例化记录器?

最佳答案

您正在覆盖 __init__。你不能在这样的 TestCase 中这样做,因为你的测试用例将由测试运行器实例化,并且传递给它的参数将是要运行的方法,所以您已经取消了所有需要完成的初始化(并且没有采用正确数量的参数)。

如果你想做一些日志记录,你可以在 setUp 中做方法,或者更可能是全局方法。

关于python - 在 python 单元测试中初始化记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11750437/

相关文章:

python - Syncano v4 错误 : "You do not have permission to perform this action"

c# - 我将如何对这个 .Net 代码进行单元测试(允许重构)

android - Robolectric:Resources$NotFoundException:带有 Android Gradle 插件 3 的字符串资源 ID

python - 应用服务器的单元测试

python - pandas read_sql 在列名中删除点

python - 如何避免属性错误: 'NoneType' object has no attribute 'text' webscraping subreddits?

python - list vs UserList 和 dict vs UserDict

python - URL 在我的 Django 项目中传递参数

c++ - gtest 与柯南一起安装 : undefined reference to `testing::internal::GetBoolAssertionFailureMessage`

c# - Leetcode测试链表