<ruby id="h6500"><table id="h6500"></table></ruby>
    1. <ruby id="h6500"><video id="h6500"></video></ruby>
          1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>

            python單元測試框架pytest簡(jiǎn)介

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

            1、Pytest介紹

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

             

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

            2、安裝pytest

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

            3、一個(gè)實(shí)例

            我們可以通過(guò)下面的實(shí)例,看看使用py.test進(jìn)行測試是多么簡(jiǎn)單。
            [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  
            這里我們定義了一個(gè)被測試函數func,該函數將傳遞進(jìn)來(lái)的參數加1后返回。我們還定義了一個(gè)測試函數test_func用來(lái)對func進(jìn)行測試。test_func中我們使用基本的斷言語(yǔ)句assert來(lái)對結果進(jìn)行驗證。
            下面來(lái)運行這個(gè)測試:
            [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 =========================  
            執行測試的時(shí)候,我們只需要在測試文件test_sample所在的目錄下,運行py.test即可。pytest會(huì )在當前的目錄下,尋找以test開(kāi)頭的文件(即測試文件),找到測試文件之后,進(jìn)入到測試文件中尋找test_開(kāi)頭的測試函數并執行。
            通過(guò)上面的測試輸出,我們可以看到該測試過(guò)程中,一個(gè)收集到了一個(gè)測試函數,測試結果是失敗的(標記為F),并且在FAILURES部分輸出了詳細的錯誤信息,幫助我們分析測試原因,我們可以看到"assert func(3) == 5"這條語(yǔ)句出錯了,錯誤的原因是func(3)=4,然后我們斷言func(3) 等于 5。
             

            4、再一個(gè)實(shí)例

            當需要編寫(xiě)多個(gè)測試樣例的時(shí)候,我們可以將其放到一個(gè)測試類(lèi)當中,如:
            [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')  
            我們可以通過(guò)執行測試文件的方法,執行上面的測試:
            [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  
            從測試結果中可以看到,該測試共執行了兩個(gè)測試樣例,一個(gè)失敗一個(gè)成功。同樣,我們也看到失敗樣例的詳細信息,和執行過(guò)程中的中間結果。

            5、如何編寫(xiě)pytest測試樣例

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

            6、如何執行pytest測試樣例

            執行測試樣例的方法很多種,上面第一個(gè)實(shí)例是直接執行py.test,第二個(gè)實(shí)例是傳遞了測試文件給py.test。其實(shí)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格式的測試報告用來(lái)與持續集成工具集成。

            生成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、最佳實(shí)踐

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

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

            老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月
              <ruby id="h6500"><table id="h6500"></table></ruby>
              1. <ruby id="h6500"><video id="h6500"></video></ruby>
                    1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>