我正在尝试运行以下脚本:
import unittest
class RandomDataGeneratorTest(unittest.TestCase):
def setUp(self):
import numpy
def test_numpy_is_imported(self):
pi = numpy.pi
if __name__ == '__main__':
unittest.main()
但是,我收到以下错误/失败:
E
======================================================================
ERROR: test_numpy_is_imported (__main__.RandomDataGeneratorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kurt/dev/clones/ipercron-compose/controller/random_data_tester.py", line 9, in test_numpy_is_imported
pi = numpy.pi
NameError: global name 'numpy' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.100s
FAILED (errors=1)
据我所知https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUp,setUp()
函数应该在任何test
函数之前运行,所以我不明白为什么会出现这个错误?
答案 0 :(得分:2)
您正在函数中导入,因此导入的名称仅存在于那里。
尝试设置内容,例如Uncaught TypeError: yearRingChart.width(...).height(...).dimension(...).group(...).innerRadius(...).controlsUseVisibility is not a function
,并断言它是在测试方法中设置的。