日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hamcrest 断言

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hamcrest 断言 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Hamcrest 是一個為了測試為目的,能組成靈活表達式的匹配器類庫。用于編寫斷言的框架,提高可讀性以及開發效率。

安裝
pip install pyhamcrest

導入
from hamcrest import *

常用方法
equal_to(obj): 比較兩個對象

close_to(value, delta): 比較兩個值是否接近,范圍:[value-delta, value+delta]

contains_string(substring: str):包含某個字符

assert_that("this is a string", equal_to("this is a string"))assert_that(1.0, close_to(0.5, 0.5))assert_that('abc', contains_string('a'))

參數化用例

import pytest from hamcrest import *@pytest.mark.parametrize("price, expect_price, delta", [(96.0, 100, 0.05), (101, 96, 0.1), (90, 100, 0.05) ]) def test_demo(price, expect_price, delta):print(price, ",", round(expect_price * delta, 2), ",", expect_price - expect_price * delta, ",",expect_price + expect_price * delta)assert_that(price, close_to(expect_price, round(expect_price * delta, 2)))

結果:

PASSED [ 33%]96.0 , 5.0 , 95.0 , 105.0 PASSED [ 66%]101 , 9.6 , 86.4 , 105.6 FAILED [100%]90 , 5.0 , 95.0 , 105.0demo_test.py:4 (test_demo[90-100-0.05]) price = 90, expect_price = 100, delta = 0.05@pytest.mark.parametrize("price, expect_price, delta", [(96.0, 100, 0.05), (101, 96, 0.1), (90, 100, 0.05)])def test_demo(price, expect_price, delta):print(price, ",", round(expect_price * delta, 2), ",", expect_price - expect_price * delta,",",expect_price + expect_price * delta) > assert_that(price, close_to(expect_price, round(expect_price * delta, 2))) E AssertionError: E Expected: a numeric value within <5.0> of <100> E but: <90> differed by <10.0>demo_test.py:11: AssertionErrorAssertion failed

總結

以上是生活随笔為你收集整理的Hamcrest 断言的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。