繁体   English   中英

使用单独的测试文件运行pytest

[英]Running pytest with separate test files

我有一个pytest良好集成做法指南推荐的目录结构,如下所示:

.
src/
    mypkg/
          __init__.py
          mypkg.py
tests/
      __init__.py
      test_mypkg.py

__init__.py文件均为空,其他文件如下:

mypkg.py

def foo(x):
    """
    Show x.
    >>> foo(5)
    x is 5
    >>> foo("hello")
    x is hello
    """
    return "x is {0}".format(x)

test.py

from mypkg import *
def test_foo():
    assert foo(5) == "x is 5"

当我从根目录运行pytest ,我得到:

tests\test_foo.py:1: in <module>
    from mypkg import *
E   ModuleNotFoundError: No module named 'mypkg'

推荐的设置方式是什么?

我不是pytest专家,但这看起来像conftest.py的任务,conftest允许您从模块/软件包/共享夹具中加载测试,等等。

请参阅此页面以获取更多信息。

py不知道mypkg.py的路径。 所以您需要使用以下两行来告知

import sys
sys.path.append("/path/to/src/mypkg")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM