[英]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.