【发布时间】:2017-03-21 16:20:56
【问题描述】:
我想获得有关使用 pytest 设置简单应用的最佳方法的建议。
我可以在单个文件上正常运行 pytest,但我想使用以下方式引导 Python 应用程序:
mypkg/
__init__.py
app.py
tests/
__init__.py
test_app.py
但是当我运行pytest 时出现错误:
NameError: name 'func' is not defined
mypkg/__init__.py 文件
# content of __init__.py
# Empty init file
mypkg/app.py 文件
# content of app.py
class mypkg:
def func(x):
return x + 1
测试/__init__.py 文件
# content of __init__.py
# Empty init file
测试/test_app.py 文件
# content of test_app.py
import pytest
import mypkg
def test_answer():
assert func(4) == 5
我使用的是 Python 3.5。
我需要setup.py,如果我尝试调用mypkg.func(4),我会得到AttributeError: modulemypkghas no attribute 'func'。
非常感谢任何帮助。
【问题讨论】:
标签: python python-3.x pytest