我有一个模块导入unittest并有一些TestCases.我想接受一些命令行选项(例如下面,数据文件的名称),但是当我尝试传递选项时,我收到消息"选项-i not recognized".是否可以让unittest +为应用程序提供选项(注意:我使用optparse来处理选项)?谢谢.
$ python test_app_data.py -i data_1.txt
option -i not recognized
Run Code Online (Sandbox Code Playgroud)
=====================
后续行动:这是建议解决方案的实施:
import cfg_master #has the optparse option-handling code
...
if __name__ == '__main__':
#add you app's options here...
options_tpl = ('-i', '--in_dir', '-o', '--out_dir')
del_lst = []
for i,option in enumerate(sys.argv):
if option in options_tpl:
del_lst.append(i)
del_lst.append(i+1)
del_lst.reverse()
for i in del_lst:
del sys.argv[i]
unittest.main()
Run Code Online (Sandbox Code Playgroud) 有没有办法从给定的测试中将参数传递给 setUp() 方法,或者用其他一些方法来模拟这个?例如,
import unittest
class MyTests(unittest.TestCase):
def setUp(self, my_arg):
# use the value of my_arg in some way
def test_1(self):
# somehow have setUp use my_arg='foo'
# do the test
def test_2(self):
# somehow have setUp use my_arg='bar'
# do the test
Run Code Online (Sandbox Code Playgroud)