2014-10-29 78 views
-1

我有一些在CLI中使用配置文件運行的python腳本。測試腳本的所有內容都在「ABC」類中。以獨立腳本和鼻子測試的方式運行python腳本

在ABC類的init中,我傳遞了在腳本中使用的配置文件。

所以,當我跑我的Python腳本獨立的,我會說「蟒蛇script_one.py -c的config.cfg」

現在的問題是,我需要運行使用nosetests目錄中的所有蟒蛇測試。鼻子測試庫不喜歡這樣的事實,即我在ABC類的init()中傳遞「config」。

我需要一種運行python的方式,如上面提到的那樣用config和nosetests。

我切換到在Python configparser模塊用來,它解析配置文件和我沒有給他們傳遞給INIT()

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

配置文件在()被引用:

if __name__ == '__main__': 

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

如果配置= c.parse_config()測試= TESTME(配置)已被使用,那麼我將不得不通過我的配置文件(xyz.cfg)作爲「script_1.py -c xyz.cfg) - 有沒有一種方法來利用鼻測試?我問這個,因爲鼻子測試不會讓我通過「配置」init()的TestMe類。

的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 

有沒有一種方法,我可以運行我的劇本與配置,並與nosetests獨立?

回答

0

nose-testconfig解決了這一切!非常便利!