美文网首页软件测试
pytest学习笔记

pytest学习笔记

作者: 梵音11 | 来源:发表于2020-07-11 16:46 被阅读0次


一、介绍

pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:

1、简单灵活,容易上手;

2、支持参数化;

3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);

4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;

5、测试用例的skip和xfail处理;

6、可以很好的和jenkins集成;

二、安装

  pip install -U pytest 

  pip install -U pytest-html

  pip install -U pytest-rerunfailures

此外还有很多很好的第三方插件,请到http://plugincompat.herokuapp.com/ 和 https://pypi.python.org/pypi?%3Aaction=search&term=pytest-&submit=search 查找

三、例子

这里列几个pytest-document中的例子

1、默认执行当前目录下的所有以test_为前缀(test_*.py)或以_test为后缀(*_test.py)的文件中以test_为前缀的函数

import pytest

# content of test_sample.py

def func(x):

    return x + 1

def test_answer():

    assert func(3) == 5

运行 py.test  或 指定特定文件 py.test -q test_sample.py

2、使用类来组成多个用例的

import pytest

# content of test_class.py

class TestClass:

    def test_one(self):

        x = "this"

    assert 'h' in x

    def test_two(self):

        x = "hello"

        assert hasattr(x, 'check')

3、在python中调用pytest: python test_class.py

import pytest

# content of test_class.py

class TestClass:

    def test_one(self):

        print 'one'

        x = "this"

        assert 'h' in x

    def test_two(self):

        print 'two'

        x = "hello"

        assert hasattr(x, 'check')

if __name__ == '__main__':

    pytest.main("-q --html=a.html")

4、来个支持参数化的例子,参数化使用pytest.mark.parametrize的参数,第一个为变量的元组,第二个是变量赋值的元组列表,具体下面的章节会仔细介绍

# content of test_time.py

import pytest

from datetime import datetime, timedelta

testdata = [

(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),

(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),

]

@pytest.mark.parametrize("a,b,expected", testdata)

def test_timedistance_v0(a, b, expected):

    diff = a - b

    assert diff == expected

@pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])

def test_timedistance_v1(a, b, expected):

    diff = a - b

    assert diff == expected

def idfn(val):

    if isinstance(val, (datetime,)):

    # note this wouldn't show any hours/minutes/seconds

        return val.strftime('%Y%m%d')

@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)

def test_timedistance_v2(a, b, expected):

    diff = a - b

    assert diff == expected

 创建了一个测试交流群,如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加测试交流群:829792258,还会有同行一起技术交流

相关文章

  • Pytest学习笔记(非完整版)

    Pytest学习笔记 记录下pytest官方文档的阅读笔记,以便后续参考使用。非完整版,个人理解为主,难免有误,望...

  • pytest学习笔记

    一、介绍 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1、简单灵活,容易上手;...

  • pytest学习笔记

    使用环境 python3 pycharm pytest pytest编写规范 测试文件以test_开头或以_tes...

  • pytest框架学习-基础入门

    查看所有Python相关学习笔记 pytest学习 一、相关内容的下载安装 1.1 基础安装 1.2 html报告...

  • pytest学习笔记--(一)

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

  • pytest学习笔记--(2)

    继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... ...

  • pytest学习笔记---3

    接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一、pytest的参数化 1...

  • pytest学习博客列表

    本来打算自己写pytest的一些内容,但是在学习pytest过程中,发现了几个不错的pytest学习系列,所以就不...

  • Python_pytest学习笔记

    第一节课pytest基本介绍 第二节课pytest配置1.命令行参数 addopts = -s —reruns n...

  • 学习笔记-Pytest(一)简介

    1. pytest简介 pytest是python的一种单元测试框架,与python自带的unittest测试框架...

网友评论

    本文标题:pytest学习笔记

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