我是新来的Python和单位test.following是主要的单元测试的程序来调用作用一个测试用例在单元测试中调用单独的Python程序
import unittest
from test import test_support
class MyTestCase1(unittest.TestCase):
def test_feature_one(self):
print "testing feature one"
execfile("/root/test/add.py")
def test_main():
test_support.run_unittest(MyTestCase1);
if __name__ == '__main__':
test_main()
add.py其他的Python程序是基本的程序,增加了两个不,并显示它。
#!/usr/bin/env python
import sys
def disp(r):
print r
def add():
res = 3+5;
disp(res)
add()
但是当我从另一个函数调用某个函数时出现问题。当我尝试运行单元测试(第一个程序)时,我遇到以下错误。但是,如果我在单元测试套装之外运行add.py作为单个程序,它工作正常。好心需要理解帮助这种情况下
======================================================================
ERROR: test_feature_one (__main__.MyTestCase1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "first.py", line 17, in test_feature_one
execfile("/root/test/add.py")
File "/root/test/add.py", line 12, in <module>
add()
File "/root/test/add.py", line 10, in add
disp(res)
NameError: global name 'disp' is not defined
----------------------------------------------------------------------