Pytest权威教程-01安装及入门

左手的ㄟ右手 2023-10-09 10:12 201阅读 0赞

安装及入门

Python支持版本: Python 2.6,2.7,3.3,3.4,3.5,Jython,PyPy-2.3

支持的平台: Unix/Posix and Windows

PyPI包名: pytest

依赖项: py,colorama (Windows)

PDF文档: 下载最新版本文档

Pytest是一个使创建简单及可扩展性测试用例变得非常方便的框架。测试用例清晰、易读而无需大量的繁琐代码。只要几分钟你就可以对你的应用程序或者库展开一个小型的单元测试或者复杂的功能测试。

安装 Pytest

在命令行执行以下命令

  1. pip install -U pytest

检查安装的Pytest版本

  1. $ pytest --version
  2. This is pytest version 3.x.y,imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py

创建你的第一个测试用例

只需要4行代码即可创建一个简单的测试用例:

  1. # test_sample.py文件内容
  2. def func(x):
  3. return x + 1
  4. def test_answer():
  5. assert func(3) == 5

就是这么简单。现在你可以执行一下这个测试用例:

  1. $ pytest
  2. =========================== test session starts ============================
  3. platform linux -- Python 3.x.y,pytest-3.x.y,py-1.x.y,pluggy-0.x.y
  4. rootdir: $REGENDOC_TMPDIR,inifile:
  5. collected 1 item
  6. test_sample.py F [100%]
  7. ================================= FAILURES =================================
  8. _______________________________ test_answer ________________________________
  9. def test_answer():
  10. > assert func(3) == 5
  11. E assert 4 == 5
  12. E + where 4 = func(3)
  13. test_sample.py:5: AssertionError
  14. ========================= 1 failed in 0.12 seconds =========================

由于func(3)并不等于5,这次测试返回了一个失败的结果信息。

注意:
你可以使用assert语句来断言你测试用例的期望结果。Pytest的高级断言内省机制, 可以智能地展示断言表达式的中间结果, 来避免来源于JUnit的方法中的变量名重复问题。

执行多条测试用例

pytest命令会执行当前目录及子目录下所有test_*.py*_test.py格式的文件。一般来说,用例需要遵循标准的测试发现规则。

断言抛出了指定异常

使用raise可以在相应代码的抛出的指定异常:

  1. # test_sysexit.py文件内容
  2. import pytest
  3. def f():
  4. raise SystemExit(1)
  5. def test_mytest():
  6. with pytest.raises(SystemExit):
  7. f()

使用“静默”模式,执行这个测试用例如:

  1. $ pytest -q test_sysexit.py
  2. . [100%]
  3. 1 passed in 0.12 seconds

使用类组织多条测试用例

一旦你需要开发多条测试用例,你可能会想要使用类来组织它们。使用Pytest可以很轻松的创建包含多条用例的测试类:

  1. # test_class.py文件内容
  2. class TestClass(object):
  3. def test_one(self):
  4. x = "this"
  5. assert 'h' in x
  6. def test_two(self):
  7. x = "hello"
  8. assert hasattr(x,'check')

Pytest可以发现所有遵循Python测试用例发现约定规则的用例,所以它能找到Test开头的测试类外以及类中所有以test_开头的函数及方法。测试类无需再继承任何对象。我们只需要简单地通过文件名来运行这个模块即可。

  1. $ pytest -q test_class.py
  2. .F [100%]
  3. ================================= FAILURES =================================
  4. ____________________________ TestClass.test_two ____________________________
  5. self = <test_class.TestClass object at 0xdeadbeef>
  6. def test_two(self):
  7. x = "hello"
  8. > assert hasattr(x,'check')
  9. E AssertionError: assert False
  10. E + where False = hasattr('hello','check')
  11. test_class.py:8: AssertionError
  12. 1 failed,1 passed in 0.12 seconds

第一条用例执行成功,第二天用例执行失败。你可以很容易地通过断言中变量的中间值来理解失败的原因。

函数测试中请求使用独立的临时目录

Pytest提供了内置fixtures方法参数,来使用任意资源,比如一个独立的临时目录:

  1. # test_tmpdir.py文件内容
  2. def test_needsfiles(tmpdir):
  3. print (tmpdir)
  4. assert 0

在测试用例函数使用tmpdir作为参数,Pytest将在测试用例函数调用之前查找并调用fixture工厂方法来创建相应的资源。在测试运行之前,Pytest为每个测试用例创建一个独立的临时目录:

  1. $ pytest -q test_tmpdir.py
  2. F [100%]
  3. ================================= FAILURES =================================
  4. _____________________________ test_needsfiles ______________________________
  5. tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
  6. def test_needsfiles(tmpdir):
  7. print (tmpdir)
  8. > assert 0
  9. E assert 0
  10. test_tmpdir.py:3: AssertionError
  11. --------------------------- Captured stdout call ---------------------------
  12. PYTEST_TMPDIR/test_needsfiles0
  13. 1 failed in 0.12 seconds

有关tmpdir处理的更多信息,请参见: 临时目录和文件

进一步阅读

查看其他pytest文档资源,来帮助你建立自定义测试用例及独特的工作流:

  • “使用pytest -m pytest来调用pyest” - 命令行调用示例
  • “将pytest与原有测试套件一起使用”- 使用之前的测试用例
  • “使用属性标记测试用例” - pytest.mark相关信息
  • “pytest fixtures:显式,模块化,可扩展” - 为你的测试提供函数基准
  • “插件编写” - 管理和编写插件
  • “优质集成实践” - 虚拟环境和测试分层

转载于:https://www.cnblogs.com/superhin/p/11455335.html

发表评论

表情:
评论列表 (有 0 条评论,201人围观)

还没有评论,来说两句吧...

相关阅读