[英]How to pass variables from setup to tests in nosetest?
使用python和nosetest,我有以下设置:
- package
- __init__.py
- test1.py
- test2.py
__init__.py
模块包含一个设置功能
def setup():
print("Setup called")
var = 42
稍后将使用它来创建唯一的标识(运行测试之间有所不同,但包内的所有测试都相同)。
测试本身如何访问此变量(在本例中为var
)? 测试脚本只是一些存根:
from nose.tools import assert_true
class TestSuite(object):
def test1(self):
# How to get content of 'var' here?
assert_true(True)
有一些pythonic的方法可以做到这一点,还是只使用环境变量来做到这一点?
在类内部调用.setup()
方法:
class Test:
def setup(self):
self.var = 1
def test_print_var(self):
print(self.var)
这也适用于从其他地方继承的方法:
class TestBase:
def setup(self):
self.var = 1
class Test(TestBase):
def test_print_var(self):
print(self.var)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.