猿问

pytest的文件夹结构和导入问题

想象一个简单的文件夹结构:


my_folder/

    __init__.py

    funcs.py

    tests/

        test_funcs.py

函数.py:


def f():

    return 2

__init__.py:


from funcs import f

test_funcs.py:


from funcs import f


def test_f():

    assert f() == 2

这是文档中建议的方法之一:https : //pytest.readthedocs.io/en/reorganize-docs/new-docs/user/directory_structure.html


但是当我从my_folder以下位置运行 pytest 时:


tests/test_funcs.py:1: in <module>

    from funcs import f

E   ModuleNotFoundError: No module named 'funcs'

这很奇怪,因为我会认为它pytest设置了它运行的路径,所以如果不手动处理它就不会出现这些类型的错误。


文档也没有给出任何关于这一点的迹象......他们只是说:


通常,您可以通过指向测试目录或模块来运行测试:


pytest tests/test_appmodule.py      # for external test dirs

pytest src/tests/test_appmodule.py  # for inlined test dirs

pytest src                          # run tests in all below test directories

pytest                              # run all tests below current dir

我错过了什么?


慕容3067478
浏览 283回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答