python單元測試框架pytest簡介

發表于:2017-11-13來源:CSDN作者:liuchunming033點擊數: 標簽:python
pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。根據pytest的官方網站介紹,它具有如下特點:

1、Pytest介紹

pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。根據pytest的官方網站介紹,它具有如下特點:

 

  • 非常容易上手,入門簡單,文檔豐富,文檔中有很多實例可以參考
  • 能夠支持簡單的單元測試和復雜的功能測試
  • 支持參數化
  • 執行測試過程中可以將某些測試跳過,或者對某些預期失敗的case標記成失敗
  • 支持重復執行失敗的case
  • 支持運行由nose, unittest編寫的測試case
  • 具有很多第三方插件,并且可以自定義擴展
  • 方便的和持續集成工具集成
由于網上pytest的中文文檔比較少,自己學習過程中,基本上看的就是英文的官方文檔,對于不想看英文的同學們,本系列文章希望能夠幫大家一馬。
 

2、安裝pytest

與安裝其他的python軟件無異,直接使用pip安裝。
[python] view plain copy
 
  1. pip install -U pytest  
安裝完成后,可以驗證安裝的版本:
[python] view plain copy
 
  1. py.test --version  

3、一個實例

我們可以通過下面的實例,看看使用py.test進行測試是多么簡單。
[python] view plain copy
 
  1. # content of test_sample.py  
  2.   
  3. def func(x):  
  4.     return x+1  
  5.   
  6. def test_func():  
  7.     assert func(3) == 5  
這里我們定義了一個被測試函數func,該函數將傳遞進來的參數加1后返回。我們還定義了一個測試函數test_func用來對func進行測試。test_func中我們使用基本的斷言語句assert來對結果進行驗證。
下面來運行這個測試:
[python] view plain copy
 
  1. $ py.test  
  2. =========================== test session starts ============================  
  3. platform linux -- Python 3.4.1 -- py-1.4.27 -- pytest-2.7.1  
  4. rootdir: /tmp/doc-exec-101, inifile:  
  5. collected 1 items  
  6. test_sample.py F  
  7. ================================= FAILURES =================================  
  8. _______________________________ test_answer ________________________________  
  9. def test_answer():  
  10. assert func(3) == 5  
  11. assert 4 == 5  
  12. E + where 4 = func(3)  
  13. test_sample.py:5: AssertionError  
  14. ========================= 1 failed in 0.01 seconds =========================  
執行測試的時候,我們只需要在測試文件test_sample所在的目錄下,運行py.test即可。pytest會在當前的目錄下,尋找以test開頭的文件(即測試文件),找到測試文件之后,進入到測試文件中尋找test_開頭的測試函數并執行。
通過上面的測試輸出,我們可以看到該測試過程中,一個收集到了一個測試函數,測試結果是失敗的(標記為F),并且在FAILURES部分輸出了詳細的錯誤信息,幫助我們分析測試原因,我們可以看到"assert func(3) == 5"這條語句出錯了,錯誤的原因是func(3)=4,然后我們斷言func(3) 等于 5。
 

4、再一個實例

當需要編寫多個測試樣例的時候,我們可以將其放到一個測試類當中,如:
[python] view plain copy
 
  1. # content of test_class.py  
  2.   
  3. class TestClass:  
  4.     def test_one(self):  
  5.         x = "this"  
  6.         assert 'h' in x  
  7.   
  8.     def test_two(self):  
  9.         x = "hello"  
  10.         assert hasattr(x, 'check')  
我們可以通過執行測試文件的方法,執行上面的測試:
[python] view plain copy
 
  1. $ py.test -q test_class.py  
  2. .F  
  3. ================================= FAILURES =================================  
  4. ____________________________ TestClass.test_two ____________________________  
  5. self = <test_class.TestClass object at 0x7fbf54cf5668>  
  6. def test_two(self):  
  7. x = "hello"  
  8. assert hasattr(x, 'check')  
  9. assert hasattr('hello''check')  
  10. test_class.py:8: AssertionError  
  11. 1 failed, 1 passed in 0.01 seconds  
從測試結果中可以看到,該測試共執行了兩個測試樣例,一個失敗一個成功。同樣,我們也看到失敗樣例的詳細信息,和執行過程中的中間結果。

5、如何編寫pytest測試樣例

通過上面2個實例,我們發現編寫pytest測試樣例非常簡單,只需要按照下面的規則:
  • 測試文件以test_開頭(以_test結尾也可以)
  • 測試類以Test開頭,并且不能帶有 __init__ 方法
  • 測試函數以test_開頭
  • 斷言使用基本的assert即可

6、如何執行pytest測試樣例

執行測試樣例的方法很多種,上面第一個實例是直接執行py.test,第二個實例是傳遞了測試文件給py.test。其實py.test有好多種方法執行測試:
[python] view plain copy
 
  1. py.test               # run all tests below current dir  
  2. py.test test_mod.py   # run tests in module  
  3. py.test somepath      # run all tests below somepath  
  4. py.test -k stringexpr # only run tests with names that match the  
  5.                       # the "string expression", e.g. "MyClass and not method"  
  6.                       # will select TestMyClass.test_something  
  7.                       # but not TestMyClass.test_method_simple  
  8. py.test test_mod.py::test_func # only run tests that match the "node ID",  
  9.                    # e.g "test_mod.py::test_func" will select  
  10.                                # only test_func in test_mod.py  

7、測試報告

pytest可以方便的生成測試報告,即可以生成HTML的測試報告,也可以生成XML格式的測試報告用來與持續集成工具集成。

生成HTML格式報告:

 

[python] view plain copy
 
  1. py.test --resultlog=path  
生成XML格式的報告:

 

 

[python] view plain copy
 
  1. py.test --junitxml=path  

 

8、如何獲取幫助信息

[python] view plain copy
 
  1. py.test --version # shows where pytest was imported from  
  2. py.test --fixtures # show available builtin function arguments  
  3. py.test -h | --help # show help on command line and config file options  

9、最佳實踐

其實對于測試而言,特別是在持續集成環境中,我們的所有測試最好是在虛擬環境中。這樣不同的虛擬環境中的測試不會相互干擾的。
由于我們的實際工作中,在同一個Jekins中,運行了好多種不同項目冊的測試,因此,各個測試項目運行在各自的虛擬環境中。
將pytest安裝在虛擬環境中:
1、將當前目錄創建為虛擬環境
[python] view plain copy
 
  1. virtualenv .        # create a virtualenv directory in the current directory  
  2. source bin/activate # on unix  
2、在虛擬環境中安裝pytest:
[python] view plain copy
 
  1. pip install pytest  
  2.  

原文轉自:http://blog.csdn.net/liuchunming033/article/details/46501653

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97