【问题标题】:TestCase fails with discover -s but running one-by-one shows OKTestCase 因发现 -s 失败,但逐个运行显示正常
【发布时间】:2018-12-19 08:48:49
【问题描述】:

unit_tests 文件夹下有多个文件。单独运行TestCase 显示测试成功。如果我用

运行它们
python -m unittest discover -s unit_tests/*

引发错误。看起来test_* 方法正在并行运行。

运行基于TestCase 的类显示所有测试都正常而同时运行所有TestCases 失败的原因可能是什么。

状态更新

发现问题。当我为我的类使用元编程时,我没有编写自定义__del__ 实现来清除与该类相关的所有对象。因此 Python 保存了对对象的引用,并且下次初始化类实例时,它会记住以前的实例值。

【问题讨论】:

    标签: python python-3.x unit-testing testing


    【解决方案1】:

    找到原因并写下状态更新。

    我的课是这样的:

    class MyClass:
    
        items = []
    
        def append(self, item):
            self.items.append(item)
    

    当我将 obj 附加到 MyClass,然后在 items 中删除带有 obj 的实例时,它在创建下一个实例时仍然存在。

    class MyClass:
    
        items = []
    
        def append(self, item):
            self.items.append(item)
    
        def __del__(self):
            [self.items.remove(order) for order in self.items]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多