相关疑难解决方法(0)

如何查看pytest运行期间创建的正常打印输出?

有时我想在我的代码中插入一些打印语句,看看在我练习时会打印出来的内容.我通常的"锻炼"方法是使用现有的pytest测试.但是当我运行这些时,我似乎无法看到任何标准输出(至少来自PyCharm,我的IDE).

在pytest运行期间有没有一种简单的方法来查看标准输出?

python logging pytest output

320
推荐指数
12
解决办法
11万
查看次数

py.test日志控制

我们最近切换到py.test进行python测试(这是非常棒的btw).但是,我正在试图弄清楚如何控制日志输出(即内置的python日志记录模块).我们安装了pytest-capturelog,这可以按预期工作,当我们想查看日志时,我们可以通过--nologcapture选项.

但是,如何控制日志记录级别(例如信息,调试等)并过滤日志记录(如果您只对特定模块感兴趣).是否有py.test的现有插件来实现这一点,还是我们需要自己动手?

谢谢,强尼

python pytest

30
推荐指数
4
解决办法
2万
查看次数

如何将stdlib日志记录与py.test结合使用

我正在使用py.test来测试我的一些模块,其中包含相当多的stdlib日志记录.我当然喜欢将日志记录到stdout,这是由py.test捕获的,因此如果测试失败,我将获得所有相关的日志消息.

这样做的问题是,在py.test 丢弃此对象之后,日志记录模块最终尝试将消息记录到py.test提供的'stdout'对象.也就是说,我得到:

Traceback (most recent call last):
  File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
    h.flush()
  File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
    self.stream.flush()
ValueError: I/O operation on closed file
Run Code Online (Sandbox Code Playgroud)

如果我关闭捕获-s,我没有任何问题,但当然这使得测试输出与不相关的日志记录无法读取.

谁能告诉我将stdlib日志记录与py.test集成的正确方法?

(我试着看看这个,它看起来应该只是没有问题,所以它对我帮助不大)

python logging unit-testing pytest

6
推荐指数
1
解决办法
1400
查看次数

我应该如何登录我的 pytest 插件?

我正在构建一个 pytest 插件,目前我通过在插件文件的顶部添加类似于以下两行的内容来登录:

logging.basicConfig()
log = logging.getLogger(PLUGIN_NAME)
Run Code Online (Sandbox Code Playgroud)

但是,这似乎没有考虑 pytest 配置(通过命令行选项或 ini 文件),例如日志文件路径、记录器格式等。

我可以手动将它们复制到我的basicConfig电话中,但我认为 pytest 和第三方插件之间有更好的工具用于日志记录,不是吗?

我在 pytest 的文档和代码中发现了相当多的日志记录参考,但所有这些都在讨论捕获测试日志。我对记录pytest 插件本身很感兴趣。

python testing logging pytest

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

在pytest中设置动态文件夹和报告名称

我在 Python 的 pytest 中动态设置报告名称和文件夹时遇到问题。例如:我已经在 2020-03-06 21:50 运行了所有 pytest 的测试,所以我想将我的报告存储在20200306name 的文件夹中report_2150.html。我希望它在测试完成后立即自动化并触发。

我在 VS Code 中工作,我的目标是与没有自动化经验的同事分享我的工作,所以我的目标是将其用作“单击测试开始”。

我的项目结构:

webtools/
|?? .vscode/
|???? settings.json
|?? drivers/
|?? pages/
|?? reports/
|?? tests/
|???? __init__.py
|???? config.json
|???? conftest.py
|???? test_1.py
|???? test_2.py
|?? setup.py
Run Code Online (Sandbox Code Playgroud)

代码示例:

settings.json

webtools/
|?? .vscode/
|???? settings.json
|?? drivers/
|?? pages/
|?? reports/
|?? tests/
|???? __init__.py
|???? config.json
|???? conftest.py
|???? test_1.py
|???? test_2.py
|?? setup.py
Run Code Online (Sandbox Code Playgroud)

config.json

{
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.enabled": true, …
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x pytest-html

4
推荐指数
1
解决办法
1206
查看次数

在 Pytest 固定装置内使用时,Python 日志记录不会记录

我有一个 Pytest + Selenium 项目,我想使用日志记录模块。

conftest.py但是,当我像这样设置登录时

@pytest.fixture(params=["chrome"], scope="class")
def init_driver(request):
    start = datetime.now()
    logging.basicConfig(filename='.\\test.log', level=logging.INFO)
    if request.param == "chrome":
        options = ChromeOptions()
        options.add_argument("--start-maximized")
        web_driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    if request.param == "firefox":
        web_driver = webdriver.Firefox(GeckoDriverManager().install())
    request.cls.driver = web_driver
    yield
    end = datetime.now()
    logging.info(f"{end}: --- DURATION: {end - start}")
    web_driver.close()
Run Code Online (Sandbox Code Playgroud)

看起来test.log根本没有创建,并且没有错误消息或其他指示出现问题。

我怎样才能做到这一点?

logging automated-tests pytest python-3.x teardown

4
推荐指数
1
解决办法
5370
查看次数

py.test将消息和测试结果/断言记录到单个文件中

我现在开始使用py.test进行新项目.我们正在配置Linux服务器,我需要编写一个脚本来检查这些服务器的设置和配置.我认为py.test是实现这些测试的好方法,直到现在它才能正常工作.

我现在面临的问题是,在这些测试结束时我需要一个日志文件,显示每个测试的一些日志消息和测试结果.对于日志消息,我使用logger:

logging.basicConfig(filename='config_check.log', level=logging.INFO)
pytest.main()
logging.info('all done')
Run Code Online (Sandbox Code Playgroud)

作为示例测试我有这个:

def test_taintedKernel():
    logging.info('checking for tainted kernel')
    output = runcmd('cat /proc/sys/kernel/tainted')
    assert output == '0', 'tainted kernel found'
Run Code Online (Sandbox Code Playgroud)

所以在我的日志文件中我想要一个像这样的输出:

INFO:root:checking for tainted kernel
ERROR:root:tainted kernel found
INFO:root:next test
INFO:root:successful
INFO:root:all done
Run Code Online (Sandbox Code Playgroud)

但是我无法将测试结果输入到日志文件中,而是在测试后获得stdout上的标准输出:

======================================= test session starts =======================================
platform linux2 -- Python 2.6.8 -- py-1.4.22 -- pytest-2.6.0
collected 14 items 

test_basicLinux.py .............F

============================================ FAILURES =============================================
_______________________________________ test_taintedKernel ________________________________________

    def test_taintedKernel():
        logging.info('checking for tainted kernel')
        output = runcmd('cat /proc/sys/kernel/tainted')
>       assert output == '0', 'tainted …
Run Code Online (Sandbox Code Playgroud)

python testing logging pytest

3
推荐指数
1
解决办法
8903
查看次数