我使用skipIf()从unittest跳过在一定条件下的测试.
@unittest.skipIf(condition), "this is why I skipped them!")
Run Code Online (Sandbox Code Playgroud)
如何判断py.test显示跳过条件?
我知道对于unittest我需要启用详细模式(-v)但是添加到py.test的相同参数增加了详细程度仍然不显示跳过原因.
@pytest.fixture
def d_service():
c = DService()
return c
# @pytest.mark.asyncio # tried it too
async def test_get_file_list(d_service):
files = await d_service.get_file_list('')
print(files)
Run Code Online (Sandbox Code Playgroud)
然而,却出现了以下错误?
已收集 0 项 / 1 错误
===================================== 错误============== =====================
________________ 收集测试/e2e_tests/test_d.py 时出错 _________________
..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:617: 在 __call__ 中
返回 self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:222: 在 _hookexec 中
返回 self._inner_hookexec(钩子、方法、kwargs)
..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:216:在
第一个结果=hook.spec_opts.get('第一个结果'),
..\..\..\..\..\anaconda3\lib\site-packages\_pytest\python.py:171:在 pytest_pycollect_makeitem 中
res = 结果.get_result()
..\..\..\..\..\anaconda3\lib\site-packages\anyio\pytest_plugin.py:98:在 pytest_pycollect_makeitem 中
标记=collector.get_closest_marker('anyio')
E AttributeError:“模块”对象没有属性“get_closest_marker”
!!!!!!!!!!!!!!!!!!!!! 已中断:收集期间出现 1 个错误!!!!!!!!!!!!!!!!!!
=========================== 2.53 秒内出现 1 个错误 ================== =========
我安装了以下软件包。错误消失,但测试被跳过。
pip install …Run Code Online (Sandbox Code Playgroud) 我刚刚设置为使用python与Python 2.6.到目前为止,除了处理"导入"语句之外,它运行良好:我似乎无法以与我的程序相同的方式响应导入.
我的目录结构如下:
src/
main.py
util.py
test/
test_util.py
geom/
vector.py
region.py
test/
test_vector.py
test_region.py
Run Code Online (Sandbox Code Playgroud)
要运行,我python main.py从src/调用.
在main.py中,我导入了vector和region
from geom.region import Region
from geom.vector import Vector
Run Code Online (Sandbox Code Playgroud)
在vector.py中,我导入了区域
from geom.region import Region
Run Code Online (Sandbox Code Playgroud)
当我在标准运行中运行代码时,这些都可以正常工作.但是,当我从src /调用"py.test"时,它会一直退出导入错误.
我的第一个问题是,当运行"test/test_foo.py"时,py.test无法直接"导入foo.py".我通过使用"imp"工具解决了这个问题.在"test_util.py"中:
import imp
util = imp.load_source("util", "util.py")
Run Code Online (Sandbox Code Playgroud)
这适用于许多文件.它似乎也暗示当pytest运行"path/test/test_foo.py"来测试"path/foo.py"时,它基于目录"path".
但是,"test_vector.py"失败了.Pytest可以找到并导入vector模块,但它无法找到任何vector的进口.使用pytest时,以下导入(来自"vector.py")都会失败:
from geom.region import *
from region import *
Run Code Online (Sandbox Code Playgroud)
这些都给出了表格的错误
ImportError: No module named [geom.region / region]
Run Code Online (Sandbox Code Playgroud)
我不知道接下来要做什么来解决这个问题; 我对Python中导入的理解是有限的.
使用pytest时处理导入的正确方法是什么?
在vector.py,我更改了import语句
from geom.region import Region
Run Code Online (Sandbox Code Playgroud)
简单地说
from region …Run Code Online (Sandbox Code Playgroud) 题
如何在不在test目录中创建包的情况下导入测试文件中的辅助函数?
上下文
我想创建一个测试辅助函数,我可以在几个测试中导入它.说,像这样:
# In common_file.py
def assert_a_general_property_between(x, y):
# test a specific relationship between x and y
assert ...
# In test/my_test.py
def test_something_with(x):
some_value = some_function_of_(x)
assert_a_general_property_between(x, some_value)
Run Code Online (Sandbox Code Playgroud)
使用Python 3.5,py.test 2.8.2
当前"解决方案"
我目前正在通过在我的项目test目录(现在是一个包)中导入一个模块来实现这一点,但是如果可能的话我想用其他一些机制来做(因为我的test目录没有包但只是测试,并且可以在已安装的软件包版本上运行测试,如py.test文档中关于良好实践的建议).
我对诗歌很陌生,想用 pytest 来设置它。我在以下设置中有一个包 mylib
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mylib-0.0.1-py3-none-any.whl\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mylib-0.0.1.tar.gz\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 poetry.lock\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mylib\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 functions.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 utils.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tests\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_functions.py\nRun Code Online (Sandbox Code Playgroud)\n在 test_functions 我有
\nimport mylib\nRun Code Online (Sandbox Code Playgroud)\n然而,当我跑步时
\npoetry run pytest\nRun Code Online (Sandbox Code Playgroud)\n它抱怨mylib没有被包括在内。我可以跑
pip install dist/mylib-0.0.1-py3-none-any.whl\nRun Code Online (Sandbox Code Playgroud)\n但这会使我的 python 环境与 mylib 变得混乱。我也想将该环境用于其他包。
\n我的问题是:使用诗歌和 pytest 的正确方法是什么?
\n我的底层 python 环境是干净的 pyenv python 3.8。使用 pyproject.toml 我为 mylib 创建一个基于项目的虚拟环境。
\n我正在设计一个py.test,它应该运行直到遇到异常.如果测试从未遇到异常,它应该在剩下的时间内继续运行,或者直到我发送一个SIGINT/SIGTERM.
是否有一种编程方式告诉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)
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()并在测试函数的最后一行失败.
我不能调试在PyCharm使用py.test.所有测试套件在"调试模式"下运行正常,但它不会在断点上停止.
我也有py.test作为默认测试运行器.
也许这不重要,但调试在我的Django服务器中正常工作.
有任何想法吗?
enable_breakpoints_and_the_mode_of_pycharm_is_ debug的图片
参考文献:
我有一个python脚本,它接受命令行参数,使用一些文件.我正在编写成功的测试,py.test将这个脚本放在其脚本中,并执行它subprocess.call.
现在我想分析代码覆盖率coverage.py.覆盖范围,通过使用时pytest-cov的插件(其中有子装卸内置),没有看到/覆盖我的剧本时,它从创建一个临时目录测试名为py.test的tmpdir夹具.Coverage 在其所在的目录中调用时会看到我的脚本(并且filename参数指向远程路径).
在这两种情况下,我的测试通过!覆盖范围3.6,pytest-2.3.5,pytest-cov 1.6,全部来自PyPi.
问题:即使在另一个目录中执行脚本,如何识别我的脚本?这是覆盖范围内的错误,还是一些无法做到的事情?如果后者毕竟tmpdir是py.test的股票机制,那会感到惊讶......
最小的例子:
我有一个脚本my_script.py,它只是回应arg_file.txt通过命令行参数提供的文件的内容.在两个不同的测试中,这一次在a中调用tmpdir,一次在脚本的位置调用.两个测试都通过了,但是在tmpdir测试中,我没有得到任何覆盖信息!
测试运行:
~/pytest_experiment$ py.test -s
=================================== test session starts ====================================
platform linux2 -- Python 2.7.4 -- pytest-2.3.5
plugins: cov
collected 2 items
tests/test_in_scriptdir.py
set_up: In directory /tmp/pytest-52/test_10
Running in directory /home/cbuchner/pytest_experiment
Command: ./my_script.py /tmp/pytest-52/test_10/arg_file.txt
--Contents of arg_file.txt--
.
tests/test_in_tmpdir.py
set_up: In directory /tmp/pytest-52/test_11
Running in directory /tmp/pytest-52/test_11 …Run Code Online (Sandbox Code Playgroud) 我希望我的测试文件夹与我的应用程序代码分开。我的项目结构是这样的
myproject/
myproject/
myproject.py
moduleone.py
tests/
myproject_test.py
Run Code Online (Sandbox Code Playgroud)
我的项目.py
from moduleone import ModuleOne
class MyProject(object)
....
Run Code Online (Sandbox Code Playgroud)
myproject_test.py
from myproject.myproject import MyProject
import pytest
...
Run Code Online (Sandbox Code Playgroud)
我使用myproject.myproject因为我使用命令
python -m pytest
Run Code Online (Sandbox Code Playgroud)
从项目根目录 ./myproject/
但是,然后这些模块中的导入失败
E ModuleNotFoundError: 没有名为“moduleone”的模块
我正在运行 Python 3.7 并已阅读自 3.3 以来,__init__不再需要空文件,这意味着我的项目成为隐式命名空间包
但是,我尝试在其中添加__init__.py文件myproject/myproject/并尝试在其中添加conftest.py文件myproject/但都不起作用
我已经阅读了一些答案,这些答案说要弄乱路径,然后在其他问题中投票说不要。
什么是正确的方法,我错过了什么?
编辑;
可能相关,我使用 arequirements.txt使用 pip 安装 pytest。这可能有关系吗?如果是这样,在这种情况下安装 pytest 的正确方法是什么?
编辑2:
其中的路径之一sys.path …
pytest ×10
python ×9
unit-testing ×2
coverage.py ×1
docker ×1
fixtures ×1
import ×1
pycharm ×1
python-3.x ×1