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

  • 相关阅读:
    硬币游戏 Project Euler 232
    屏幕空间的近似全局光照明(Approximative Global Illumination in Screen Space)
    四维之美
    vertex texture fetching in HLSL, and heightfield normal calculation
    一个VS小插件(跳出括号)
    我的算法书籍收藏
    Algorithms.算法概论.习题答案
    UML用例图教程详解
    大连理工大学软件学院博客地址
    快递查询API,我推荐“爱快递”
  • 原文地址:https://www.cnblogs.com/moonpool/p/11350780.html
Copyright © 2011-2022 走看看