小编Emo*_*ons的帖子

用于 Python 单元测试的单例析构函数

我需要在 Python (2.7) 中实现单例模式并使用单元测试覆盖代码。

下面是我使用的代码:

class Singleton(object):
    def __new__(cls):
        if not hasattr(cls, 'instance'):
             cls.instance = super(Singleton, cls).__new__(cls)
        return cls.instance
Run Code Online (Sandbox Code Playgroud)

为了使单元测试独立,我需要为此类实现一个析构函数。如何才能做到这一点?

python singleton unit-testing

4
推荐指数
1
解决办法
6643
查看次数

标签 统计

python ×1

singleton ×1

unit-testing ×1