首页 > 其他 > 详细

pytest官网文档の第一章:安装和快速开始

时间:2019-10-12 12:12:44      阅读:71      评论:0      收藏:0      [点我收藏+]

1.1 安装

  • 安装命令
pip install pytest

技术分享图片

 

 

  • 查看版本
pytest --version

技术分享图片

 

 

 1.2 创建第一个测试函数

  • 测试代码如下:
# -*- coding: utf-8 -*-
# 开发人员   :RayChou
# 开发时间   :2019-10-12  10:27
# 文件名称   :test_simple.PY
# 开发工具   :PyCharm
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5
  • 执行结果如下:
(venv) D:\workspace\demo1\test_module>pytest
=========================================================================================================================== test session starts ===========================================================================================================================
platform win32 -- Python 3.6.5, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: D:\workspace\demo1\test_module
collected 1 item

test_simple.py F                                                                                                                                                                                                                                                     [100%]

================================================================================================================================ FAILURES =================================================================================================================================
_______________________________________________________________________________________________________________________________ test_answer _______________________________________________________________________________________________________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_simple.py:10: AssertionError
============================================================================================================================ 1 failed in 0.12s ============================================================================================================================

(venv) D:\workspace\demo1\test_module>

 1.3 执行多个测试用例

pytest通过检测test_module模块下以test_xxx.py和xxx_test.py命名的文件,并识别以test_xxx命名的函数并执行。所以如果一个目录下有多个符合命名规则的文件,则会批量加入执行队列;

 1.4 断言特定异常

  • 测试代码
# -*- coding: utf-8 -*-
# 开发人员   :RayChou
# 开发时间   :2019-10-12  10:39 
# 文件名称   :test_sysexit.PY
# 开发工具   :PyCharm
import pytest

def f():
    raise SystemExit(1)

def test_mytest():
    with pytest.raises(SystemExit):
        f()
  • 使用安静模式(添加参数q,quiet)执行
(venv) D:\workspace\demo1\test_module>pytest -q test_sysexit.py
.                                                                                                                                                                                                                                                                    [100%]
1 passed in 0.03s

(venv) D:\workspace\demo1\test_module>

 

 1.5 同一个类中组合多个测试用例

  • 测试代码
# -*- coding: utf-8 -*-
# 开发人员   :RayChou
# 开发时间   :2019-10-12  10:46 
# 文件名称   :test_class.PY
# 开发工具   :PyCharm
class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")
  • 执行结果
(venv) D:\workspace\demo1\test_module>pytest -q test_class.py
.F                                                                                                                                                                                                                                                                   [100%]
================================================================================================================================ FAILURES =================================================================================================================================
___________________________________________________________________________________________________________________________ TestClass.test_two ____________________________________________________________________________________________________________________________

self = <test_module.test_class.TestClass object at 0x0000000003463E10>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, "check")
E       AssertionError: assert False
E        +  where False = hasattr(hello, check)

test_class.py:13: AssertionError
1 failed, 1 passed in 0.11s

(venv) D:\workspace\demo1\test_module>

 1.6 功能测试中获取一个唯一的临时路径

  • 执行代码
# -*- coding: utf-8 -*-
# 开发人员   :RayChou
# 开发时间   :2019-10-12  11:00 
# 文件名称   :test_tmpdir.PY
# 开发工具   :PyCharm

def test_needfiles(tmpdir):
    print(tmpdir)
    assert 0
  • 执行结果
(venv) D:\workspace\demo1\test_module>pytest test_tmpdir.py
=========================================================================================================================== test session starts ===========================================================================================================================
platform win32 -- Python 3.6.5, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: D:\workspace\demo1\test_module
collected 1 item

test_tmpdir.py F                                                                                                                                                                                                                                                     [100%]

================================================================================================================================ FAILURES =================================================================================================================================
_____________________________________________________________________________________________________________________________ test_needfiles ______________________________________________________________________________________________________________________________

tmpdir = local(‘C:\\Users\\raychou\\AppData\\Local\\Temp\\pytest-of-raychou\\pytest-6\\test_needfiles0‘)

    def test_needfiles(tmpdir):
        print(tmpdir)
>       assert 0
E       assert 0

test_tmpdir.py:9: AssertionError
-------------------------------------------------------------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------------------------------------------------------------
C:\Users\zhoulei17344\AppData\Local\Temp\pytest-of-zhoulei17344\pytest-6\test_needfiles0
============================================================================================================================ 1 failed in 0.12s ============================================================================================================================

(venv) D:\workspace\demo1\test_module>

从执行结果中我们可以知道当传入tmpdir时,pytest会在执行程序前,会为程序申请资源并且创建一个唯一的临时路径。

 

pytest官网文档の第一章:安装和快速开始

原文:https://www.cnblogs.com/raychou1995/p/11660338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!