Python測試框架之pytest高階用法(六)


1.修改 Python traceback 輸出

pytest --showlocals     # show local variables in tracebacks
pytest -l               # show local variables (shortcut)
pytest --tb=auto        # (default) 'long' tracebacks for the first and last
                        # entry, but 'short' style for the other entries
pytest --tb=long        # exhaustive, informative traceback formatting
pytest --tb=short       # shorter traceback format
pytest --tb=line        # only one line per failure
pytest --tb=native      # Python standard library formatting
pytest --tb=no          # no traceback at all

  --full-trace 參數會打印更多的錯誤輸出信息,比參數 --tb=long 還多,即使是 Ctrl+C 觸發的錯誤,也會打印出來

2. 執行失敗的時候跳轉到 PDB

pytest --pdb              # 每次遇到失敗都跳轉到 PDB
pytest -x --pdb           # 第一次遇到失敗就跳轉到 PDB,結束測試執行
pytest --pdb --maxfail=3  # 只有前三次失敗跳轉到 PDB 

  pdb 是 Python 標准庫的調試模塊。在 pytest 中,可以直接使用 --pdb 參數在測試失敗時開啟調試;

直接使用 --pdb 參數:

import pytest
def division(a, b):
    return int(a / b)
@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c
@pytest.mark.parametrize('a', [100,75])
@pytest.mark.parametrize('b, c', [(4,25),(3,25)])
def test_1(a, b, c):
    res = division(a, b)
    assert res == c
if __name__ == '__main__':
    pytest.main(["-q","--pdb"])  

斷言失敗,進入 pdb:

 

pdb 提示符出現后,便可以使用 pdb 的交互調試功能,查看錯誤時,有以下常用命令:

  •  p/print expr :輸出變量 expr 的值;
  • pp expr :美化輸出 expr 的值;
  • l/list :列出錯誤並顯示錯誤之前和之后的5行代碼;
  • l/lsit begin, end :列出錯誤,並顯示指定行號之間的代碼;
  • a/args :打印當前函數的所有參數和變量;
  • u/up :移動到堆棧的上一層;
  • d/down :移動到堆棧的下一層;
  • q/quit :退出當前調試會話(也會退出測試會話);

在控制台與 pdb 進行交互:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> e:\pycharmprojects\lianxi\test_abc.py(286)test_1()
-> assert res == c
(Pdb) p
*** SyntaxError: unexpected EOF while parsing
(Pdb) p a
75
(Pdb) pp a
75
(Pdb) l
281  	    assert res == c
282  	@pytest.mark.parametrize('a', [100,75])
283  	@pytest.mark.parametrize('b, c', [(4,25),(3,25)])
284  	def test_1(a, b, c):
285  	    res = division(a, b)
286  ->	    assert res == c
287  	if __name__ == '__main__':
288  	    pytest.main(["-q","--pdb"])
289  	
290  	# import pytest
291  	# @pytest.fixture(scope='function')
(Pdb) l 275,285
275  	import pytest
276  	def division(a, b):
277  	    return int(a / b)
278  	@pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), (1, 0, 0), (6, 8, 0)])
279  	def test_1(a, b, c):
280  	    res = division(a, b)
281  	    assert res == c
282  	@pytest.mark.parametrize('a', [100,75])
283  	@pytest.mark.parametrize('b, c', [(4,25),(3,25)])
284  	def test_1(a, b, c):
285  	    res = division(a, b)
(Pdb) a
a = 75
b = 4
c = 25
(Pdb) u
> d:\myprogram\miniconda3\lib\site-packages\_pytest\python.py(180)pytest_pyfunc_call()
-> result = testfunction(**testargs)
(Pdb) u
> d:\myprogram\miniconda3\lib\site-packages\pluggy\callers.py(187)_multicall()
-> res = hook_impl.function(*args)
(Pdb) d
> d:\myprogram\miniconda3\lib\site-packages\_pytest\python.py(180)pytest_pyfunc_call()
-> result = testfunction(**testargs)
(Pdb) q

------ generated html file: file://E:\PycharmProjects\lianxi\report.html ------
=========================== short test summary info ===========================
FAILED test_abc.py::test_1[4-25-75] - assert 18 == 25
!!!!!!!!!!!!!!!!!! _pytest.outcomes.Exit: Quitting debugger !!!!!!!!!!!!!!!!!!!
1 failed, 1 passed in 112.57s (0:01:52)

  更多的pdb使用方法可參考pdb詳細的使用方法:https://docs.python.org/3/library/pdb.html

3.設置斷點

在用例腳本中加入如下python代碼,pytest會自動關閉執行輸出的抓取,這里,其他test腳本不會受到影響,帶斷點的test上一個test正常輸出

 import pdb; pdb.set_trace()

4.獲取用例執行性能數據

pytest --durations=10

  

5.生成 JUnitXML 格式的結果文件

這種格式的結果文件可以被Jenkins或其他CI工具解析

pytest --junitxml=path

6.禁用插件 

例如,關閉 doctest 插件

pytest -p no:doctest

7.從Python代碼中調用pytest

pytest.main()                      # 基本用法
pytest.main(['-x', 'mytestdir'])   # 傳入配置參數
 
 
// 指定自定義的或額外的插件
# content of myinvoke.py
import pytest
class MyPlugin(object):
    def pytest_sessionfinish(self):
        print("*** test run reporting finishing")
 
pytest.main(["-qq"], plugins=[MyPlugin()]) 

8.測試腳本遷移后快速部署包含pytest的virtualenv

例如你從Gitlab倉庫里clone了項目組的刀刀同學編寫的測試腳本到你自己的電腦里,你想修改些東西,並調試,咋辦?可以通過下面的操作快速創建 VirtualEnv

cd <repository>
pip install -e .

  

 

 

  

 


免責聲明!

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



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