zoukankan      html  css  js  c++  java
  • pytest fixture中scope试验,包含function、module、class、session、package

     

    上图是试验的目录结构

    conftest.py:存放pytest fixture的文件

     1 import uuid
     2 import pytest
     3 
     4 @pytest.fixture(scope="module")
     5 def test_module():
     6     return "module"+str(uuid.uuid4())
     7 
     8 @pytest.fixture(scope="class")
     9 def test_class():
    10     return "class"+str(uuid.uuid4())
    11 
    12 @pytest.fixture(scope="function")
    13 def test_function():
    14     return "function"+str(uuid.uuid4())
    15 
    16 @pytest.fixture(scope="session")
    17 def test_session():
    18     return "session"+str(uuid.uuid4())
    19 
    20 
    21 @pytest.fixture(scope="package")
    22 def test_package():
    23     return "package"+str(uuid.uuid4())

     test_class.py:类测试文件

     1 class Test_for_pytest_scope(object):
     2     def test_case1(self,test_module,test_class,test_function,test_session,test_package):
     3         print("testcase/test_class.py-test_case1||" + test_function)
     4         print("testcase/test_class.py-test_case1||"+test_module)
     5         print("testcase/test_class.py-test_case1||"+test_class)
     6         print("testcase/test_class.py-test_case1||" + test_session)
     7         print("testcase/test_class.py-test_case1||" + test_package)
     8 
     9     def test_case2(self,test_module,test_class,test_function,test_session,test_package):
    10         print("testcase/test_class.py-test_case2||" + test_function)
    11         print("testcase/test_class.py-test_case2||"+test_module)
    12         print("testcase/test_class.py-test_case2||"+test_class)
    13         print("testcase/test_class.py-test_case2||" + test_session)
    14         print("testcase/test_class.py-test_case2||" + test_package)

    其它测试文件都是打印fixture的返回信息

    下图是打印结果

    结论:

    package的试验结果和预期有些出入,其它的作用范围大小关系为   function<class<module<session

    function 每个方法和函数执行前都会重新调用一些fixture,得到一个新的uuid
    class 一个文件(module)内,class内部方法共享数据,函数不共享数据
    module 一个文件就是一个module,一个module内只执行一次fixture,且module内共享数据
    package uuid都一致,和理解上有些出入,以为不同package会重新调用一次fixture
    session

    一次执行都一致

    补充:翻看官方文档,发现package还处于试验阶段。

  • 相关阅读:
    npm常用命令
    关于事件委托和时间冒泡(以及apply和call的事项)
    js 杂记
    angular中关于ng-repeat的性能问题
    关于日期的一些东西
    杂记
    angular中关于自定义指令——repeat渲染完成后执行动作
    angular中事件戳转日期的格式
    ES6-promise
    angular中ng-class的一些用法
  • 原文地址:https://www.cnblogs.com/moonpool/p/11350780.html
Copyright © 2011-2022 走看看