pytest之 配置文件


pytest尋找命令行參數的配置文件順序是:pytest.ini,tox.ini和setup.cfg。

例如當我們執行:

 

  1. py.test path/to/testdir path/other/  
py.test path/to/testdir path/other/

的時候,pytest將會從所有測試目錄的共同目錄下開始尋找配置文件,直到尋找到系統的根目錄位置。具體的是:

 

  1. # first look for pytest.ini files  
  2. path/pytest.ini  
  3. path/setup.cfg     # must also contain [pytest] section to match  
  4. path/tox.ini       # must also contain [pytest] section to match  
  5. pytest.ini  
  6. ...                # all the way down to the root  
# first look for pytest.ini files
path/pytest.ini
path/setup.cfg     # must also contain [pytest] section to match
path/tox.ini       # must also contain [pytest] section to match
pytest.ini
...                # all the way down to the root

為了讓pytest更快的找到配置文件,我們最好是將其放到所有測試腳本的頂層目錄下,如上例中的path目錄下。

3、如何避免pytest尋找某些目錄或者文件中的測試函數

默認的情況下,pytest將會進入到當前目錄下的目錄和文件中,去收集測試用例(test_開頭的函數)。但是這幾個目錄和文件pytest是不會進入的:

 

  •  ’.*’:以‘.’開頭的文件和目錄
  • ’CVS’:CVS文件
  • ’_darcs’:_darcs版本控制目錄中
  • ’{arch}’:
  • ’*.egg’:所有.egg結尾的文件中。

 

我們可以自定義不讓pytest進入的目錄,我們可以將這些目錄或者文件寫入到配置文件中,例如:

 

  1. # content of setup.cfg  
  2. [pytest]  
  3. norecursedirs = .svn _build tmp*  
# content of setup.cfg
[pytest]
norecursedirs = .svn _build tmp*

這樣,pytest就不會進入到.svn、_build、和任何tmp開頭的文件中去收集測試函數。

4、指定什么樣的類被認為是測試類

默認情況下Test開頭的類被認為是測試類,pytest將其中的所有test開頭的方法認為是測試方法。我們也可以指定什么樣的類被認為是測試類,例如指定Suite結尾的類是測試類:

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_classes = *Suite  
# content of pytest.ini
[pytest]
python_classes = *Suite

 

5、指定什么樣的文件被認為是測試文件

默認情況下,以test開頭或者結尾的文件被認定為測試文件,pytest將在其中尋找測試方法。我們也可以指定什么樣的文件被認為是測試文件,例如指定_file結尾的文件是測試文件:

 

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_classes = *_file  
# content of pytest.ini
[pytest]
python_classes = *_file

 

6、指定什么樣的函數被認為測試函數

 

默認情況下,所有以test開頭的函數都被認為是測試函數。我們可以指定什么樣的函數是測試函數,例如,指定_test結尾的函數是測試函數:

  1. # content of pytest.ini  
  2. [pytest]  
  3. python_functions = *_test  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM