Pytest(1)Pytest自动化测试框架快速入门

Pytest(1)Pytest自动化测试框架快速入门,第1张

Pytest(1)Pytest自动化测试框架快速入门 目录 一、Pytest的特点 二、Pytest的安装 三、创建第一个Pytest测试脚本 四、Pytest执行多个测试脚本 五、Pytest对引发异常类型的断言 六、Pytest使用类组织测试函数 正文 一、Pytest的特点 1.1、简单

说pytest简单是因为一方面pytest执行起来非常简单,另一方面pytest可以像unittest那样仅仅使用一些简单的功能就可以完成一大部分的工作

1.2、开源

pytest是完全开源的,源代码存放于github,见 pytest源代码

1.3、支持参数化

pytest支持参数化,主要用于基于数据驱动的自动化测试场景

1.4、可生成优美的报告

pytest与allure结合可以产生非常漂亮的html报告

1.5、兼容性好

pytest可以执行由nose或者unittest编写的自动化测试脚本

1.6、丰富的插件

pytest拥有丰富的插件,同时可以自定义扩展插件

1.7、功能完善

pytest有丰富的功能,如用例失败重跑、并发执行、标记标签、标记失败等等

1.8、有良好的开源生态

pytest有丰富的文档,开源社区活跃,很方便的与jenkins等持续集成工具结合

1.9、执行方式灵活

pytest提供了多种调用自动化脚本执行的方式,能满足不同场景下的执行需求

二、Pytest的安装 2.1、安装

使用如下命令即可完成安装

pip install pytest
2.2、升级

使用如下命令可以查看pytest的版本

pytest --version

如下,表示已经安装成功了

2.3、卸载

执行如下命令即可下载pytest

pip uninstall pytest
三、创建第一个Pytest测试脚本 3.1、四行代码写一个测试脚本

如下,创建一个名为test_sample.py的文件,然后在文件中编写如下四行代码,即完成第一个pytest测试脚本的编写,脚本内容非常简单,这里就不再赘述了

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5
3.2、第一个pytest测试脚本执行

(1)在cmd窗口进入当前文件所在的目录下,执行执行pytest命令即可执行,结果如下:

$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item

test_sample.py F                                                     [100%]

================================= FAILURES =================================
_______________________________ test_answer ________________________________

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

test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================

很明显,这里4不等于5,因此结果报错了,最下面有详细的执行结果

四、Pytest执行多个测试脚本

pytest默认的发现脚本的规则还是挺复杂的,这里先暂时记住pytest会在当前目录以及递归的到对当前目录的子目录中寻找test_.py或者_test.py文件,然后在这些文件中寻找test_开头的函数即可,详细的默认的用例发现规则后续再详细展开

五、Pytest对引发异常类型的断言

(1)通过pytest.raise可以对代码中引发异常的类型进行断言,创建一个名为test_sysexit.py文件,编写测试脚本如下:

import pytest


def f():
    raise SystemExit(1)


def test_mytest():
    with pytest.raises(SystemExit):
        f()

执行结果如下,即代码中产生的异常与断言的结果一致,用例通过

$ pytest -q test_sysexit.py
.                                                                    [100%]
1 passed in 0.12s
六、Pytest使用类组织测试函数 6.1、使用类组织测试函数

如下,编写以Test开头的类,此外此类不能有__init__初始化函数,然后在类中编写test_开头的方法即为测试函数,

# 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")

执行结果如下:

$ pytest -q test_class.py
.F                                                                   [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________

self = 

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

test_class.py:8: AssertionError
========================= short test summary info ==========================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
1 failed, 1 passed in 0.12s
6.2、使用类组织测试函数的好处
  • 可以有效的组织用例
  • 可以仅在类中共享fixture
  • 可以在类上打标签从而对类中的所有用例打标签
6.3、使用类组织用例的注意点

使用类组织用例需要注意的是,类中的每个用例都是测试类的一个独立对象,类中的用例如果通过类属性共享变量将是非常糟糕的设计,如下便是例子

# content of test_class_demo.py
class TestClassDemoInstance:
    value = 0

    def test_one(self):
        self.value = 1
        assert self.value == 1

    def test_two(self):
        assert self.value == 1

执行结果如下:

$ pytest -k TestClassDemoInstance -q
.F                                                                   [100%]
================================= FAILURES =================================
______________________ TestClassDemoInstance.test_two ______________________

self = 

    def test_two(self):
>       assert self.value == 1
E       assert 0 == 1
E        +  where 0 = .value

test_class_demo.py:9: AssertionError
========================= short test summary info ==========================
FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0 == 1
1 failed, 1 passed in 0.12s

至此,对pytest已经有了一个简单的了解,并且能简单的编写一个测试用例进行测试了,甚至可以通过类去编写多个测试用例进行简单的测试了,更多精彩内容,请关注后续持续更新

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5670380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存