相关疑难解决方法(0)

从装饰师访问自己

在unittest的setUp()方法中,我设置了一些变量,稍后在实际测试中引用.我还创建了一个装饰器来做一些日志记录.有没有办法可以从装饰器访问这些变量?

为简单起见,我发布此代码:

def decorator(func):
    def _decorator(*args, **kwargs):
        # access a from TestSample
        func(*args, **kwargs)
    return _decorator

class TestSample(unittest.TestCase):    
    def setUp(self):
        self.a = 10

    def tearDown(self):
        # tear down code

    @decorator
    def test_a(self):
        # testing code goes here
Run Code Online (Sandbox Code Playgroud)

什么是访问的最好办法一个从装饰(()在设置中设定)?

python unit-testing scope

68
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×1

scope ×1

unit-testing ×1