国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python單元測試框架之pytest -- 斷言

這篇具有很好參考價值的文章主要介紹了Python單元測試框架之pytest -- 斷言。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

對于測試來講,不管是功能測試,自動化測試,還是單元測試。一般都會預設一個正確的預期結果,而在測試執(zhí)行的過程中會得到一個實際的結果。測試的成功與否就是拿實際的結果與預期的結果進行比較。這個比的過程實際就是斷言(assert)。

  在unittest單元測試框架中提供了豐富的斷言方法,例如assertEqual()、assertIn()、assertTrue()、assertIs()等,而pytest單元測試框架中并沒提供特殊的斷言方法,而是直接使用python的assert進行斷言。

  下面我們就來介紹assert?的使用。

比較大小與是否相等 ? ? ? ? ? ? ? ? ? ? ?

test_assert.py

#coding=utf-8
import pytest

# 功能
def add(a,b):
    return a + b

# 測試相等
def test_add():
    assert add(3,4) == 7 

# 測試不相等
def test_add2():
    assert add(17,22) != 50

# 測試大于
def test_add3():
    assert add(17,22) <= 50

# 測試小于
def test_add4():
    assert add(17,22) >= 50


if __name__ == '__main__':
    pytest.main("test_assert.py")

? ? 定義一個add()函數(shù),用于計算兩個入?yún)⑾嗉?,并將相加的結果返回。

  而assert可以使用直接使用“==”、“!=”、“<”、“>”、“>=”、"<=" 等符號來比較相等、不相等、小于、大于、大于等于和小于等于。

  運行結果:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 4 items

test_assert.py ...F

================================== FAILURES ===================================
__________________________________ test_add4 __________________________________

    def test_add4():
>       assert add(17,22) >= 50
E    assert 39 >= 50
E     +  where 39 = add(17, 22)

test_assert.py:22: AssertionError
===================== 1 failed, 3 passed in 0.02 seconds ======================

  顯然,17加22的結果并不大于50,所有最后一條用例失敗。

測試包含或不包含 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

test_assert2.py

#coding=utf-8
import pytest


# 測試相等
def test_in():
    a = "hello"
    b = "he"
    assert b in a 


# 測試不相等
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

if __name__ == '__main__':
    pytest.main("test_assert2.py")

?  通過定義a和b 字符串變量來比較包含的關系。

  assert 可以直接使用 in 和not in 來比較包含與不包含。

  運行結果:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 2 items

test_assert2.py F.

================================== FAILURES ===================================
___________________________________ test_in ___________________________________

    def test_in():
        a = "hello"
        b = "hi"
>       assert b in a
E    assert 'hi' in 'hello'

test_assert2.py:9: AssertionError
===================== 1 failed, 1 passed in 0.01 seconds ======================

  顯然“hello”并不包含“hi”,所以第一條測試用例運行失敗。

測試true或false ? ? ? ? ? ? ? ? ? ? ? ??

test_assert3.py

#coding=utf-8
import pytest


#用于判斷素數(shù)
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
        return True


# 判斷是否為素數(shù)
def test_true():
    assert is_prime(13)


# 判斷是否不為素數(shù)
def test_true():
    assert not is_prime(7)

if __name__ == '__main__':
    pytest.main("test_assert3.py")

?  通過is_prime()函數(shù)來判斷n 是否為素數(shù)(只能被1和它本身整除的數(shù))。返回值為ture或false。

  通過assert不需要任何輔助符號,直接判斷對象是否為ture,而assert not 用于判斷是否為false。

  運行結果:

============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile: 
plugins: html
collected 1 items

test_assert3.py F

================================== FAILURES ===================================
__________________________________ test_true __________________________________

    def test_true():
>       assert not is_prime(7)
E    assert not True
E     +  where True = is_prime(7)

test_assert3.py:22: AssertionError
========================== 1 failed in 0.01 seconds ===========================

  顯示,對于第二條測試用例來講,7是素數(shù),所以,is_prime()函數(shù)的返回結果是Ture,而assert?not?需要的正確結果是False,因此,用例執(zhí)行失敗。

加油吧,測試人!如果你需要提升規(guī)劃,那就行動吧,在路上總比在起點觀望的要好。未來的你肯定會感謝現(xiàn)在拼命的自己!

Python單元測試框架之pytest -- 斷言??文章來源地址http://www.zghlxwxcb.cn/news/detail-510708.html

到了這里,關于Python單元測試框架之pytest -- 斷言的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Pytest自動化測試框架---(單元測試框架)

    Pytest自動化測試框架---(單元測試框架)

    unittest是python自帶的單元測試框架,它封裝好了一些校驗返回的結果方法和一些用例執(zhí)行前的初始化操作,使得單元測試易于開展,因為它的易用性,很多同學也拿它來做功能測試和接口測試,只需簡單開發(fā)一些功能(報告,初始化webdriver,或者http請求方法)便可實現(xiàn)。 但自

    2024年02月14日
    瀏覽(120)
  • Pytest單元測試框架 —— Pytest+Allure+Jenkins的應用

    Pytest單元測試框架 —— Pytest+Allure+Jenkins的應用

    一、簡介 pytest+allure+jenkins進行接口測試、生成測試報告、結合jenkins進行集成。 pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高 allure-pytest是python的一個第三方庫。用于連接pytest和allure,使它們可以配合在一起

    2024年02月07日
    瀏覽(94)
  • [Python] 斷言assert與單元測試

    在Python中,斷言(assertion)是一種用于檢查程序中的條件是否為真的工具。它用于在程序的特定點處驗證一些假設,并在條件為假時觸發(fā)異常如果斷言為真,則程序繼續(xù)執(zhí)行;如果斷言為假,則引發(fā)AssertionError異常。 assert語句的語法如下: 其中,condition是一個布爾表達式,如

    2024年02月02日
    瀏覽(22)
  • Python單元測試pytest捕獲日志輸出

    Python單元測試pytest捕獲日志輸出

    使用pytest進行單元測試時,遇到了需要測試日志輸出的情況,查看了文檔 https://docs.pytest.org/en/latest/how-to/capture-stdout-stderr.html https://docs.pytest.org/en/latest/how-to/logging.html 然后試了一下,捕捉logger.info可以用caplog,獲取print輸出可用capsys,Demo如下: - a.py - test_a.py - 驗證:

    2024年04月10日
    瀏覽(28)
  • Python 面試:單元測試unit testing & 使用pytest

    calc.py test_calc.py employee.py test_employee.py 輸出為: setupClass setUp test_apply_raise tearDown .setUp test_email tearDown .setUp test_fullname tearDown .teardownClass Ran 3 tests in 0.001s OK employee.py test_employee.py 輸出為: setupClass setUp test_apply_raise tearDown .setUp test_email tearDown .setUp test_fullname tearDown .setUp tearDown

    2024年02月10日
    瀏覽(27)
  • Python測試框架 Pytest —— mock使用(pytest-mock)

    Python測試框架 Pytest —— mock使用(pytest-mock)

    安裝:pip install pytest-mock 這里的mock和unittest的mock基本上都是一樣的,唯一的區(qū)別在于pytest.mock需要導入mock對象的詳細路徑。 先將需要模擬的天氣接口,以及需要模擬的場景的代碼寫好,然后在進行遵循pytest的用例規(guī)范進行書寫關于mock的測試用例 通過上述代碼,提供pytest中

    2024年02月09日
    瀏覽(23)
  • Python自動化測試:unittest與pytest框架

    在Python中, unittest 和 pytest 都是常用的自動化測試框架。它們提供了編寫測試用例、測試套件和執(zhí)行測試的強大功能。 1. unittest框架 unittest 是Python標準庫的一部分,因此無需額外安裝。它提供了豐富的斷言方法,用于驗證測試結果。 示例代碼: python復制代碼 import unittest c

    2024年02月20日
    瀏覽(38)
  • python pytest 最簡單的接口自動化測試框架

    最近由于工作的原因,需要開發(fā)一個接口自動化測試框架,使用pytest框架、數(shù)據(jù)驅動,并展示直觀的測試報告。 具體的開發(fā)過程如下: 安裝必要的庫: pytest:用于編寫和運行測試用例。 requests:用于發(fā)送 HTTP 請求。 pytest-html:用于生成 HTML 格式的測試報告。 可以使用以下命

    2024年03月19日
    瀏覽(46)
  • Python測試框架pytest:常用參數(shù)、查找子集、參數(shù)化、跳過

    Python測試框架pytest:常用參數(shù)、查找子集、參數(shù)化、跳過

    Pytest是一個基于python的測試框架,用于編寫和執(zhí)行測試代碼。pytest主要用于API測試,可以編寫代碼來測試API、數(shù)據(jù)庫、UI等。 pytest是一個非常成熟的全功能的Python測試框架,主要有以下幾個優(yōu)點: 簡單靈活,容易上手。pytest的語法簡潔明了,易于理解和使用。 支持參數(shù)化。

    2024年02月13日
    瀏覽(22)
  • Python 自動化測試框架unittest與pytest的區(qū)別

    Python 自動化測試框架unittest與pytest的區(qū)別

    ?? 博客主頁: 美團程序員 ?? 專注于軟件測試領域相關技術實踐和思考,持續(xù)分享自動化軟件測試開發(fā)干貨知識! ?? 如果你也想學習軟件測試,文末卡片有我的交流群632880530,加入我們,一起交流和學習! 引言 前面一篇文章Python單元測試框架介紹已經(jīng)介紹了python單元測

    2024年02月13日
    瀏覽(32)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包