• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python - pytest 测试框架(二)

武飞扬头像
lixiang5453
帮助2

pytest fixture


pytest支持以xUnit格式型的测试模型(setup/teardown),但还与python自带的unittest还是有一点差别,如下

  1. 模块形式----使用setup_module/teardown_module  
  2. 函数形式----使用setup_function/teardown_function
  3. 类形式----使用setup_class/teardown_class
  4. 方法形式---使用setup_method/teardown_method

注意:

1.pytest也可以直接运行unittest模式的测试用例

2.如果你在pytest模式中使用setupClass()函数是不行的,不会识别,但如果用例类继承之unittest.Testcase,还是可以识别的

1、fixture scope的范围参数

之前使用@pytest.fixture(scope='module')来定义框架,scope的参数有以下几种

  • function   每一个用例都执行
  • class        每个类执行
  • module     每个模块执行(函数形式的用例)
  • session     每个session只运行一次,在自动化测试时,登录步骤可以使用该session
  1.  
    import os
  2.  
     
  3.  
    import pytest
  4.  
     
  5.  
    """
  6.  
    fixtures 前置条件/后置条件
  7.  
    fixture的作用范围
  8.  
     
  9.  
    fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function
  10.  
     
  11.  
    -function:每一个函数或方法都会调用
  12.  
     
  13.  
    -class:每一个类调用一次,一个类中可以有多个方法
  14.  
     
  15.  
    -module:每一个.py文件调用一次,该文件内又有多个function和class
  16.  
     
  17.  
    -session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module,需要使用 session 需要在当前的 文件下新建一个 conftest.py,将 fixture 写在这个文件中
  18.  
    """
  19.  
     
  20.  
     
  21.  
    @pytest.fixture
  22.  
    def login():
  23.  
    print("token!!!!!!")
  24.  
    return "token1128"
  25.  
     
  26.  
     
  27.  
    # fixtures 自定义作用域时使用 scope 来指定即可
  28.  
    @pytest.fixture(scope="class")
  29.  
    def db_connect():
  30.  
    print("db_connect")
  31.  
    return "console"
  32.  
     
  33.  
     
  34.  
    # fixtures 也可以自动调用,使用 autouse=True ,但这时候就没有返回值
  35.  
    @pytest.fixture(scope="module", autouse=True)
  36.  
    def check_dir():
  37.  
    if not os.path.exists("imgs"):
  38.  
    os.mkdir("imgs")
  39.  
    # yield 表示后置条件的开始,下边的代码就是后置条件,如果有返回值的话,写在 yield 后边
  40.  
    yield 1111
  41.  
    os.rmdir("imgs")
  42.  
    print("用例执行后删除了 imgs 目录")
  43.  
     
  44.  
     
  45.  
    class TestUser:
  46.  
     
  47.  
    # def setup_class(self):
  48.  
    # token = login()
  49.  
    # self.token = token
  50.  
    # print("获取token")
  51.  
     
  52.  
    def test_add_user(self, login, ):
  53.  
    print("test_add_user", login)
  54.  
     
  55.  
    def test_delete_user(self, login):
  56.  
    print("test_delete_user", login)
  57.  
     
  58.  
    def test_modify_user(self, login, db_connect):
  59.  
    print("test_modify_user", login)
  60.  
     
  61.  
     
  62.  
    class TestOrder:
  63.  
    def test_order_user(self, login):
  64.  
    print("test_order_user", login)
  65.  
     
  66.  
    def test_delete_order(self, login):
  67.  
    print("test_delete_order", login)
  68.  
     
  69.  
    def test_update_order(self, login):
  70.  
    print("test_update_order", login)
  71.  
     
  72.  
     
  73.  
    if __name__ == '__main__':
  74.  
    pytest.main(["-vs", __file__])
学新通

运行结果:

  1.  
    /Users/zhulixiang/Downloads/pythonProject/thz/venv/bin/python "/Users/zhulixiang/Downloads/pythonProject/thz/Day11/pytest_fixtures 使用.py"
  2.  
    ============================= test session starts ==============================
  3.  
    platform darwin -- Python 3.7.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- /Users/zhulixiang/Downloads/pythonProject/thz/venv/bin/python
  4.  
    cachedir: .pytest_cache
  5.  
    metadata: {'Python': '3.7.5', 'Platform': 'Darwin-21.1.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.5', 'py': '1.10.0', 'pluggy': '1.0.0'}, 'Plugins': {'PyTestReport': '0.2.1', 'metadata': '1.11.0', 'json-report': '1.4.1', 'ordering': '0.6', 'Faker': '9.5.1'}, 'JAVA_HOME': '/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home'}
  6.  
    rootdir: /Users/zhulixiang/Downloads/pythonProject/thz/Day11
  7.  
    plugins: PyTestReport-0.2.1, metadata-1.11.0, json-report-1.4.1, ordering-0.6, Faker-9.5.1
  8.  
    collecting ... collected 6 items
  9.  
     
  10.  
    pytest_fixtures 使用.py::TestUser::test_add_user token!!!!!!
  11.  
    test_add_user token1128
  12.  
    PASSED
  13.  
    pytest_fixtures 使用.py::TestUser::test_delete_user token!!!!!!
  14.  
    test_delete_user token1128
  15.  
    PASSED
  16.  
    pytest_fixtures 使用.py::TestUser::test_modify_user db_connect
  17.  
    token!!!!!!
  18.  
    test_modify_user token1128
  19.  
    PASSED
  20.  
    pytest_fixtures 使用.py::TestOrder::test_order_user token!!!!!!
  21.  
    test_order_user token1128
  22.  
    PASSED
  23.  
    pytest_fixtures 使用.py::TestOrder::test_delete_order token!!!!!!
  24.  
    test_delete_order token1128
  25.  
    PASSED
  26.  
    pytest_fixtures 使用.py::TestOrder::test_update_order token!!!!!!
  27.  
    test_update_order token1128
  28.  
    PASSED用例执行后删除了 imgs 目录
  29.  
     
  30.  
     
  31.  
    ============================== 6 passed in 0.07s ===============================
  32.  
     
  33.  
    Process finished with exit code 0
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhieajij
系列文章
更多 icon
同类精品
更多 icon
继续加载