相关疑难解决方法(0)

python,unittest:有没有办法将命令行选项传递给应用程序

我有一个模块导入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)

python unit-testing

45
推荐指数
2
解决办法
3万
查看次数

任何将参数从测试传递到 Python 单元测试测试的 setUp() 方法的方法?

有没有办法从给定的测试中将参数传递给 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)

python arguments python-unittest

6
推荐指数
1
解决办法
2729
查看次数

标签 统计

python ×2

arguments ×1

python-unittest ×1

unit-testing ×1