pytest中的assert斷言

2022-02-03 12:56:46 字數 978 閱讀 4476

assert斷言

1. 判斷兩個數值是否相等,相等則認為是true

2. 判斷兩個值大小關係

3. 判斷函式方法的返回值和某個值是否相等或者大小關係

上面3種可以統稱為數值大小比較

4. 判斷部分字串是否包含在某個字串中,「a」 in 「abc」,在為true,不在為false

5. 判斷 函式結果不為false, 不是false則為true,反之false

assert not false
比較運算子: ==   相等

!= 不相等

in 後者包含前者

not false  不為true

<  前者小於後者

>  後者小於前者

import pytest 

def inc(a):

return a + 1

class testccc:

def test_demo1(self):

assert 2 == 2

def test_demo2(self):

assert 2 != 3

def test_demo3(self):

assert inc(4) != 4

def test_demo4(self):

assert inc(4) != 5

# 判斷某個字元是否在某個字串中

def test_demo6(self):

assert "ho" in "hello"

def test_demo7(self):

assert "h" in "hello"

def test_demo8(self):

assert not false

def test_demo9(self):

assert 5<6

if __name__ == '__main__':

pytest.main(['-sv'])

pytest學習教程 assert 3

assert就是斷言,每個測試用例都需要斷言。與unittest不同,pytest使用的是python自帶的assert關鍵字來進行斷言,大大降低了學習成本。assert關鍵字後面可以接乙個表示式,只要表示式的最終結果為true,那麼斷言通過,用例執行成功,否則用例執行失敗。pytest的用例失敗描...

pytest框架筆記 六 assert斷言

斷言是寫自動化測試基本最重要的一步,乙個用例沒有斷言,就失去了自動化測試的意義了,什麼是斷言呢?簡單來講就是實際結果和期望結果去對比,符合預期那就測試pass,不符合預期那就測試failed pytest允許您使用標準python斷言來驗證python測試中的期望和值。encoding utf 8 ...

pytest文件11 assert斷言

斷言是寫自動化測試基本最重要的一步,乙個用例沒有斷言,就失去了自動化測試的意義了。什麼是斷言呢?簡單來講就是實際結果和期望結果去對比,符合預期那就測試pass,不符合預期那就測試 failed pytest允許您使用標準python斷言來驗證python測試中的期望和值。例如,你可以寫下 conte...