Pytest ModuleNotFoundError:没有名为“x”的模块

2024-09-30 18:13:03 发布

您现在位置:Python中文网/ 问答频道 /正文

****已解决:将__init__.py添加到Test/,并将testcode.py重命名为test_code.py。运行测试cd -> Zee并键入pytest****


结构:

|--Zee/
|   |--Test/
|   |   |--__init__.py
|   |   |--test_code.py
|   |--Codetotest/
|   |   |--code.py

在code.py中

class Foo():
    some code...

在testcode.py中

from Codetotest.code import Foo

def test_foo():
   assert ...

当我移动到命令行中的Zee目录并运行pytest Test/testcode.py时,我得到ModuleNotFoundError: No module named Zee。我怎样才能解决这个问题

我尝试通过添加建议的here使测试成为一个模块。从多个目录运行,没有骰子

Pytest版本5.3.4,从python 3.6导入

我不明白的是,当我将__init__.py添加到Zee/时,它会给我同样的错误


Tags: pytest目录键入fooinitpytestcd
1条回答
网友
1楼 · 发布于 2024-09-30 18:13:03

在模块目录中需要一个__init__.py

以下是一个典型的项目结构:

| zee-project-directory/
|   | tests/
|   |   | test_zee.py
|   | zee/
|   |   | __init__.py
|   |   | code.py

代码.py

class Foo():
    some code...

测试_zee.py

from zee.code import Foo

def test_foo():
   assert ...

相关问题 更多 >