【发布时间】:2009-06-22 23:23:05
【问题描述】:
我有一个导入 unittest 并有一些 TestCases 的模块。我想
接受一些命令行选项(例如下面的数据文件的名称),
但是当我尝试传递选项时,我收到消息option -i not recognized。是否可以让unittest + 为应用程序提供选项(注意:我使用optparse 来处理选项)?谢谢。
$ python test_app_data.py -i data_1.txt
option -i not recognized
======================
跟进:这是建议解决方案的实施:
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()
【问题讨论】:
-
一般来说,是的。在这种情况下,答案似乎很大程度上取决于您没有提供的细节。
-
@jd。您的“跟进”应作为答案发布 - 您的问题应仅包含......好吧......问题。
标签: python unit-testing