小编non*_*bot的帖子

有没有办法改变pytest的.cache目录的位置?

我需要能够将pytest的.cache目录的位置更改为env变量WORKSPACE.由于服务器权限超出我的控制范围,我遇到此错误,因为我的用户没有权限在运行测试的目录中写入:

py.error.EACCES: [Permission denied]: open('/path/to/restricted/directory/tests/.cache/v/cache/lastfailed', 'w')
Run Code Online (Sandbox Code Playgroud)

有没有办法将.cache目录的路径设置为环境变量WORKSPACE?

python directory caching pytest

19
推荐指数
3
解决办法
6672
查看次数

如何将pytest的结果/日志保存到文件中?

我无法尝试将pytest中显示的结果保存到文件中(txt,log,无所谓).在下面的测试示例中,我想将控制台中显示的内容捕获到某种文本/日志文件中:

import pytest
import os

def test_func1():
    assert True


def test_func2():
    assert 0 == 1

if __name__ == '__main__':

    pytest.main(args=['-sv', os.path.abspath(__file__)])
Run Code Online (Sandbox Code Playgroud)

控制台输出我想保存到文本文件:

test-mbp:hi_world ua$ python test_out.py
================================================= test session starts =================================================
platform darwin -- Python 2.7.6 -- py-1.4.28 -- pytest-2.7.1 -- /usr/bin/python
rootdir: /Users/tester/PycharmProjects/hi_world, inifile: 
plugins: capturelog
collected 2 items 

test_out.py::test_func1 PASSED
test_out.py::test_func2 FAILED

====================================================== FAILURES =======================================================
_____________________________________________________ test_func2 ______________________________________________________

    def test_func2():
>       assert 0 == 1
E       assert 0 == 1

test_out.py:9: AssertionError
========================================= 1 failed, 1 passed in …
Run Code Online (Sandbox Code Playgroud)

python logging pytest

7
推荐指数
1
解决办法
1万
查看次数

上下文管理器作为类还是函数?

我最近一直在研究Python的contextmanager(更具体地说,Python 3的contextlib或其向后移植的contextlib2),我想知道将它们编写为类与函数相比有何优点/缺点?

它们似乎都以相同的方式运行并以相同的方式处理异常。有很多很酷的实用程序,例如 ExitStack(),但这些实用程序似乎可以在编写为类或函数的上下文管理器中实现。因此,我正在努力寻找一个很好的理由来解释为什么人们想要将上下文管理器详细地编写为一个类,而它们可以被编写为一个函数并且只需使用 contextmanager 装饰器即可。

这是我写的一个简单的例子,展示了两者做同样的事情:

# !/usr/bin/python -u
# encoding: utf-8

from contextlib import contextmanager

# Function-based
@contextmanager
def func_custom_open(filename, mode):
    try:
        f = open(filename, mode)
        yield f
    except Exception as e:
        print(e)
    finally:
        f.close()

# Class-based
class class_custom_open(object):

    def __init__(self, filename, mode):
        self.f = open(filename, mode)

    def __enter__(self):
        return self.f

    def __exit__(self, type, value, traceback):
        self.f.close()


if __name__ == '__main__':

    # Function-based
    with func_custom_open('holafile_func.txt', 'w') as func_f:
        func_f.write('hola func!')

    # Class-based
    with class_custom_open('holafile_class.txt', 'w') as class_f:
        class_f.write('hola …
Run Code Online (Sandbox Code Playgroud)

python class function contextmanager

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

pytest:如果发生崩溃/段错误/等。测试期间发生,有没有办法让 pytest 将崩溃记录为测试失败并继续测试?

我有一些使用 pytest 编写的单元测试,它们成功地导致了段错误的发生。然而,如果在执行期间发生段错误(在我的 Mac 上),pytest 似乎会完全退出,并且不提供有关导致 python 解释器崩溃的原因的信息。

现在我可以从日志中推断出崩溃的特定测试并确定其中使用的值,但我想知道是否有任何方法能够以某种方式将崩溃记录为常规测试失败并继续迭代我的测试而不停止?

如果有帮助,我可以尝试举一个例子,但我认为这个问题应该是不言自明的。

python testing unit-testing pytest python-unittest

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