网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
步骤:
1.首先需要在contetest.py添加命令行选项,命令行传入参数”—cmdopt“, 用例如果需要用到从命令行传入的参数,就调用cmdopt函数:
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption(
"--cmdopt", action="store", default="type1", help="my option: type1 or type2"
)
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
2.测试用例编写案例
# content of test_sample.py
import pytest
def test_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.运行结果:
3.带参数启动
如果不带参数执行,那么传默认的default=”type1”,接下来在命令行带上参数去执行
$ pytest -s test_sample.py --cmdopt=type2
二、pytest 断言
断言是判断实际结果与预期结果的重要方法。pytest除了支持正常情况的断言,还支持异常断言。
1、正常断言
正常的断言在上一篇博客中已经有所体现,pytest使用最基本的python中的assert语句进行断言,下面我们再举一个例子
# content of test_assert1.py
def f():
return 3
def test_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.1
rootdir: /tmp/doc-exec-87, inifile:
collected 1 items
test_assert1.py F
================================= FAILURES =================================
______________________________ test_function _______________________________
def test_function():
> assert f() == 4
E assert 3 == 4
E + 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方法:
import pytest
def test_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.1
rootdir: C:\Users\liu.chunming\Desktop, inifile:
plugins: capturelog, instafail, pythonpath
collected 1 items
idlist.py .
========================== 1 passed in 0.04 seconds ===========================
有的时候,我们可能需要在测试中用到产生的异常中的某些信息,比如异常的类型type,异常的值value等等。下面我们修改下上面的测试:
import pytest
def test_recursion_depth():
with pytest.raises(ZeroDivisionError) as excinfo:
1/0
assert 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.1
rootdir: C:\Users\liu.chunming\Desktop, inifile:
plugins: capturelog, instafail, pythonpath
collected 1 items
idlist.py F
================================== FAILURES ===================================
____________________________ test_recursion_depth _____________________________
def test_recursion_depth():
with pytest.raises(ZeroDivisionError) as excinfo:
1/0
> assert excinfo.type == 'RuntimeError'
E assert <type 'exceptions.ZeroDivisionError'> == 'RuntimeError'
E + where <type 'exceptions.ZeroDivisionError'> = <ExceptionInfo ZeroDivisi
onError tblen=1>.type
idlist.py:5: AssertionError
========================== 1 failed in 0.05 seconds ==========================
因为该测试断言产生的异常类型是RuntimeError,而实际上产生的异常类型是ZeroDivisionError,所以测试失败了。在测试结果中,可以看到assert子表达式excinfo.type的值。
三、pytest 生成测试报告
本文讲述pytest如何生成测试报告。
首先准备一段测试代码:
import py.test
class TestCase(object):
def test_eq_set(self):
assert set([0, 10, 11, 12]) == set([0, 20, 21])
def test_eq_dict(self):
assert {'a': 0, 'b': 1, 'c': 0} == {'a': 0, 'b': 2, 'd': 0}
def test_eq_list(self):
assert [0, 1, 2] == [0, 1, 3]
def test_eq_longer_list(self):
assert [1,2] == [1,2,3]
1、文本格式的报告
py.test test_report.py --resultlog=d:\log.txt
指定当前路径下生成log.txt文件,打开文件,内容如下:
2、生成JUnitXml格式报告
该格式方便与CI服务器进行集成
py.test test_report.py --junitxml=d:\log.xml
打开生成的log.xml,内容如下:
3、将测试报告发送到pastebin服务器
最后
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!