ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

009、fixture scope='session' 关于seesion的疑问

2021-07-18 13:00:24  阅读:187  来源: 互联网

标签:self py fixture print conftest session test scope def


 

实验现象:  conftest.py ( 内有代码 scope='seesion' ), 如果是放在项目根目录下只执行一次; 如果放在项目 子package中,并且有多个  子package,会被执行多次。

疑问:scope='seesion' 说好的不是只执行一次吗?  为什么会执行多次 ?

 

目录结构如下:

 

test_aa 目录下 conftest.py 代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10
import pytest


@pytest.fixture(scope='session', autouse=True)
def aa():
    print('\n我是test_aa目录下的conftest.py')
View Code

test_aa 目录下 test_one.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:09

class TestAA():
    def test_01(self):
        print('=========test_01========')

    def test_02(self):
        print('=========test_02========')


class TestBB():
    def test_03(self):
        print('=========test_03========')

    def test_04(self):
        print('=========test_04========')
View Code

test_aa 目录下 test_two.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:09


class TestCC():
    def test_05(self):
        print('\n=========test_05========')

    def test_06(self):
        print('=========test_06========')


class TestDD():
    def test_07(self):
        print('=========test_07========')

    def test_08(self):
        print('=========test_08========')
View Code

 

 

test_bb 目录下 conftest.py 代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10
import pytest


@pytest.fixture(scope='session', autouse=True)
def bb():
    print('\n我是test_bb目录下的conftest.py')
View Code

test_bb目录下 test_four.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:09


class TestEE():
    def test_09(self):
        print('=========test_09========')

    def test_10(self):
        print('=========test_10========')


class TestFF():
    def test_11(self):
        print('=========test_11========')

    def test_12(self):
        print('=========test_12========')
View Code

test_bb目录下 test_three.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10

class TestGG():
    def test_13(self):
        print('\n=========test_13========')

    def test_14(self):
        print('=========test_14========')


class TestHH():
    def test_15(self):
        print('=========test_15========')

    def test_16(self):
        print('=========test_16========')
View Code

 

 

test_cc 目录下 conftest.py 代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10
import pytest


@pytest.fixture(scope='session', autouse=True)
def cc():
    print('\n我是test_cc目录下的conftest.py')
View Code

test_cc目录下 test_five.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10

class TestII():
    def test_17(self):
        print('=========test_17========')

    def test_18(self):
        print('=========test_18========')


class TestJJ():
    def test_19(self):
        print('=========test_19========')

    def test_20(self):
        print('=========test_20========')
View Code

test_cc目录下 test_six.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10

class TestKK():
    def test_21(self):
        print('\n=========test_21========')

    def test_22(self):
        print('=========test_22========')


class TestMM():
    def test_23(self):
        print('=========test_23========')

    def test_24(self):
        print('=========test_24========')
View Code

 

 

项目根目录下的 conftest.py  代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/18 11:10
import pytest


@pytest.fixture(scope='session', autouse=True)
def day07():
    print('\n我是day07目录下的conftest.py')
View Code

 

 

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day07>pytest -s
============================================================ test session starts =============================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day07
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 24 items

test_aa\test_one.py
我是day07目录下的conftest.py

我是test_aa目录下的conftest.py
=========test_01========
.=========test_02========
.=========test_03========
.=========test_04========
.
test_aa\test_two.py
=========test_05========
.=========test_06========
.=========test_07========
.=========test_08========
.
test_bb\test_four.py
我是test_bb目录下的conftest.py
=========test_09========
.=========test_10========
.=========test_11========
.=========test_12========
.
test_bb\test_three.py
=========test_13========
.=========test_14========
.=========test_15========
.=========test_16========
.
test_cc\test_five.py
我是test_cc目录下的conftest.py
=========test_17========
.=========test_18========
.=========test_19========
.=========test_20========
.
test_cc\test_six.py
=========test_21========
.=========test_22========
.=========test_23========
.=========test_24========
.

============================================================= 24 passed in 0.13s =============================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day07>
View Code

 

 

另外说明,当我把上面代码中所有 conftest.py 中 的  scope = session 改成 package  ,执行结果保持不变。

源码中是这样写的:

The scope for which this fixture is shared; one of ``"function"``
        (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.

 

标签:self,py,fixture,print,conftest,session,test,scope,def
来源: https://www.cnblogs.com/qq-2780619724/p/15026315.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有