Hamcrest 断言
生活随笔
收集整理的這篇文章主要介紹了
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 断言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SharePoint 2013的100个
- 下一篇: Android相关sdk使用