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

标签 python pytest

我有 the pytest good integration practices guide 推荐的目录结构,看起来像这样:

.
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)

测试.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 允许从模块/包/共享装置等加载测试......

参见 this page了解更多信息。

关于python - 使用单独的测试文件运行 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46771568/

相关文章:

python - Django:通过查询计算列值的总和

python - 安装mysql后Windows cmd无法识别python或pip

python - 如何让敌人改变方向 PYTHON

python - Pytest 单元测试失败,因为目标函数具有 cachetools.ttl_cache 装饰器

python - 如何将脚本作为pytest测试运行

python - 打字,自定义集合类型

python - 如何向量化分类数据

python - 双参数化

python - 带有西里尔符号的 pytest 3.0.5

python - 如何在 pytest 中的 mark.parametrize 中使用固定装置