栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python单元测试框架pytest学习总结一:安装、运行第一个测试脚本,以及标记、跳过的使用。

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python单元测试框架pytest学习总结一:安装、运行第一个测试脚本,以及标记、跳过的使用。

pytest框架学习总结:
作为一个小白,首先要先把准备工作做好:
1.安装:pip install pytest
2、 查看是否安装成功:pip list
装好之后就可以开始了。
先了解下pytest单元测试框架的一些规则:
这个框架只要你装了,然后import进来之后,就可以对你的函数,类,模块,进行测试。
那么怎么测试呢,就是你自己除了有要测试的代码(你的函数,类,模块),还要自己写测试代码,什么样的呢,就是得是pytest能够识别的格式:
–》如果是测试文件需要以test_开头(或者以_test结尾)
–》class要以Test开头,且不能有__init__方法
–》函数以test开头
–》所有的包pakege必须要有__init__.py文件

有了以上的规则,有规矩,那么我们就按照这样的规矩写一些我们要测试的点。
我们知道要测试一个东西是不是对的,首先是要有输入,然后有输出,最后就是一个预期,手工测试的话,就是判断输出符不符合预期,这个预期是我们自己知道的,但是你要是用自动化实现,也就是让计算机给你测试,那么你得告诉计算机,你的输入,和你的预期,判断的事交给计算机,那就是用pytest中assert方法来判断。
断言使用assert 方法。

3、查看pytest命令行参数,可以用pytest -h 或pytest --help查看
常用的:
-v 显示详细的信息
-q显示简要的信息
-m显示被mark标记的测试用例
-s 将测试例中的打印函数的内容显示出来

下面给出了一个测试脚本,很简单。
以下是cmd也就是命令行中,我这里是直接在pycharm的terminal窗口中执行的。后面会介绍不使用命令行窗口的方式。
测试第一个脚本:

import pytest
def add(x, y):
    if type(x) is int or type(y) is int:
        return x+y
    else:
        raise TypeError

def testcase1():
    print("正常值。。。")
    assert add(1,2) == 3

def testcase2():
    print("异常值")
    with pytest.raises(TypeError):  
        assert add(1,"2") != 3

def testCase3():
    print("特殊字符:")
    with pytest.raises(TypeError):
        assert add("$", "****") != 3

执行用例有以下几种方法:
①在终端:执行pytest (这种是不指定具体的测试名称,默认从当前目录以及子目录中查找符合规则的测试用例进行执行) 或者pytest 指定文件名(可以直接指定文件名,如果是类名则需要文件名::类名,如果是方法,则还需要文件名::类名::方法名,如果是函数名,文件名::函数名)

(venv) D:自动化Bpython_test>pytest -s  -vv test_10_21.py
=========================================================================== test session starts ============================================================================
platform win32 -- Python 3.6.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- c:python36python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.5', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '6.1.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.22'
, 'assume': '2.3.3', 'html': '3.1.0', 'metadata': '1.11.0', 'pythonpath': '0.7.3'}}
rootdir: D:自动化Bpython_test
plugins: allure-pytest-2.8.22, assume-2.3.3, html-3.1.0, metadata-1.11.0, pythonpath-0.7.3
collected 3 items                                                                                                                                                           

test_10_21.py::test_case1 正常值。。。
PASSED
test_10_21.py::test_case2 异常值
PASSED
test_10_21.py::test_Case3 特殊字符:
PASSED

============================================================================ 3 passed in 0.04s =============================================================================

②通过关键字匹配的方式,需要指定参数-k
比如test_10_21.py文件下面有几个测试case,是函数,分别是:test_a 、test_ab、test_c,那么如果想执行前面两个,可以pytest -k “a” test_10_21.py,就是执行文件下类名、函数名、方法名中有a的,都执行,不区分大小写。
③通过标记,默认情况下,pytest如果不指定执行的文件,那么会把当前目录下所有的符合规则的文件都执行了,但是有些情况下,比如回归测试,我们只想执行部分,那么可以使用标记函数进行选择性的执行。指定参数-m,并且需要在测试例的头部使用装饰器@pytest.mark.标记名,来进行标记:

import pytest
def add(x, y):
    if type(x) is int or type(y) is int:
        return x+y
    else:
        raise TypeError
@pytest.mark.notToDo
def test_case1():
    print("正常值。。。")
    assert add(1,2) == 3
    print("正常值。。。")
@pytest.mark.toDo
def test_case2():
    print("异常值")
    with pytest.raises(TypeError):
        assert add(1,"2") != 3
@pytest.mark.toDo
def test_Case3():
    print("特殊字符:")
    with pytest.raises(TypeError):
        assert add("$", "****") != 3

if __name__ == '__main__':
    pytest.main(['-s','-m','toDo','test_10_21.py'])

运行结果:

C:Python36python.exe D:/自动化B/python_test/test_10_21.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:自动化Bpython_test
plugins: allure-pytest-2.8.22, assume-2.3.3, html-3.1.0, metadata-1.11.0, pythonpath-0.7.3
collected 3 items / 1 deselected / 2 selected

test_10_21.py 异常值
.特殊字符:
.

============================== warnings summary ===============================
test_10_21.py:7
  D:自动化Bpython_testtest_10_21.py:7: PytestUnknownMarkWarning: Unknown pytest.mark.notToDo - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.notToDo

test_10_21.py:12
  D:自动化Bpython_testtest_10_21.py:12: PytestUnknownMarkWarning: Unknown pytest.mark.toDo - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.toDo

test_10_21.py:17
  D:自动化Bpython_testtest_10_21.py:17: PytestUnknownMarkWarning: Unknown pytest.mark.toDo - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
    @pytest.mark.toDo

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================= 2 passed, 1 deselected, 3 warnings in 0.18s =================

Process finished with exit code 0

可以看到一个被过滤掉了,没有执行。
④对指定的包进行执行:
pytest --pyargs pkg.testing
这将导入pkg.testing并使用其文件系统位置来查找和运行测试。(这个我没有看懂啊。。。。)

⑤另外,pytest还可以指定参数当用例执行的过程中遇到错误,可以停止运行,后续的测试用例将不会被执行:-x 参数
pytest -x test_10_21.py

⑥–maxfail=num:这个参数的功能是指定当执行用例的错误个数达到num,那么后面的都不会继续执行:
pytest --maxfail=1 test_10_21.py

下面介绍pycharm中不使用命令行窗口的方式执行:
①在执行的文件中加入下面的语句,且需要import pytest:
if name == ‘main’:
pytest.main([’-sv’, ‘test_10_21.py’])
上面是执行了文件名,如果不指定,则会执行这个目录下以及其子目录下所有的满足条件的用例。

C:Python36python.exe D:/自动化B/python_test/test_10_21.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:自动化Bpython_test
plugins: allure-pytest-2.8.22, assume-2.3.3, html-3.1.0, metadata-1.11.0, pythonpath-0.7.3
collected 3 items

test_10_21.py 正常值。。。
.异常值
.特殊字符:
.

============================== 3 passed in 0.05s ==============================

Process finished with exit code 0

上面的mark是指定哪些需要执行,但是一般我们不执行的比较少的话,可以指定不执行哪些。
pytest中使用pytest.mark.skip就可以实现。

import pytest
def add(x, y):
    if type(x) is int or type(y) is int:
        return x+y
    else:
        raise TypeError
# @pytest.mark.ToDo3
def test_case1():
    print("正常值。。。")
    assert add(1,2) == 3
    print("正常值。。。")
# @pytest.mark.toDo2
def test_case2():
    print("异常值")
    with pytest.raises(TypeError):
        assert add(1,"2") != 3
@pytest.mark.skip(reason = "我不想被执行")
def test_Case3():
    print("特殊字符:")
    with pytest.raises(TypeError):
        assert add("$", "****") != 3

# if __name__ == '__main__':
#     pytest.main(['-s','-m','toDo','test_10_21.py'])

运行结果:

(venv) D:自动化Bpython_test>pytest -s  -v  test_10_21.py
=========================================================================== test session starts ============================================================================
platform win32 -- Python 3.6.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- c:python36python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.5', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '6.1.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.22'
, 'assume': '2.3.3', 'html': '3.1.0', 'metadata': '1.11.0', 'pythonpath': '0.7.3'}}
rootdir: D:自动化Bpython_test
plugins: allure-pytest-2.8.22, assume-2.3.3, html-3.1.0, metadata-1.11.0, pythonpath-0.7.3
collected 3 items                                                                                                                                                           

test_10_21.py::test_case1 正常值。。。
正常值。。。
PASSED
test_10_21.py::test_case2 异常值
PASSED
test_10_21.py::test_Case3 SKIPPED

======================================================================= 2 passed, 1 skipped in 0.15s =======================================================================

pytest还支持跳过的时候根据判断进行是否跳过:

count = 1
@pytest.mark.skipif(count <= 1, reason = "count值太小")
def test_case2():
    print("异常值")
    with pytest.raises(TypeError):
        assert add(1,"2") != 3
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/339348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号