将python脚本作为standlone脚本运行,也作为nosetests运行

时间:2014-10-29 23:04:07

标签: testing scripting config combinations nose

我有python脚本,它们在CLI上使用配置文件运行。测试脚本的所有内容都在课堂内" ABC"。

在ABC类的 init 中,我传递了在脚本中使用的配置文件。

因此,当我独立运行我的python脚本时,我会说" python script_one.py -c config.cfg"

现在问题是我需要使用nosetests运行目录中的所有python测试。鼻子测试库不喜欢我传递的事实" config"在ABC类的 init ()中。

我需要一种运行python的方法,如上所述,使用config和nosetests。

我在Python中切换到ConfigParser模块,它解析配置文件,我不必将它们传递给 init ()

class TestMe(object):
    def __init__(self):
        config = ConfigParser.ConfigParser()
        config.read(file)

配置文件在 main ()中引用:

if __name__ == '__main__':

    #config = c.parse_config()
    #test = TestMe(config)
    file = "configs/test_config.yaml"
    print "inside main..."
    config = ConfigParser.ConfigParser()
    config.read(file)

如果 config = c.parse_config()并且 test = TestMe(config)已被使用,那么我将不得不传递我的配置文件(xyz.cfg) as" script_1.py -c xyz.cfg) - 有没有办法在鼻子测试中利用它?我问这个是因为鼻子测试不会让我通过" config"到TestMe类的 init ()。

当我执行时,config.read(文件)会导致错误" nosetests script_1.py"

E
======================================================================
ERROR: Failure: TypeError ('type' object is not iterable)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 519, in makeTest
    return self._makeTest(obj, parent)
  File "/usr/local/lib/python2.7/site-packages/nose/loader.py", line 578, in _makeTest
    return MethodTestCase(obj)
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 345, in __init__
    self.inst = self.cls()
  File "/home/..../script_1.py", line 30, in __init__
    config.read(file)
  File "/usr/local/lib/python2.7/ConfigParser.py", line 300, in read
    for filename in filenames:
TypeError: 'type' object is not iterable

有没有办法可以使用config和nosetests独立运行我的脚本?

1 个答案:

答案 0 :(得分:0)

nose-testconfig解决了这一切!非常方便!