2011-10-07 51 views
19

不幸的是,我發現在Python中保持單元測試的方法太多了,而且通常沒有很好的文檔記錄。Python模塊單元測試的最佳文件結構組織?

我正在尋找一個 「終極」 結構,一個將完成大部分的以下要求:

  • 通過測試框架可被發現,其中包括:
    • pytest
    • nosetests
    • tox
  • 測試應該是outside the module files和另一個目錄中的模塊本身(維護),可能位於包級別的tests/目錄中。
  • 應該有可能只是執行測試文件(測試必須能夠知道的是,應該測試模塊)

請提供樣品測試文件,做了假的測試,指定文件名和目錄。

+0

你的問題是,真的嗎?你爲什麼不使用其中的一個框架,讓所有人都按照自己的意願去做? – pvoosten

回答

17

這是我一直在使用方法:

目錄結構

# All __init__.py files are empty in this example. 
app 
    package_a 
     __init__.py 
     module_a.py 
    package_b 
     __init__.py 
     module_b.py 
    test 
     __init__.py 
     test_app.py 
    __init__.py 
main.py 

main.py

# This is the application's front-end. 
# 
# The import will succeed if Python can find the `app` package, which 
# will occur if the parent directory of app/ is in sys.path, either 
# because the user is running the script from within that parect directory 
# or because the user has included the parent directory in the PYTHONPATH 
# environment variable. 

from app.package_a.module_a import aaa 
print aaa(123, 456) 

module_a.py

# We can import a sibling module like this. 
from app.package_b.module_b import bbb 
def aaa(s, t): 
    return '{0} {1}'.format(s, bbb(t)) 

# We can also run module_a.py directly, using Python's -m option, which 
# allows you to run a module like a script. 
# 
# python -m app.package_a.module_a 
if __name__ == '__main__': 
    print aaa(111, 222) 
    print bbb(333) 

module_b.py

def bbb(s): 
    return s + 1 

test_app.py

import unittest 

# From the point of view of testing code, our working modules 
# are siblings. Imports work accordingly, as seen in module_a. 
from app.package_a.module_a import aaa 
from app.package_a.module_a import bbb 

class TestApp(unittest.TestCase): 

    def test_aaa(self): 
     self.assertEqual(aaa(77, 88), '77 89') 

    def test_bbb(self): 
     self.assertEqual(bbb(99), 100) 

# Simiarly, we can run our test modules directly as scripts using the -m option, 
# or using nose. 
# 
# python -m app.test.test_app 
# nosetests app/test/test_app.py 

if __name__ == '__main__': 
    unittest.main() 
+0

謝謝,例如,但我敢肯定,如果你將運行test_app.py它會抱怨,無法找到'應用程序'。認爲測試必須在包被部署到Python包含路徑之前通過。 – sorin

+0

@sorin不錯,但您可以在我的答案結尾處提供的兩個示例中獲得非常接近的結果。 – FMc

+0

整個過程就是在沒有任何參數的情況下調用測試框架。 – sorin