目前,当我的一个测试失败时,它会打印出单元测试的参数,然后是堆栈跟踪。但是,如果参数是大型 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):
[...]
Run Code Online (Sandbox Code Playgroud)
好问题。
搜索和测试各种命令行选项py.test,我没有找到任何方法来获得完整的词典打印输出。
这可能是有充分理由的,无休止的打印输出并不是很有用。
虽然在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"
Run Code Online (Sandbox Code Playgroud)
你可以试试:
$ py.test
Run Code Online (Sandbox Code Playgroud)
你会看到:
$ 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 ========================================
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5166 次 |
| 最近记录: |