2010-09-08 46 views
4

我確實有幾個小模塊,裏面有測試,py.testnose不尋找它們,因爲它們的文件名中不包含test如何使py.test或nose在所有python文件中查找測試?

我該如何說服py.testnose以遞歸方式在所有python文件中查找測試 - '''包括文件名'''中沒有test的測試?

在源文件中,我保留標準命名約定:class testSomeName與方法def test_some_name

如果這是不可能的,我可以使用什麼其他解決方案來獲得相同的結果。

我不想手動創建包含測試的所有文件的列表,我想要一個支持發現的解決方案。

回答

5

你也可以看看Nose將發現測試,而無需使用一個固定的文件名約定。

您可以使用以下代碼繞過用於過濾鼻子中文件的正則表達式。 創建一個Python模塊(即my_nosetests.py

import nose 
from nose.plugins.base import Plugin 

class ExtensionPlugin(Plugin): 

    name = "ExtensionPlugin" 

    def options(self, parser, env): 
     Plugin.options(self,parser,env) 

    def configure(self, options, config): 
     Plugin.configure(self, options, config) 
     self.enabled = True 

    def wantFile(self, file): 
     return file.endswith('.py') 

    def wantDirectory(self,directory): 
     return True 

    def wantModule(self,file): 
     return True 


if __name__ == '__main__': 
    includeDirs = ["-w", ".", ".."] 
    nose.main(addplugins=[ExtensionPlugin()], argv=sys.argv.extend(includeDirs)) 

現在運行my_nosetests.py,如果你正在運行nosetests,你應該有你的測試運行。請注意,您實際上正在加載所有模塊並在其中搜索測試。注意模塊加載的任何副作用。

+0

鼻子是件好事! – dmitko 2010-09-08 18:26:40

+0

你可以給一個鼻子的解決方案,我會接受它! – sorin 2010-09-09 13:22:50

+0

使用鼻子在根目錄中使用nosetests或將目錄作爲參數傳遞。默認情況下它會遞歸。 – Rod 2010-09-09 13:49:58

1

documentation

默認情況下不以點開頭的所有目錄遍歷,尋找測試_ *。PY和* _test.py文件。這些Python文件在其包名下導入。

你能確定這是你的代碼的情況嗎?

更新

(買者自負:我還沒有試過/測試這一點)如何使用用於收集的目錄和文件的hooks

py.test要求收集的文件和目錄下面的兩個基本掛鉤:

def pytest_collect_directory(path, parent): 
    """ return Collection node or None for the given path. """ 

def pytest_collect_file(path, parent): 
    """ return Collection node or None for the given path. """ 

無論是對於一個給定的路徑返回collection node。所有鉤子實現的所有返回節點都將參與收集和運行協議。所述parent對象是父節點,並且可以被用於通過parent.config對象來訪問命令行選項。

+1

我做了RTFM和準確,這是問題。我想改變這種行爲,我有單位測試,但它們都嵌入在模塊文件中,並從__main__中調用。我相信在將來我會想要移動它們,但目前我只是想掃描它們,即使在不使用這種命名的文件中。 – sorin 2010-09-08 17:57:04

+1

@Sorin:更新了我的答案。往上看。 – 2010-09-08 18:17:19

6

隨着py.test這很簡單。與此內容創建一個conftest.py文件:

# content of conftest.py file at root of your project 
def pytest_collect_file(path, parent): 
    if path.ext == ".py": 
     return parent.Module(path, parent) 

這將延長征收過程爲每個「py」爲文件的測試「模塊」節點。把它放到conftest.py文件使它在具體項目的擴展,如果你鍵入它自動加載:

py.test 

作參考之用,也可鍵入:

py.test --collectonly 

,看看有什麼測試和文件收集,例如輸出:

<Directory 'morecollect'> 
    <Module 'conftest.py'> 
    <Directory 'pkg'> 
    <Module 'test_x.py'> 
     <Function 'test_hello2'> 
    <Module 'x.py'> # this is collected because of our conftest.py extension 
    <Function 'test_hello'> 

如果需要的話,你也可以包上面conftest.py文件作爲installable plugin,使通過安裝插件可用的擴展名。在這種情況下,根本不需要任何conftest.py文件。

+0

目前我無法測試這個方法是否工作,因爲py.test崩潰了,看到這個bug:http://bitbucket.org/hpk42/py-trunk/issue/119/fail- to-impot -__ init__py-internalerror – sorin 2010-09-14 11:53:29

+0

嘿索林。好的,Ronny和我剛剛發佈了一個新版本,附有一些修復程序,包括您看到的問題。做「easy_install -U py」應該帶給你。感謝報道,玩得開心。霍爾格 – hpk42 2010-09-14 15:41:39

5

將一個文件「setup.cfg」在項目的根目錄,它包含這兩條線:

[pytest] 
python_files=*.py 

然後py.test選擇所有*.py文件測試。在這裏它的解釋:pytest docs

與鼻:

nosetests --all-modules 
相關問題