python测验2_python pytest测试框架(二)

本文链接:https://blog.csdn.net/yxxxiao/article/details/94602614

目录

一、pytest 命令行传参

二、pytest 断言

1、正常断言

2、异常断言

三、pytest 生成测试报告

1、文本格式的报告

2、生成JUnitXml格式报告

3、将测试报告发送到pastebin服务器

4、生成Html格式报告

一、pytest 命令行传参

命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在cmd执行"pytest --html=report.html",这里面的”--html=report.html“就是从命令行传入的参数对应的参数名称是html,参数值是report.html,

总结:

1.conftest.py文件名字是固定的,不可以做任何修改

2.文件和用例文件在同一个目录下,那么conftest.py作用于整个目录

3.conftest.py文件所在目录必须存在__init__.py文件

4.conftest.py文件不能被其他文件导入

5.所有同目录测试文件运行前都会执行conftest.py文件

步骤:

1.首先需要在contetest.py添加命令行选项,命令行传入参数”—cmdopt“, 用例如果需要用到从命令行传入的参数,就调用cmdopt函数:

#content of conftest.py

importpytestdefpytest_addoption(parser):

parser.addoption("--cmdopt", action="store", default="type1", help="my option: type1 or type2")

@pytest.fixturedefcmdopt(request):return request.config.getoption("--cmdopt")

2.测试用例编写案例

#content of test_sample.py

importpytestdeftest_answer(cmdopt):if cmdopt == "type1":print("first")elif cmdopt == "type2":print("second")assert 0 #to see what was printed

if __name__ == "__main__":

pytest.main(["-s", "test_case1.py"])

3.运行结果:

1781369-20190910111142432-483937534.png

3.带参数启动

如果不带参数执行,那么传默认的default=”type1”,接下来在命令行带上参数去执行

$ pytest -s test_sample.py --cmdopt=type2

二、pytest 断言

断言是判断实际结果与预期结果的重要方法。pytest除了支持正常情况的断言,还支持异常断言。

1、正常断言

正常的断言在上一篇博客中已经有所体现,pytest使用最基本的python中的assert语句进行断言,下面我们再举一个例子

#content of test_assert1.py

deff():return 3

deftest_function():assert f() == 4

执行上面测试:

$ py.test test_assert1.py=========================== test session starts ============================platform linux-- Python 3.4.1 -- py-1.4.27 -- pytest-2.7.1rootdir:/tmp/doc-exec-87, inifile:

collected1items

test_assert1.py F================================= FAILURES =================================

______________________________ test_function _______________________________

deftest_function():> assert f() == 4Eassert 3 == 4E+ where 3 =f()

test_assert1.py:5: AssertionError========================= 1 failed in 0.01 seconds =========================

我们发现,该测试失败了。而且pytest帮我们打印出来了中间表达式f()的结果,这样我们就可以非常清晰的知道该测试为什么失败。但是,如果你像下面这样写assert语句,则我们得不到assert表达式中的子表达是的中间结果:

assert a % 2 == 0, "value was odd, should be even"

所以,我们在写assert语句的时候,子表达式最好是一个函数签名,这样我们就可以得到该函数的计算结果,以便我们知道测试为什么失败。我们将上面的语句改成下面这样就可以了:

value = a % 2

assert value == 0, "value was odd, should be even"

2、异常断言

有些时候我们会对某些异常写断言语句,例如我们断言1除以0,将产生一个ZeroDivisionError类型的异常。针对这样的断言,pytest给我们提供了pytest.raise方法:

importpytestdeftest_zero_division():

with pytest.raises(ZeroDivisionError):1 / 0

执行这个测试:

C:\Users\liu.chunming\Desktop>py.test idlist.py============================= test session starts =============================platform win32-- Python 2.7.10 -- py-1.4.28 -- pytest-2.7.1rootdir: C:\Users\liu.chunming\Desktop, inifile:

plugins: capturelog, instafail, pythonpath

collected1items

idlist.py .========================== 1 passed in 0.04 seconds ===========================

有的时候,我们可能需要在测试中用到产生的异常中的某些信息,比如异常的类型type,异常的值value等等。下面我们修改下上面的测试:

importpytestdeftest_recursion_depth():

with pytest.raises(ZeroDivisionError) as excinfo:1/0assert excinfo.type == 'RuntimeError'

这个测试中,我们使用了测试的异常类型:excinfo.type。执行这个测试:

C:\Users\liu.chunming\Desktop>py.test idlist.py============================= test session starts =============================platform win32-- Python 2.7.10 -- py-1.4.28 -- pytest-2.7.1rootdir: C:\Users\liu.chunming\Desktop, inifile:

plugins: capturelog, instafail, pythonpath

collected1items

idlist.py F================================== FAILURES ===================================

____________________________ test_recursion_depth _____________________________

deftest_recursion_depth():

with pytest.raises(ZeroDivisionError) as excinfo:1/0> assert excinfo.type == 'RuntimeError'Eassert == 'RuntimeError'E+ where =

onError tblen=1>.type

idlist.py:5: AssertionError========================== 1 failed in 0.05 seconds ==========================

因为该测试断言产生的异常类型是RuntimeError,而实际上产生的异常类型是ZeroDivisionError,所以测试失败了。在测试结果中,可以看到assert子表达式excinfo.type的值。

三、pytest 生成测试报告

本文讲述pytest如何生成测试报告。

首先准备一段测试代码:

importpy.testclassTestCase(object):deftest_eq_set(self):assert set([0, 10, 11, 12]) == set([0, 20, 21])deftest_eq_dict(self):assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}deftest_eq_list(self):assert [0, 1, 2] == [0, 1, 3]deftest_eq_longer_list(self):assert [1,2] == [1,2,3]

1、文本格式的报告

py.test test_report.py --resultlog=d:\log.txt

指定当前路径下生成log.txt文件,打开文件,内容如下:

1781369-20190910111631314-242816521.png

2、生成JUnitXml格式报告

该格式方便与CI服务器进行集成

py.test test_report.py --junitxml=d:\log.xml

打开生成的log.xml,内容如下:

1781369-20190910111713111-1337989060.png

3、将测试报告发送到pastebin服务器

py.test test_report.py --pastebin=all

1781369-20190910111739118-44012985.png

点击生成的网址,内容如下:

1781369-20190910111833738-1156978356.png

当然,你可以只将失败的报告发送到pastebin服务器

py.test test_report.py --pastebin=failed

4、生成Html格式报告

这个需要安装pytest的第三方插件pytest-html:

pip install -U pytest-html

执行测试:

py.test test_report.py --html=d:\log.html

打开生成的测试报告log.html:

1781369-20190910112001135-1741266466.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值