实验现象: 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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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: [email protected]
- # @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"``.
来源: http://www.bubuko.com/infodetail-3798851.html