pytest 斷言
- 斷言:一個標准的用例都包含了斷言,編寫pytest自動化腳本的時候,也需要設置斷言
- assert使用
- 常用分三種 1:比較大小與是否相等 2:包含或不包含 3:驗證boolean
- 例子
- 比較大小
-
#coding: UTF-8 import pytest # 比較大小與是否相等 def test_assert_equal assert 2+2==2*2 class Test_Class(): def test_assert_equal(self): assert 4==3 def test_assert_noequal(self): assert 4!=3 def test_assert_greater(self): assert 4>=3 if __name__ == '__main__': pytest.main("-v -s assert_001_equal_test.py")
- 包含
#coding: UTF-8 import pytest # 測試包含或不包含 def test_in(): a = "Hello,Jerry" b = "Hello" assert b in a def test_not_in(): a = "Hello,Jerry" b = "Jerry" assert b not in a class Test_Class(): def test_in(self): a = "Hello,Jerry" b = "Hello" assert b in a if __name__ == '__main__': pytest.main("-v -s assert_002_contain_test.py")
- 驗證boolean
#coding: UTF-8 import pytest def check(flag): if flag!=0: return True else: return False def test_boolean(): assert check(1) class Test_Boolean_Assert(): def test_boolean(self): assert check(0) if __name__ == '__main__': pytest.main("-v -s assert_003_boolean_test.py")
歡迎一起交流(群號:575479860)