py.test在哪里以及如何寻找灯具?我在同一文件夹中的2个文件中有相同的代码.当我删除conftest.py时,找不到运行test_conf.py的cmdopt(也在同一个文件夹中.为什么没有搜索到sonoftest.py?
# content of test_sample.py
def test_answer(cmdopt):
if cmdopt == "type1":
print ("first")
elif cmdopt == "type2":
print ("second")
assert 0 # to see what was printed
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
Run Code Online (Sandbox Code Playgroud)
文档说
http://pytest.org/latest/fixture.html#fixture-function
- pytest因test_前缀而找到test_ehlo.测试函数需要一个名为smtp的函数参数.通过查找名为smtp的夹具标记函数来发现匹配夹具功能.
- 调用smtp()来创建实例.
- 调用test_ehlo()并在测试函数的最后一行失败.
我尝试运行时收到以下错误pytest repo/tests/test_file.py:
$ pytest repo/tests/test_file.py
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests/test_file.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 362, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/marlo/repo/conftest.py')
During handling of the above exception, another exception occurred:
Traceback …Run Code Online (Sandbox Code Playgroud) 正如pytest 文档中所建议的,我设置了我的包,目的不是不将我的测试与我的包一起分发:
\n\nsetup.py\nmypkg/\n __init__.py\n mypkg/\n appmodule.py \ntests/\n test_app.py\n ...\nRun Code Online (Sandbox Code Playgroud)\n\n但我对如何确保这些测试在存在时正确运行(例如在 Travis CI 上或在项目目录的克隆中)感到困惑。
\n\n我希望测试脚本中的导入应用于相邻mypkg/目录中的源(而不是任何mypkg可能安装在 中的源site-packages),但我遇到了令我困惑的错误。
如果我遵循文档中的建议pytest并且不__init__.py添加tests/then
from __future__ import absolute_import\nfrom mypkg.appmodule import * \nRun Code Online (Sandbox Code Playgroud)\n\n我明白了
\n\n\n\n\n导入错误:没有名为 mypkg.appmodule 的模块
\n
当与
\n\nfrom __future__ import absolute_import\nfrom ..mypkg.appmodule import * \nRun Code Online (Sandbox Code Playgroud)\n\n我明白了
\n\n\n\n\nValueError:尝试在非包中进行相对导入
\n
如果我忽略文档并包含一个__init__.py,那么对于后者我会得到
\n\n\nValueError:尝试超出顶级包的相对导入
\n
仅通过包含 …