在上一篇文章中,我们执行scope='package’时报如下错:
D:\pythonProject\dependency\dep\dep2>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep\dep2
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 6 items
test_dep_01.py::Testna::test_01 function test_01
PASSED
test_dep_01.py::Testna::test_02 function test_02
PASSED
test_dep_02.py::test_02 function test_02
PASSED
test_dep_02.py::Testdep1::test_01 class test_01
PASSED
test_dep_02.py::Testdep1::test_02 ERROR
test_dep_02.py::test_01 function test_01
PASSED
============================================================================== ERRORS ===============================================================================
________________________________________________________________ ERROR at setup of Testdep1.test_02 _________________________________________________________________
item = <Function test_02>
def pytest_runtest_setup(item):
"""Check dependencies if this item is marked "dependency".
Skip if any of the dependencies has not been run successfully.
"""
marker = item.get_closest_marker("dependency")
if marker is not None:
depends = marker.kwargs.get('depends')
if depends:
scope = marker.kwargs.get('scope', 'module')
manager = DependencyManager.getManager(item, scope=scope)
> manager.checkDepend(depends, item)
E AttributeError: 'NoneType' object has no attribute 'checkDepend'
D:\python3.7.6\lib\site-packages\pytest_dependency.py:179: AttributeError
====================================================================== short test summary info ======================================================================
ERROR test_dep_02.py::Testdep1::test_02 - AttributeError: 'NoneType' object has no attribute 'checkDepend'
==================================================================== 5 passed, 1 error in 0.10s =====================================================================
明明设置的@pytest.mark.dependency(depends=[“test_dep_01.py::Testna::test_01”]中的方法也已经执行了,为什么还报错了?
解决方法是:在dep2目录下增加__init__.py文件即可。 _init_.py文件内容是空的。关于_init__.py文件的作用后面我们再介绍。