pytest测试框架面试题,python测试框架pytest

  pytest测试框架面试题,python测试框架pytest

  就测试而言,无论是功能测试、自动化测试还是单元测试。一般来说,一个正确的预期结果是预设的,在测试执行的过程中会得到一个实际的结果。测试的成功在于将实际结果与预期结果进行比较。这种比较的过程实际上是断言的。让我们介绍一下assert的用法:

  1.比较大小是否等于test_assert.py

  # coding=utf-8 import pytest # function def add(a,b): return a b#测试等式def test _ add (): assert add (3,4)==7 #测试不等式def test_add2(): assert add(17,22)!=50#测试大于deftest _ add3 (): Assert Add (17,22)=50 #测试小于deftest _ add4 (): Assert Add (17,22)=50 if _ _ name _ _= _ _ main _ :pytest . main( test

  定义一个add()函数,用于计算两个参数的相加,并返回相加的结果。断言可以使用“==”、“!="、""、"="、=等符号来比较相等、不相等、小于、大于、大于或等于以及小于或等于。

  运行结果:

  ==========================测试会话开始==========================平台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============================失败===========================_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test _ add 4 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ def test _ add 4():assert add(17,22)=50E assert 39=50E其中39=add(17,22) test _ assert.py: 22:断言错误===============1失败,3通过

  二。测试是否包含test_assert2.py

  # coding=utf-8 import pytest # test equality def test _ in():a= hello b= he assert b in a # test不等式def test _ not _ in():a= hello b= hi assert b not in AIF _ _ name _= _ main _ _ :pytest . main( test _ assert 2 . py )通过定义a和b字符串变量比较包含的关系,Assert可以直接使用in和not in来比较包含和排除。

  运行结果:

  ====================================================平台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 _ assert 2 . py f .========================================失败次数========

  三。测试真假test_assert3.py

  #coding=utf-8import pytest#用于判断质数def是_ prime (n):如果n=1:对范围内的I返回false, n):If n % I==0:return false return true #判断是否为素数def test _ true():assert is _ prime(13)#判断是否为素数def test _ true():assert not is _ prime(7)If _ _ name _ _= _ _ main _ _ :pytest . main( test _ asset 3 . py )使用is _ prime()函数判断n是否为素数(只能被1和自身整除的数)。 返回值为真或假。Assert不需要任何辅助符号来直接判断一个对象是否为ture,而assert not用于判断其是否为false。

  运行结果:

  ===========================测试会话开始============================平台win32-Python 2。7 .十倍于一倍。4 .30-pytest-2。7 .2 rootdir :D:\ pyse \ pytest \ test _ case,inifile:plugins:html collected 1 items test _ assert 3。py F===================================失败=======显示,对于第二条测试用例来讲,7是素数,所以,is_prime()函数的返回结果是真的,而assertnot需要的正确结果是假的,因此,用例执行失败。创建了一个测试交流群,如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加测试交流群:829792258,还会有同行一起技术交流

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: