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还处于试验阶段。

  • 相关阅读:
    Blackfin DSP(六):BF533的SPORT接口
    Blackfin DSP(五):BF533的SPI接口
    Blackfin DSP(八):BF533的DMA
    Blackfin DSP(四):BF533 EBIU之SDRAM
    Blackfin DSP(二):寄存器操作与GPIO
    python从小白到大咖方便查看链接
    mysql以及redis的主从搭建
    git,es的基本查询,组合查询,mapping映射,i分词,term和match
    go的接口,并发和并行,协程,信道,缓冲处理,异常处理
    接口幂等性,倒排索引,索引操作,文档基本增删改查,文档查询
  • 原文地址:https://www.cnblogs.com/moonpool/p/11350780.html
Copyright © 2011-2022 走看看