python测试框架之pytest

打印 上一主题 下一主题

主题 1840|帖子 1840|积分 5530

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pytest 根本使用

pytest官方文档/手册
pytest安装

pip install -U pytest
pytest的测试case收集规则

Conventions for Python test discovery


  • 如果未指定有关path的下令行参数,则从当前目次开始查找test case。
  • 如果下令指定了查找目次,则会递归的查找这些目次下的文件,除非目次名与norecursedirs中的目次名匹配
    在这些目次中,搜刮以测试包名开头的文件,比方test_*.py和*_test.py;test_是测试包名的前缀,*_test.py是测试文件的后缀名;
    在类之外,带有test_前缀的测试函数或方法;
    在带有前缀(test/Test)的测试类中带有前缀test_的测试函数或方法;带有@staticmethod和@classmethod装饰器的方法也会被收集
自定义测试case收集规则
继续unittest.TestCase 来实现
待补充。。。
pytest - fixture的使用

“requesting” fixtures
  1. import pytest
  2. class Fruit:
  3.     def __init__(self, name):
  4.         self.name = name
  5.         self.cubed = False
  6.         
  7.     def cube(self):
  8.         self.cubed = True
  9. class FruitSalad:
  10.     def __init__(self, *fruit_bowl):
  11.         self.fruit = fruit_bowl
  12.         self._cube_fruit()
  13.     def _cube_fruit(self):
  14.         for fruit in self.fruit:
  15.             fruit.cube()
  16. @pytest.fixture
  17. def fruit_bowl():
  18.     return [Fruit("apple"), Fruit("banana")]
  19. '''
  20. 当pytest执行测试函数(test_fruit_salad)时,会检查测试函数签名中的参数(fruit_bowl),并查找与参数同名的fixture对象(fruit_bowl()),pytest一旦找到它们,就会执行fixture对象,并捕获返回结果,然后将结果作为参数传入测试函数
  21. '''
  22. def test_fruit_salad(fruit_bowl):
  23.     # Act
  24.     fruit_salad = FruitSalad(*fruit_bowl)
  25.     # Assert
  26.     assert all(fruit.cubed for fruit in fruit_salad.fruit)    # def all(__iterable: Iterable[object]) -> bool ,Return True if bool(x) is True for all values x in the iterable.
复制代码
Fixtures can request other fixtures
  1. import pytest
  2. @pytest.fixture
  3. def first_entry():
  4.     return "a"
  5. @pytest.fixture
  6. def order(first_entry):
  7.     return [first_entry]   #  first_entry 的为 a
  8. def test_string(order):                # order的值为 ["a"]
  9.     order.append("b")
  10.     assert order == ["a", "b"]
  11. '''
  12. pytest执行test_sting时,会查找与参数order同名的fixture对象:order(first_entry),然后执行函数order(first_entry),在执行order时,再查找与参数first_entry同名的fixture对象:first_entry(),然后捕获结果:'a',作为参数传递给order函数,再捕获order的函数返回值:['a'],作为参数传递给test_string
  13. '''
复制代码
fixture是可以重复使用的
一个test/fixture同时可以request多余一个fixture
  1. import pytest
  2. @pytest.fixture
  3. def first_entry():
  4.     return "a"
  5. @pytest.fixture
  6. def second_entry():
  7.     return 2
  8. @pytest.fixture
  9. def order(first_entry, second_entry):
  10.     return [first_entry, second_entry]
  11. @pytest.fixture
  12. def expected_list():
  13.     return ["a", 2, 3.0]
  14. def test_string(order, expected_list):
  15.     order.append(3.0)
  16.     assert order == expected_list
复制代码
在一次测试中可以多次请求固定数据(返回值会被缓存)
  1. import pytest
  2. @pytest.fixture
  3. def first_entry():
  4.     return "a"
  5. @pytest.fixture
  6. def order():
  7.     return []
  8. @pytest.fixture
  9. def append_first(order, first_entry):
  10.     return order.append(first_entry)
  11. def test_string_only(append_first, order, first_entry):
  12.         # print(append_first)     # None
  13.     # print(order)            # ['a']
  14.     # print(first_entry)      # 'a'
  15.     assert order == [first_entry]
复制代码
自动实行fixture
  1. import pytest
  2. @pytest.fixture
  3. def first_entry():
  4.     return "a"
  5. @pytest.fixture
  6. def order(first_entry):
  7.     return []
  8. @pytest.fixture(autouse=True)
  9. def append_first(order, first_entry):
  10.     return order.append(first_entry)
  11. def test_string_only(order, first_entry):
  12.     assert order == [first_entry]
  13. def test_string_and_int(order, first_entry):
  14.     order.append(2)
  15.     assert order == [first_entry, 2]
复制代码
scope:可以在类、模块、包、会话之间共享fixtures资源
  1. # aa.py
  2. import smtplib
  3. import pytest
  4. @pytest.fixture(scope="module")
  5. def smtp_connection():
  6.     return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
  7. # ======================================================
  8. # test_aa.py
  9. def test_ehlo(smtp_connection):
  10.     response, msg = smtp_connection.ehlo()
  11.     assert response == 250
  12.     assert b"smtp.gmail.com" in msg
  13.     assert 0  # for demo purposes
  14. def test_noop(smtp_connection):
  15.     response, msg = smtp_connection.noop()
  16.     assert response == 250
  17.     assert 0  # for demo purposes
复制代码
fixture 的作用范围
Fixtures are created when first requested by a test, and are destroyed based on their :scope


  • function: the default scope, the fixture is destroyed at the end of the test.
  • class: the fixture is destroyed during teardown of the last test in the class.
  • module: the fixture is destroyed during teardown of the last test in the module.
  • package: the fixture is destroyed during teardown of the last test in the package where the fixture is defined, including sub-packages and sub-directories within it.
  • session: the fixture is destroyed at the end of the test session.
factories as fixturesparametrizing fixturesUsing marks with parametrized fixtures等其他用法以后补充。。。
skip and xfail

待补充。。。
pytest - 属性标记测试函数

待补充。。。
pytest - 参数化测试

待补充。。。
pytest - mock/monkeypatch的使用

待补充。。。
pytest - 运行方式

pytest - 运行方式/下令

忽略某些测试的方式:在下令行里用 --ignore=path 参数
  1. usage: pytest [options] [file_or_dir] [file_or_dir] [...]
复制代码
待补充。。。
pytest - 处置惩罚测试失败的case

待补充。。。
pytest - 测试输出

待补充。。。
捕获 stdout/stderr输出

待补充。。。
捕获警告信息

待补充。。。
pytest - 测试报告

待补充。。。
pytest - 自带的测试报告

待补充。。。
pytest + allure测试报告

待补充。。。
pytest - 进阶使用

安装和使用插件

待补充。。。
编写插件

待补充。。。
编写hook函数

待补充。。。
pytest与现有测试框架 联合

待补充。。。
pytest调用基于unittest的测试case

待补充。。。
实现xunit样式的设置

待补充。。。
设置bash

待补充。。。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表