1个练习引起的系列学习之pytest(一)

    拳不离手曲不离口,每日操练不可少!
python

    今天的练习题目:输入某年某月某日,判断这一天是这一年的第几天?git

    代码写完了,自测的工做仍是不可少的,想尝试着用工具或者框架完成这项工做。
github

    代码:https://github.com/wanglanqing/Python_Project/tree/master/dayByDay/day4框架


1、安装ide

使用pip工具安装很是方便,执行pip install pytest便可。工具


2、编写测试用例学习


    1.用例规则测试

    • 以test_开头或以_test结尾的测试文件;this

    • 以Test开头的测试类;spa

    • 以test_开头的测试方法;

    • 测试类中,不能有__init__方法;



    2.正常断言

    pytest的断言使用assert,同unittest框架相比,大大下降了断言的学习成本。

def test_20171231_365(self):
    self.d4.get_date(2017, 12, 31)
    days = self.d4.get_days()
    assert days==365

    

    2.异常断言

    对于无效的数据,进行了异常的处理,最初单纯的使用assert时,发现执行该条case时,总会出错。经过使用with pytest.raises(Exception) as err_info的方式,可以ExceptionInfo() object,经过object的type、match() 、value等进行异常断言。

def test_day_is_minus(self):
    with pytest.raises(LowThanZero) as err_info:
        self.d4.get_date(2010,-2,1)
        self.d4.get_days()
    assert err_info.match('输入的值小于0')


  python 提供的API中描述了with pytest.raise()的使用方法。

>>> value = 15
>>> with raises(ValueError) as exc_info:
...     if value > 10:
...         raise ValueError("value must be <= 10")
...     assert exc_info.type == ValueError  # this will not execute


3、执行

    在pycharm中执行,【Run】-【Edit Configurations】,设置Working directory

    c0a7386dab616c31a13ca10834e75ba9.jpg

配置好以后,便可执行。

c7339808e1a261e24617a431d2d9f5ed.png


4、生成测试报告

       在命令行执行pytest --help,能够查看pytest的用法。

5b272aad930d144998584cc3abdb86df.png

    修改设置,【Run】-【Edit Configurations】,在Additional Arguments处,增长--junit-xml参数。

f70b22bf7a5efaaac82be93ca774d01f.png运行结束后,测试报告已保存到本地。

相关文章
相关标签/搜索