2017-06-14 41 views
0

這是一種不尋常的情況 - 大多數Django用戶都希望Django的測試運行者能夠找到他們所有的測試。如何禁用Django的測試發現?

我正在研究一個Python庫,其中包含多個運行不同配置的測試套件,所以我不想讓發現從錯誤配置中找到並運行測試。如何完全禁用發現,並依賴僅在INSTALLED_APPS中顯式聲明的應用程序運行測試的1.6版之前的行爲?

我的圖書館結構:

library/ # django app used by others 
tests/  # custom test suites here 
    core/  # tests of core functionality 
    custom/ # tests of a custom feature requiring separate config 
    contrib/ # tests for assorted contrib features, also requiring separate config 
    manage_core.py # separate manage.py files for each "project" 
    manage_custom.py # these specify settings file to use. 
    manage_contrib.py 
    settings.py   # base settings for all tests 
    settings_core.py # settings for 'core' tests including unique INSTALLED_APPS 
    settings_custom.py # settings for 'custom' tests; different INSTALLED_APPS 
    settings_contrib.py # settings for 'contrib' tests; different INSTALLED_APPS 

的問題是,該命令,它應該只對「的contrib」測試套件運行測試,也發現和「核心」運行測試: ./manage_contrib.py test contrib.tests

+0

如果未安裝必要的應用程序,您可以[跳過測試](https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures)。 – knbk

回答

0

嗯,不幸的是我不知道設置參數可能讓你告訴unittest只能從單個應用程序運行(a-la「settings.TEST_DIRECTORIES = settings.INSTALLED_APPS」),但如果你能夠給你的測試一個獨特的命名約定,運行測試套件時可以使用--pattern =選項。

舉例來說,如果你有

/myapp/tests/test_a_models.py 
/myapp/tests/test_b_models.py 

你只能用./manage.py test --pattern='*_a_*'運行,然後運行b相./manage.py test --pattern='*_b_*'

肯定不理想,但可能會完成這項工作取決於有多少靈活性你在您自己的應用程序中使用測試命名約定。

0

當您在Django中運行測試時,默認情況下它會運行以def test_開頭的所有函數。當您在前面添加下劃線時,它將忽略該特定測試。將所有不想運行的測試功能更改爲:def _test_,它應該可以工作。如果您有太多的測試需要忽略,您可以進行搜索並從test_更換爲_test_

1

它從Django文檔丟失,但該命令行中有一個選項,通過./manage.py help test發現:

-t TOP_LEVEL, --top-level-directory TOP_LEVEL 
        Top level of project for unittest discovery. 

令人困惑的是,指定模塊測試沒有出現,以防止測試發現,但指定子目錄呢,是這樣的:

./manage_contrib.py test contrib.tests -t ./contrib/

這似乎是防止位於的contrib外試驗發現。