相关疑难解决方法(0)

py.test如何以及在哪里找到固定装置

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)

conftest.py的内容

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)

sonoftest.py的内容

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

  1. pytest因test_前缀而找到test_ehlo.测试函数需要一个名为smtp的函数参数.通过查找名为smtp的夹具标记函数来发现匹配夹具功能.
  2. 调用smtp()来创建实例.
  3. 调用test_ehlo()并在测试函数的最后一行失败.

python fixtures pytest

33
推荐指数
3
解决办法
2万
查看次数

Pytest"错误:无法加载路径/到/ conftest.py"

我尝试运行时收到以下错误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

15
推荐指数
1
解决办法
5824
查看次数

如果我不随包分发 pytest 测试,如何组织它们?

正如pytest 文档中所建议的,我设置了我的包,目的不是将我的测试与我的包一起分发:

\n\n
setup.py\nmypkg/\n    __init__.py\n    mypkg/\n      appmodule.py  \ntests/\n    test_app.py\n    ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我对如何确保这些测试在存在时正确运行(例如在 Travis CI 上或在项目目录的克隆中)感到困惑。

\n\n

我希望测试脚本中的导入应用于相邻mypkg/目录中的源(而不是任何mypkg可能安装在 中的源site-packages),但我遇到了令我困惑的错误。

\n\n

如果我遵循文档中的建议pytest并且不__init__.py添加tests/then

\n\n
from __future__ import absolute_import\nfrom mypkg.appmodule import * \n
Run Code Online (Sandbox Code Playgroud)\n\n

我明白了

\n\n
\n

导入错误:没有名为 mypkg.appmodule 的模块

\n
\n\n

当与

\n\n
from __future__ import absolute_import\nfrom ..mypkg.appmodule import * \n
Run Code Online (Sandbox Code Playgroud)\n\n

我明白了

\n\n
\n

ValueError:尝试在非包中进行相对导入

\n
\n\n

如果我忽略文档并包含一个__init__.py,那么对于后者我会得到

\n\n
\n

ValueError:尝试超出顶级包的相对导入

\n
\n\n

仅通过包含 …

software-distribution pytest python-2.7

5
推荐指数
1
解决办法
2185
查看次数