我有一个项目目录结构如下(我认为这很标准):
my_project
setup.py
mypkg
__init__.py
foo.py
tests
functional
test_f1.py
unit
test_u1.py
Run Code Online (Sandbox Code Playgroud)
我正在使用py.test作为我的测试框架,我希望能够py.test tests在my_project目录中运行以运行我的测试.这确实有效,直到我尝试import mypkg在测试中使用(例如)导入我的应用程序代码.那时,我收到错误"没有名为mypkg的模块".在进行一些调查时,似乎py.test运行测试与测试文件的目录sys.path,但不是py.test运行的目录.
为了解决这个问题,我conftest.py在我的tests目录中添加了一个文件,其中包含以下代码:
import sys, os
# Make sure that the application source directory (this directory's parent) is
# on sys.path.
here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, here)
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但它是确保测试看到应用程序代码的好方法吗?有没有更好的方法来实现这一点,或者我在如何构建项目时做错了什么?
我已经看过其他一些使用的项目py.test(例如,pip但是我看不到代码做了这样的事情,但是运行py.test tests似乎在那里工作.我不知道为什么,但我担心他们可能以更简单的方式取得了同样的结果.
我查看了py.test文档,但是我看不出这个问题的解释或建议的方法来处理它.
使用 pytest 和 vscode-python 我想运行之前使用 unittest 框架实现的测试。
因此,我在相应的目录中使用 pytest 成功运行了测试tests。
pytest
Run Code Online (Sandbox Code Playgroud)
我还设置了 vscode-python 并成功测试了几乎所有测试。
但是,这些从子目录加载数据的测试tests/data会失败,因为 vscode-python 似乎从测试目录之外的另一个目录运行 pytest tests。
abc/
|-- tests/
|-- test_function.py
|-- data/
Run Code Online (Sandbox Code Playgroud)
如何设置 vscode-python,以便成功读取已实施的测试中的所有数据文件?
正如标题所说 - 我的文件夹结构是这样的:
venv/
__init__.py
.circleci/
config.yml
Dockerfile
docker-compose.yml
config.py
requirements.txt
src/
__init__.py
other_scripts.py
tests/
__init__.py
test_a.py
test_b.py
Run Code Online (Sandbox Code Playgroud)
测试文件有from config import *一行。运行$ pytest来自根目录在本地或通过bash外壳到所述容器(一个的virtualenv内)按预期方式工作,但对CircleCI生成失败与ImportError: No module named 'config'对于上面的代码行。我正在使用 python3.5 和 circleCI 2.0。
提前致谢!