免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
pytest文檔57-單元測試代碼覆蓋率(pytest-cov)

前言

我們在做測試的時候,經(jīng)常遇到領(lǐng)導(dǎo)的靈魂拷問:你的測試用例覆蓋率是多少,達(dá)到100%了么?你如何保證你的測試質(zhì)量?
測試用例的覆蓋率如何統(tǒng)計呢,如何知道開發(fā)的代碼,我們都測到了,不會存在漏測的情況。

pytest-cov

先命令行安裝 pytest-cov 2.10.1版本

pip install pytest-cov==2.10.1

環(huán)境要求:
1.python3.6.6 版本
備注:其它版本沒試過

python3.6.0版本會遇到以下問題

INTERNALERROR>raise CoverageException("Couldn't use data file {!r}:{}".format(self.filename, msg)) INTERNALERROR> coverage.misc.CoverageException: Couldn't use data file'C:\\Users\\Desktop\\Pytest\\.coverage': Safety level may not be changed inside a transaction

解決辦法:安裝3.6.1以上版本

實(shí)現(xiàn)功能

在做單元測試時,代碼覆蓋率常常被拿來作為衡量測試好壞的指標(biāo),甚至,用代碼覆蓋率來考核測試任務(wù)完成情況,
比如,代碼覆蓋率必須達(dá)到80%或 90%。于是乎,測試人員費(fèi)盡心思設(shè)計案例覆蓋代碼。
單元測試的方法有:語句覆蓋/判定覆蓋/條件覆蓋/路徑覆蓋

先看一個簡單的案例,前端實(shí)現(xiàn)一個功能,根據(jù)接口返回的不同code值,判斷支付的結(jié)果,給用戶返回提示友好的信息

前端實(shí)現(xiàn)功能:根據(jù)接口返回的不同code值,判斷支付的結(jié)果,給用戶返回提示友好的信息 接口返回格式:{ "code": 0, "msg": "success!", "data": [] } 錯誤碼參照 0 - 成功 30000 - 參數(shù)錯誤 30001 - 余額不足 30002 - 達(dá)到當(dāng)天最大額度 201102 - 銀行卡被凍結(jié)

實(shí)現(xiàn)代碼

# pay.py ''' # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ 接口返回格式 { "code": 0, "msg": "success!", "data": [] } 錯誤碼參照 0 - 成功 30000 - 參數(shù)錯誤 30001 - 余額不足 30002 - 達(dá)到當(dāng)天最大額度 201102 - 銀行卡被凍結(jié) ''' def pay_status(result): '''根據(jù)接口返回code狀態(tài),給用戶提示對應(yīng)的結(jié)果''' if result.get("code") == 0: return "支付成功" elif result.get("code") == 30000: return "支付失敗: %s" % result.get("msg") elif result.get("code") == 30001: return "支付失敗: %s" % result.get("msg") elif result.get("code") == 30002: return "支付失敗: %s" % result.get("msg") elif result.get("code") == 201102: return "支付失敗: %s" % result.get("msg") else: return "支付失敗: 系統(tǒng)異常,未知錯誤"

整個項目目錄結(jié)構(gòu)如下

  • src 是項目的源碼
  • tests 是我們寫的單元測試用例
  • src和tests放同一個項目的根目錄下

用例設(shè)計

在tests/test_pay.py下寫測試用例,先只寫一個支付成功的案例

from src.pay import pay_status # 作者-上海悠悠 QQ交流群:717225969 # blog地址 https://www.cnblogs.com/yoyoketang/ def test_pay_success(): result = { "code": 0, "msg": "success!", "data": [] } assert pay_status(result) == "支付成功"

運(yùn)行用例

運(yùn)行用例的時候加上 —cov 參數(shù)

pytest —cov

運(yùn)行結(jié)果

>pytest --cov ================================================= test session starts ================================================= platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 rootdir: D:\soft\pytest-demo-cov plugins: change-report-1.0, cov-2.10.1, html-1.19.0, metadata-1.8.0 collected 1 item tests\test_pay.py . [100%] ----------- coverage: platform win32, python 3.6.6-final-0 ----------- Name Stmts Miss Cover --------------------------------------- src\__init__.py 0 0 100% src\pay.py 13 9 31% tests\__init__.py 0 0 100% tests\test_pay.py 4 0 100% --------------------------------------- TOTAL 17 9 47% ================================================== 1 passed in 0.10s ==================================================

從報告可以看出src\pay.py 的代碼測試覆蓋率是31%,其它文件都是100%覆蓋,這就說明我們單元測試代碼測試覆蓋率是31%
還有一個指標(biāo)是測試用例的執(zhí)行率,測試用例在test_pay.py文件,執(zhí)行率是100%,說明用例全部執(zhí)行了。

coverage生成html報告

coverage 相關(guān)參數(shù)查看,使用pytest -h

> pytest -h coverage reporting with distributed testing support: --cov=[SOURCE] Path or package name to measure during execution (multi-allowed). Use --cov= to not do any source filtering and record everything. --cov-report=TYPE Type of report to generate: term, term-missing, annotate, html, xml (multi-allowed). term, term- missing may be followed by ":skip-covered". annotate, html and xml may be followed by ":DEST" where DEST specifies the output location. Use --cov-report= to not generate any output. --cov-config=PATH Config file for coverage. Default: .coveragerc --no-cov-on-fail Do not report coverage if test run fails. Default: False --no-cov Disable coverage report completely (useful for debuggers). Default: False --cov-fail-under=MIN Fail if the total coverage is less than MIN. --cov-append Do not delete coverage but append to current. Default: False --cov-branch Enable branch coverage. --cov-context=CONTEXT Dynamic contexts to use. "test" for now.

生成html的報告

pytest —cov —cov-report=html

執(zhí)行完成,在項目根目錄會生成 htmlcov 目錄

運(yùn)行 index.html 文件查看代碼覆蓋率

點(diǎn)開src\pay.py

想覆蓋率達(dá)到100%,那得再繼續(xù)寫用例,讓每個if分支情況都覆蓋到

指定被測代碼

如果我們想指定執(zhí)行項目里面的某個模塊,可以通過—cov=模塊 來運(yùn)行

pytest —cov=src

>pytest --cov=src ================================================= test session starts ================================================= platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 rootdir: D:\soft\pytest-demo-cov plugins: change-report-1.0, cov-2.10.1, html-1.19.0, metadata-1.8.0 collected 1 item tests\test_pay.py . [100%] ----------- coverage: platform win32, python 3.6.6-final-0 ----------- Name Stmts Miss Cover ------------------------------------- src\__init__.py 0 0 100% src\pay.py 13 9 31% ------------------------------------- TOTAL 13 9 31% ================================================== 1 passed in 0.07s ==================================================

也可以指定具體的py模塊名稱

pytest —cov=src.pay

但不能寫成pytest --cov=src/pay.py

2020年第五期《python接口自動化+測試開發(fā)》課程,10月11號開學(xué)(火熱報名中?。?/a>
10月11-1月320:30-22:30

打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
聊聊 Python 代碼覆蓋率工具 - Coverage
pytest入門 -7 conftest.py的應(yīng)用
入坑 Python 后強(qiáng)烈推薦的一套工具庫
超干!如何建立一個完美的 Python 項目?
使用Pytest創(chuàng)建一個Python測試自動化項目
pytest系列(三) - fixture 的多樣玩法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服