Pytest学习(五) 您所在的位置:网站首页 simpletest报错 Pytest学习(五)

Pytest学习(五)

2024-04-18 17:27| 来源: 网络整理| 查看: 265

常见的状态passed:测试通过failed:断言失败error:代码编写上的错误xfail:预期失败,加了 @pytest.mark.xfail()测试通过的栗子(passed)

示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:17 # @Author : longrong.lang # @FileName: test_pass.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 测试通过的例子 ''' import pytest @pytest.fixture() # 定义一个测试数据 def data(): return 1 def test_pass(data): assert 1 == data if __name__ == '__main__': pytest.main(["-q", "test_pass.py"]) 输出结果:断言失败的栗子(failed)1、正常断言失败

示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:26 # @Author : longrong.lang # @FileName: test_failed.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 断言失败的栗子 ''' import pytest @pytest.fixture() def data(): return 'python' def test_failed(data): assert 'test' in data if __name__ == '__main__': pytest.main(["-q", "test_failed.py"]) 输出结果:2、测试方法里主动抛出异常了

示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:26 # @Author : longrong.lang # @FileName: test_failedException.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 断言失败的栗子 ''' import pytest @pytest.fixture() def data(): return 'python' def test_failed(data): # 这块随便抛出一个异常了 raise IOError assert 'py' in data if __name__ == '__main__': pytest.main(["-q", "test_failedException.py"]) 输出结果:代码编写上的错误(error)1、fixture中有错

示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:34 # @Author : longrong.lang # @FileName: test_error.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 代码编写上的错误栗子 ''' import pytest @pytest.fixture() def data(): str = 'python' assert 'test' in str return str def test_error(data): assert data == 'python' if __name__ == '__main__': pytest.main(["-q", "test_error.py"]) 输出结果:

fixture里面断言失败,导致fixture标记的data会报错,使得data没有返回值;而test_error调用了错误的fixture,所以error表示代码写的有问题了

2、参数不存在

示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:34 # @Author : longrong.lang # @FileName: test_errorNoParam.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 代码编写上的错误栗子 ''' import pytest def data(): str = 'python' return str def test_error(data): assert data == 'python' if __name__ == '__main__': pytest.main(["-q", "test_errorNoParam.py"]) 输出结果:

data参数并不存在,找不到自然就error了

总结:测试用例的代码有异常,包括主动抛出异常或代码有异常,都算failed当测试用例调用的fixture有异常,或传入的参数有异常的时候,都算error如果一份测试报告中,error的测试用例数量越多,说明测试用例质量越差预期失败的栗子(xfail)

这个和testng的异常测试差不多了,就是断言预期的异常,可以测试是否需要代码抛出异常或不抛出。示例代码如下:

# -*- coding: utf-8 -*- # @Time : 2020/10/25 17:51 # @Author : longrong.lang # @FileName: test_xfail.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang ''' 预期失败栗子 ''' import pytest # 断言装饰器 @pytest.mark.xfail(raises=ZeroDivisionError) def test_xfail(): var = 1 / 0 print(var) 输出结果:

代码有异常,且和raised的异常类匹配,所以是xfail(算测试通过的一种,表示符合期望捕捉到的异常),并不算failed

如果和raised的异常类不匹配,则是failed



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有