py.test在堆栈跟踪中打印出完整数据

时间:2014-07-18 18:48:29

标签: python pytest

目前,当我的一个测试失败时,它会打印出单元测试的参数,然后是堆栈跟踪。但是,如果参数是大型JSON对象,则不会显示完整对象。有没有办法轻松强制py.test不切断数据?堆栈跟踪示例:

TestSpecialOfferDefaultContentUser.test_check_default_content_for_user[test_data0] 

self = <scripts.spof.test_01_special_offer_default_content_user.TestSpecialOfferDefaultContentUser instance at 0x00000000022CF8C8>
config = <merlin.lib.configuration_test.TestConfiguration object at 0x00000000022DD1D0>
test_data = {'amount_of_number_of_days_options': 6, 'available_for_guest': 'True', 'fl_password': 'password', 'fl_user': 'gg_abrestest@yahoo.com', ...}

@pytest.mark.parametrize("test_data", _test_cases_special_offer)
def test_check_default_content_for_user(self, config, test_data):
[...]

2 个答案:

答案 0 :(得分:3)

你是对的,目前无法显示,这可能是一个功能请求所以请提交错误。

然而,解决这个问题的“标准”方法是简单地依靠py.test的优秀输出捕获并在测试期间显式打印参数:

def test_foo(large_dict):
    pprint.pprint(large_dict)
    assert 0, 'Something went wrong'

这将导致其余报告中的信息在失败时可用。

答案 1 :(得分:2)

好问题。

未找到命令行开关强制完成打印输出。

搜索和测试py.test的各种命令行选项,我没有找到任何方法来填充 字典打印输出。

这可能是有充分理由的,无休止的打印输出并不是很有用。

使用assert消息获取自己的打印输出

虽然py.test在解释出错的有用信息方面做得非常出色,但有时你必须自己动手。

assert具有带消息的可选参数。如果您使用它,py.test打印输出将替换为您在该消息中添加的内容。

拥有文件test_it.py

def test_it():
    dct = {str(i): i for i in xrange(100)}
    assert dct == "Hi"


def test_it2():
    dct = {str(i): i for i in xrange(100)}
    assert dct == "Hi", str(dct) + " shall sound friendly"

你可以尝试:

$ py.test

你会看到:

$ py.test
========================================== test session starts ===========================================
platform linux2 -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2
collected 2 items

test_it.py FF

================================================ FAILURES ================================================
________________________________________________ test_it _________________________________________________

    def test_it():
        dct = {str(i): i for i in xrange(100)}
>       assert dct == "Hi"
E       assert {'0': 0, '1': 1, '10': 10, '11': 11, ...} == 'Hi'

test_it.py:3: AssertionError
________________________________________________ test_it2 ________________________________________________

    def test_it2():
        dct = {str(i): i for i in xrange(100)}
>       assert dct == "Hi", str(dct) + " shall sound friendly"
E       AssertionError: {'24': 24, '25': 25, '26': 26, '27': 27, '20': 20, '21': 21, '22': 22, '23': 23, '28': 28, '29': 29, '0': 0, '4': 4, '8': 8, '59': 59, '58': 58, '55': 55, '54': 54, '57': 57, '56': 56, '51'
: 51, '50': 50, '53': 53, '52': 52, '88': 88, '89': 89, '82': 82, '83': 83, '80': 80, '81': 81, '86': 86, '87': 87, '84': 84, '85': 85, '3': 3, '7': 7, '39': 39, '38': 38, '33': 33, '32': 32, '31': 31, '30': 30, '
37': 37, '36': 36, '35': 35, '34': 34, '60': 60, '61': 61, '62': 62, '63': 63, '64': 64, '65': 65, '66': 66, '67': 67, '68': 68, '69': 69, '2': 2, '6': 6, '99': 99, '98': 98, '91': 91, '90': 90, '93': 93, '92': 92
, '95': 95, '94': 94, '97': 97, '96': 96, '11': 11, '10': 10, '13': 13, '12': 12, '15': 15, '14': 14, '17': 17, '16': 16, '19': 19, '18': 18, '48': 48, '49': 49, '46': 46, '47': 47, '44': 44, '45': 45, '42': 42, '
43': 43, '40': 40, '41': 41, '1': 1, '5': 5, '9': 9, '77': 77, '76': 76, '75': 75, '74': 74, '73': 73, '72': 72, '71': 71, '70': 70, '79': 79, '78': 78} shall sound friendly

test_it.py:8: AssertionError
======================================== 2 failed in 0.02 seconds ========================================