我正在使用鼻子测试进行 Python 单元测试,以试验Python 类和模块固定装置,以便在我的测试中进行最少的设置。
问题是我不确定如何使用在我的测试中定义的任何变量和函数(例如setupUpModule
:)。setUpClass
test_1
这是我用来尝试的:
import unittest
def setUpModule():
a = "Setup Module variable"
print "Setup Module"
def tearDownModule():
print "Closing Module"
class TrialTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print a #<======
b = "Setup Class variable"
@classmethod
def tearDownClass(cls):
print "Closing Setup Class"
def test_1(self):
print "in test 1"
print a #<======
print b #<======
def test_2(self):
print "in test 2"
def test_3(self):
print "in test 3"
def test_4(self):
print "in test 4"
def test_5(self):
print "in test 5"
if __name__ == "__main__":
unittest.main()
我得到的错误是:
Setup Module
ERROR
Closing Module
======================================================================
ERROR: test suite for <class 'one_setup.TrialTest'>
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/nose/suite.py", line 208, in run
self.setUp()
File "/Library/Python/2.7/site-packages/nose/suite.py", line 291, in setUp
self.setupContext(ancestor)
File "/Library/Python/2.7/site-packages/nose/suite.py", line 314, in setupContext
try_run(context, names)
File "/Library/Python/2.7/site-packages/nose/util.py", line 469, in try_run
return func()
File "/Users/patila14/Desktop/experimental short scripts/one_setup.py", line 13, in setUpClass
print a
NameError: global name 'a' is not defined
----------------------------------------------------------------------
当然,如果我这样做gloabl a
,global b
它会起作用。有没有更好的办法?