2016-09-06 39 views
3

我要找的運行python -m unittest discover的一種方式,它會發現,比如說,目錄A測試,B和C.然而,目錄A,B和C有一個名爲dependencies內每個目錄他們,其中也有一些測試,但是,我不想跑。運行單元測試發現忽略特定目錄

有沒有辦法運行我的測試,滿足這些約束,而無需爲此創建腳本?

回答

-1

似乎python -m unittest下降到模塊目錄但不在其他目錄中。

它迅速嘗試了以下結構

temp 
    + a 
    - test_1.py 
    + dependencies 
    - test_a.py 

隨着結果

>python -m unittest discover -s temp\a 
test_1 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.002s 

OK 

然而,如果目錄是一個模塊目錄(包含文件__init__.py)的情況是不同的。

temp 
+ a 
    - __init__.py 
    - test_1.py 
    + dependencies 
    - __init__.py 
    - test_a.py 

這裏的結果是

>python -m unittest discover -s temp\a 
test_a 
.test_1 
. 
---------------------------------------------------------------------- 
Ran 2 tests in 0.009s 

OK 

這個答案的實用性爲你現在要看它是否是你的文件夾dependencies不是一個模塊的目錄可以接受的。

編輯:使用pytest是一個選項看到您的評論

會後?這個測試運行者有很多命令參數,其中一個專門用於排除測試。

Changing standard (Python) test discovery

從他們的網站

測試集

過程中忽略的路徑,您可以通過將--ignore=path選項的CLI收集過程中容易忽略某些測試目錄和模塊。 Pytest允許多個--ignore選項

+0

不幸的是,它有一個'__init __。py'文件,它不能有任何不同:( – lucasnadalutti

0

我遇到了同樣的問題,最終能夠找到這些方便的參數傳遞給unittest發現,解決了我的問題。

在此記載:https://docs.python.org/2/library/unittest.html#test-discovery

-s, --start-directory directory 
Directory to start discovery (. default) 

-p, --pattern pattern 
Pattern to match test files (test*.py default) 

所以我修改了我的命令是:

python -m unittest discover -s test 

因爲所有其實我是想運行在一個模塊,測試的所有測試。您也可以使用-p理論上匹配只觸及您的測試的正則表達式,忽略它可能找到的所有其餘部分。

相關問題