pytest_sessionstart不执行的问题表现

问题:使用的是pytest 6.2.1、pytest 7.4.3版本,一个conftest.py,包含pytest_sessionstart。如果conftest.py不在测试目录根下,那么pytest_sessionstart就不会运行。但是,把conftest.py移到主目录,pytest_sessionstart它就能运行。

test
   |-conftest.py
   |-test1
      |-conftest.py

解决方式

pytest 6.2.1

暂未发现解决方式,只能将pytest_sessionstart放到主目录下才能运行

pytest 7.4.3

pytest.ini的testpath,将测试目录设置到具体的目录或者是根目录/*才能执行

testpaths = test/test1

testpaths = test/*

曲线救国方式

在同级的__init__.py 定义方法进行执行

文章目录