有没有办法抑制pytest的内部弃用警告?
语境:我期待评估从移植测试套件的难度nose来pytest.该套件相当大,并且基于使用nose风格yield的测试生成器.
我想首先确保现有测试通过pytest,然后可能将测试生成器更改为parameterized.
刚运行$ pytest path-to-test-folderpytest 3.0.4完全由页面和页面控制
WC1 ~repos/numpy/numpy/lib/tests/test_twodim_base.py yield tests are deprecated, and scheduled to be removed in pytest 4.0
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这些警告?
使用py.test,两个在不同目录中调用相同的测试会导致py.test失败.这是为什么?如何在不重命名所有测试的情况下更改此设置?
复制做:
; cd /var/tmp/my_test_module
; mkdir -p ook/test
; mkdir -p eek/test
; touch ook/test/test_proxy.py
; touch eek/test/test_proxy.py
; py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.4
collected 0 items / 1 errors
==================================== ERRORS ====================================
___________________ ERROR collecting ook/test/test_proxy.py ____________________
import file mismatch:
imported module 'test_proxy' has this __file__ attribute:
/home/ygolanski/code/junk/python/mymodule/eek/test/test_proxy.py
which is not the same as the test file we want to collect:
/home/ygolanski/code/junk/python/mymodule/ook/test/test_proxy.py
HINT: remove __pycache__ / .pyc files and/or use …Run Code Online (Sandbox Code Playgroud) 说我有一堆测试:
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
Run Code Online (Sandbox Code Playgroud)
是否有一个装饰器或类似的东西,我可以添加到函数,以防止py.test只运行该测试?结果可能看起来像......
@pytest.disable()
def test_func_one():
...
def test_func_two():
...
def test_func_three():
...
Run Code Online (Sandbox Code Playgroud)
我在py.test文档中搜索过类似的内容,但我想我可能会在这里遗漏一些东西.
当我在本地运行测试时它的工作正常,但是在创建了docker并在容器内部运行后,我遇到了错误.
/usr/local/lib/python3.5/site-packages/_pytest/config.py:325: in _getconftestmodules
return self._path2confmods[path]
E KeyError: local('/apis/db/tests')
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:356: in _importconftest
return self._conftestpath2mod[conftestpath]
E KeyError: local('/apis/db/tests/conftest.py')
During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:362: in _importconftest
mod = conftestpath.pyimport()
/usr/local/lib/python3.5/site-packages/py/_path/local.py:680: in pyimport
raise self.ImportMismatchError(modname, modfile, self)
_pytest.config.ConftestImportFailure: ImportMismatchError('conftest', '/projects/my_project/db/tests/conftest.py', local('/apis/db/tests/conftest.py'))
Run Code Online (Sandbox Code Playgroud)
/ apis - 它是Dockerfile中的WORKDIR.
让我们假设我们有这样的结果:
import py, pytest
ERROR1 = ' --- Error : value < 5! ---'
ERROR2 = ' --- Error : value > 10! ---'
class MyError(Exception):
def __init__(self, m):
self.m = m
def __str__(self):
return self.m
def foo(i):
if i < 5:
raise MyError(ERROR1)
elif i > 10:
raise MyError(ERROR2)
return i
# ---------------------- TESTS -------------------------
def test_foo1():
with pytest.raises(MyError) as e:
foo(3)
assert ERROR1 in str(e)
def test_foo2():
with pytest.raises(MyError) as e:
foo(11)
assert ERROR2 in str(e)
def test_foo3(): …Run Code Online (Sandbox Code Playgroud) 你如何测试pytest中的单个文件?我只能在文档中找到忽略选项而不"仅测试此文件"选项.
最好这可以在命令行上工作而不是setup.cfg,因为我想在ide中运行不同的文件测试.整个套房需要很长时间.
当我试图通过命令行运行我的测试时
py.test file_name.py
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
该py.test命令未能在我的情况,而pytest运行完全正常.
我使用pytest-flask插件:
platform linux -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/sebastian/develop/py/flask-rest-template, inifile:
plugins: flask-0.10.0
Run Code Online (Sandbox Code Playgroud)
当我调用时,$ py.test我收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 301, in _getconftestmodules
return self._path2confmods[path]
KeyError: local('/home/sebastian/develop/py/flask-rest-template')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 332, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local('/home/sebastian/develop/py/flask-rest-template/conftest.py')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 338, …Run Code Online (Sandbox Code Playgroud) 我想在我的测试套件中的每个测试之前和之后运行额外的设置和拆卸检查.我看过灯具但不确定它们是否是正确的方法.我需要在每次测试之前运行设置代码,我需要在每次测试后运行拆卸检查.
我的用例是检查没有正确清理的代码:它会留下临时文件.在我的设置中,我将检查文件,在拆解时我也检查文件.如果有额外的文件,我希望测试失败.
在我开始在python项目中执行测试之前,我读了一些环境变量并设置了一些读取这些值的变量.我的测试将根据读取的这些值在所需的环境中运行.
例如:假设环境变量被称为"ENV_NAME"和"ENV_NUMBER"
现在,我想使用py.test运行测试
如果我硬编码这些环境变量,例如:ENV_NAME ='staging',我的代码中的ENV_NUMBER ='5'然后通过在项目目录的根目录执行py.test命令来运行测试,所有测试都成功运行.
但是,我不想硬编码这些值.有没有办法,我可以将这些环境变量作为py.test的命令行参数发送?
我的想法更多
py.test -ENV_NAME ='staging'-ENV_NUMBER ='5'.但是,这不起作用.
请帮忙!谢谢!