2

Pytest系列(一)初次了解 - 北漂的雷子

 2 years ago
source link: https://www.cnblogs.com/leiziv5/p/16133371.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pytest系列(一)初次了解

在之前,我分享过unittest系列,后来有很多人问我,能不能出pytest的教程,正好最近在整理pytest相关的资料,那么,就趁着这个机会,去和大家分享一下pytest系列。

 pytest是一个非常成熟的全功能的Python测试框架。那么他有什么优点呢。

pytest优点
入门简单,文档丰富
支持单元测试,功能测试
支持参数化,重复执行,部分执行,测试跳过
兼容其他测试框架(nose,unittest 等)
支持生成html报告
可集成CI环境(Jenkins 等)
第三方插件丰富,良好的自定义扩展性

   那么我们对比下它与unittest。

(1)unittest
测试文件必须先 import unittest
测试类必须继承unittest.TestCase
测试方法必须以“test_”开头
测试类必须要有unittest.main()方法
unittest只有setup/teardown装载测试用例
(2)pytest
测试文件名必须以“test_”开头
测试类以Test开头,并且不能带有 init 方法
测试方法必须以“test_”开头
除了有setup/teardown,还能更自由的定义fixture装载测试用例

  那么我们看下它支持的python

Python支持版本: Python 2.6,2.7,3.3+
支持的平台: Unix/mac and Windows
PyPI包名: pytest

          如何安装,很简单

pip install -U pytest

        安装完毕后

 我们可以在安装的时候,看到它的版本,也可以使用

pytest --version

如何快速写一个pytest用例呢,很简单

def add(a:int,b:int):
    return a+b
def test_answer():
    assert add(3,4) == 7
pytest py_test_one.py

用例执行通过,如果用例失败呢?

def add(a:int,b:int):
    return a+b
def test_answer():
    assert add(3,4) == 8

  测试失败输出

  一个简单的小的入门的demo就完成了。

文章首发在公众号上,欢迎关注


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK