美文网首页
[接口测试_B] 02 Pytest的简单示例

[接口测试_B] 02 Pytest的简单示例

作者: 乐大爷L | 来源:发表于2018-04-01 20:06 被阅读215次

Pytest是什么

Pytest是Python的一个测试工具,可以用于所有类型和级别的软件测试。Pytest是一个可以自动查找到你编写的用例并运行后输出结果的测试框架。

Pytest有什么特点

  • pytest是一个命令行工具
  • pytest可以扩展第三方插件
  • pytest易于持续集成和应用于web自动化测试
  • pytest编写用例简单,并具有很强的可读性
  • pytest可以直接采用assert进行断言,不必采用self.assertEqual()等
  • pytest可以运行unittest编写的用例
  • pytest可以运行以testtest开头或结尾的包、文件和方法

Pytest的简单示例

# test_simple.py
import requests

def test_one():
    r = requests.get('https://api.github.com/events')
    assert r.status_code == 200

运行测试用例可以直接在命令行中执行该py文件 pytest test_simple.py

(python_interface_base) F:\python_interface_test\python_interface_test\test_requests>pytest test_simple.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: F:\python_interface_test\python_interface_test\test_requests, inifile:
collected 1 item

test_simple.py .                                                         [100%]

========================== 1 passed in 3.26 seconds ===========================

运行py文件中的单个用例

# test_simple.py
import requests

def test_one():
    r = requests.get('https://api.github.com/events')
    assert r.status_code == 200

def test_two():
    r = requests.get('https://api.github.com/events')
    assert r.encoding == 'utf'

运行py文件中的单个用例时,可以采用命令pytest test_simple.py::test_two

(python_interface_base) F:\python_interface_test\python_interface_test\test_requests>pytest test_simple.py::test_two
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: F:\python_interface_test\python_interface_test\test_requests, inifile:
collected 1 item

test_simple.py F                                                         [100%]

================================== FAILURES ===================================
__________________________________ test_two ___________________________________

    def test_two():
        r = requests.get('https://api.github.com/events')
>       assert r.encoding == 'utf'
E       AssertionError: assert 'utf-8' == 'utf'
E         - utf-8
E         ?    --
E         + utf

test_simple.py:9: AssertionError
========================== 1 failed in 1.56 seconds ==========================

由于断言失败,从结果中可以看到失败的具体原因。

相关文章

  • [接口测试_B] 02 Pytest的简单示例

    Pytest是什么 Pytest是Python的一个测试工具,可以用于所有类型和级别的软件测试。Pytest是一个...

  • pytest使用

    使用介绍2.1. 安装pip install pytest 2.2. 示例代码编写规则编写pytest测试样例非常...

  • [接口测试_B] 07 Pytest的测试报告

    pytest命令行运行时,可以直接在控制台中查看到输出的结果,但这样的结果并不直观,也不易于保存用于后续分析和分享...

  • pytest学习笔记--(一)

    这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+re...

  • Python测试框架 - Pytest

    一 . Pytest 简介 Pytest是python的一种单元测试框架。 1. pytest 特点 入门简单,文...

  • unittest-前后置和断言

    unittest框架是Python内置的单元测试框架,可以用来做单元测试,接口测试,UI自动化测试。 简单示例 针...

  • pytest-django简单使用记录

    pytest是一款简单的测试库,方便对于自己的代码进行单元测试,简单的学习使用了一下。 安装 pytest-dja...

  • [接口测试_B] 05 Pytest参数化处理

    pytest的参数化方式 pytest.fixture()方式进行参数化,fixture装饰的函数可以作为参数传入...

  • 接口自动化框架_pytest+jenkins+allure

    本接口测试框架使用python语言实现,基于pytest测试框架,同时集成Jenkins和Allure 核心特性 ...

  • pytest demo

    使用pytest与allure实现一个简单的测试demo python的一个测试框架pytest 首先,实现一个简...

网友评论

      本文标题:[接口测试_B] 02 Pytest的简单示例

      本文链接:https://www.haomeiwen.com/subject/olyacftx.html