【发布时间】:2011-11-27 06:54:46
【问题描述】:
在 unittest 的 setUp() 方法中,我设置了一些 self 变量,稍后在实际测试中会引用这些变量。我还创建了一个装饰器来做一些日志记录。有没有一种方法可以让我从装饰器中访问那些 self 变量?
为简单起见,我发布此代码:
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
从装饰器访问 a(在 setUp() 中设置)的最佳方式是什么?
【问题讨论】:
标签: python unit-testing scope