我有以下包结构:
.
├── my_app
│ ├── app.py
│ ├── cli.py
│ ├── db.py
│ └── __init__.py
├── setup.py
├── tests
│ ├── data
│ │ └── foobar.gz
│ ├── test_app.py
│ └── test_database.py
└── tox.ini
在中test_app.py
,我这样做:
import pkg_resources
path = '../tests/data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)
现在我已经看到消息:
/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py:1145: DeprecationWarning: Use of .. or absolute path in a resource path is not allowed and will raise exceptions in a future release. self, resource_name
我该怎么办?
(我也可以更改包的结构,但我想将测试数据保留在测试中-无需运行应用程序)
要在将测试文件名传递给pkg_resources
您时想要获取测试资源,请使用文件中的路径进行以下测试:
import pkg_resources
path = 'data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)
使用pytest时,我还在以下模式中使用了这种模式conftest.py
:
@pytest.fixture('session')
def test_data_dir():
return os.path.join(os.path.dirname(__file__), 'data')
我可以使用os.path.join()
它轻松获得任何测试数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句