有时我想在我的代码中插入一些打印语句,看看在我练习时会打印出来的内容.我通常的"锻炼"方法是使用现有的pytest测试.但是当我运行这些时,我似乎无法看到任何标准输出(至少来自PyCharm,我的IDE).
在pytest运行期间有没有一种简单的方法来查看标准输出?
# coding=utf-8
import pytest
def whatever():
return 9/0
def test_whatever():
try:
whatever()
except ZeroDivisionError as exc:
pytest.fail(exc, pytrace=True)
Run Code Online (Sandbox Code Playgroud)
================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django, cov
collected 1 items
pytest_test.py F
====================================== FAILURES ======================================
___________________________________ test_whatever ____________________________________
def test_whatever():
try:
whatever()
except ZeroDivisionError as exc:
> pytest.fail(exc, pytrace=True)
E Failed: integer division or modulo by zero
pytest_test.py:12: Failed
============================== 1 failed in 1.16 seconds ==============================
Run Code Online (Sandbox Code Playgroud)
如何使pytest打印回溯,所以我会看到whatever函数中引发异常的位置?
我使用easy_install在mac上安装pytest并开始为具有文件结构的项目编写测试,如下所示:
repo/
repo/app.py
repo/settings.py
repo/models.py
repo/tests/
repo/tests/test_app.py
Run Code Online (Sandbox Code Playgroud)
py.test在repo目录中运行时,一切都按预期运行
但是当我在linux或windows上尝试相同的东西时(两者都有pytest 2.2.3),每当它从我的应用程序路径首次导入某些东西时就会吠叫.比如说from app import some_def_in_app
我是否需要编辑我的PATH才能在这些系统上运行py.test?有没有人经历过这个?
我最近发现了pytest.看起来很棒.但是,我觉得文档可能更好.
我正在尝试了解哪些conftest.py文件的用途.
在我(当前很小的)测试套件中,我conftest.py在项目根目录中有一个文件.我用它来定义我注入测试的灯具.
我有两个问题:
conftest.py吗?它有其他用途吗?conftest.py文件吗?我什么时候想这样做?将举例说明.更一般地说,您如何conftest.py在py.test测试套件中定义文件的目的和正确使用?
环境:
Python 3.10.11
Flask==2.2.2
Run Code Online (Sandbox Code Playgroud)
我在 docker 容器中使用 BASE Image 运行 Flask 后端代码:
FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime
但是当我使用 version 运行 pytest 时pytest 7.4.2,
pip install pytest
pytest
Run Code Online (Sandbox Code Playgroud)
它引发了一个错误,日志如下:
==================================== ERRORS ====================================
_____________ ERROR collecting tests/test_fiftyone_utils_utils.py ______________
ImportError while importing test module '/builds/kw/data-auto-analysis-toolkit-backend/tests/test_fiftyone_utils_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/conda/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_fiftyone_utils_utils.py:2: in <module>
import daat # noqa: F401
/opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/__init__.py:1: in <module>
from daat.app import app
/opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/app/__init__.py:6: in <module>
from flask import …Run Code Online (Sandbox Code Playgroud) 我想开始我的Python代码编写单元测试,以及py.test框架听起来比Python捆绑的一个更好的选择单元测试.所以我在我的项目中添加了一个"tests"目录,并添加了test_sample.py.现在我想配置PyCharm来运行我的"tests"目录中的所有测试.
据称PyCharm 在其测试运行中支持py.test.您应该能够创建运行/调试配置来运行测试,并且据称PyCharm具有专门用于py.test的"创建配置"对话框.但这是关于这个主题的文档的完整范围,我无法在任何地方找到这个所谓的对话框.
如果我右键单击项目工具窗口中的目录,我应该看到"创建<名称>"菜单项,但唯一以"创建"开头的菜单项是"创建运行配置".好吧,也许文档是错误的,"创建运行配置"确实听起来很有希望.不幸的是,其子菜单中只有两个项目是"C:\ mypath中的单元测试..."和"C:\ mypath中的Doctests ......",两者都不适用 - 我既不使用unittest也不使用doctest.py.test没有菜单项.
如果我打开我的test_sample.py并在编辑器窗口中右键单击,我会得到承诺的"创建<名称>"菜单项:在test_sa中创建"单元测试...",然后"运行" '在test_sa中运行单元测试...'"和在test_sa中调试'单元测试......'".所以,这一切都与unittest框架有关; py.test没什么.
如果我尝试使用"unittest"的菜单项,我会看到一个对话框,其中包含"名称","类型","测试"组框,其中包含"文件夹"和"模式","脚本"和"类" "和"功能",等等.这听起来像是为Python单元测试添加配置的对话框,而不是像应该显示的"名称"和"测试运行"和"关键字"选项在py.test对话框的配置中.对话框中没有任何内容可以切换我添加的测试框架.
我在Windows上使用PyCharm 1.5.2和Python 3.1.3以及pytest 2.0.3.我可以从命令行成功运行py.test我的测试,所以这并不像pytest没有正确安装那么简单.
如何配置PyCharm来运行我的py.test测试?
我正在尝试使用pytest模块进行测试驱动开发.pytest我写的时候pytest不会到控制台print.
我用print它来运行它...
在pytest my_tests.py似乎是说,它应该是默认的工作:http://pytest.org/latest/capture.html
但:
import myapplication as tum
class TestBlogger:
@classmethod
def setup_class(self):
self.user = "alice"
self.b = tum.Blogger(self.user)
print "This should be printed, but it won't be!"
def test_inherit(self):
assert issubclass(tum.Blogger, tum.Site)
links = self.b.get_links(posts)
print len(links) # This won't print either.
Run Code Online (Sandbox Code Playgroud)
什么都没有打印到我的标准输出控制台(只是正常的进度和多少次测试通过/失败).
我正在测试的脚本包含print:
class Blogger(Site):
get_links(self, posts):
print len(posts) # It won't get printed in the test.
Run Code Online (Sandbox Code Playgroud)
在documentation模块中,默认情况下会打印所有内容,这正是我需要的.但是,我希望unittest出于其他原因使用.它似乎是这样的基本功能,也许我很想念它!?
有谁知道如何显示打印语句?
有没有办法选择pytest测试从文件运行?例如,包含要执行的测试列表的文件foo.txt:
tests_directory/foo.py::test_001
tests_directory/bar.py::test_some_other_test
Run Code Online (Sandbox Code Playgroud)
或者有没有办法从pytest的不同目录中选择多个测试,测试名称中没有共同的模式?
py.test -k <pattern> 允许单一模式.
一种选择是针对每个测试使用pytest.mark,但我的要求是运行来自不同文件的不同测试组合.
有没有办法为每个模式指定多个模式和测试文件名?或者有没有办法在文件中指定确切的测试路径并将该文件作为pytest的输入提供?或者是否有可用于此目的的钩子功能?
如何assert almost equal处理浮动的py.test而不诉诸于:
assert x - 0.00001 <= y <= x + 0.00001
Run Code Online (Sandbox Code Playgroud)
更具体地说,知道一个简洁的解决方案来快速比较浮动对,这将是有用的,而无需解压缩它们:
assert (1.32, 2.4) == i_return_tuple_of_two_floats()
Run Code Online (Sandbox Code Playgroud) 如果我尝试使用 python 3.10 执行测试,pytest则会出现以下错误:
TypeError: required field "lineno" missing from alias
Run Code Online (Sandbox Code Playgroud)
谷歌说这是一个已知问题,但我找不到解决的方法。
如果我用 3.9 启动测试,测试就会开始正常
pytest ×10
python ×10
unit-testing ×4
testing ×2
flask ×1
logging ×1
output ×1
pycharm ×1
python-2.7 ×1
python-3.10 ×1
werkzeug ×1