Pytest 断言

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)

 

posted @   Jerry-Learn  阅读(1363)  评论(1)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示