assertrepr_compare: use safeformat with -vv
This commit is contained in:
		
							parent
							
								
									fbb7f663be
								
							
						
					
					
						commit
						d91ff0af8a
					
				|  | @ -0,0 +1 @@ | ||||||
|  | Display untruncated assertion message with ``-vv``. | ||||||
|  | @ -7,6 +7,7 @@ from typing import Optional | ||||||
| 
 | 
 | ||||||
| import _pytest._code | import _pytest._code | ||||||
| from _pytest import outcomes | from _pytest import outcomes | ||||||
|  | from _pytest._io.saferepr import safeformat | ||||||
| from _pytest._io.saferepr import saferepr | from _pytest._io.saferepr import saferepr | ||||||
| from _pytest.compat import ATTRS_EQ_FIELD | from _pytest.compat import ATTRS_EQ_FIELD | ||||||
| 
 | 
 | ||||||
|  | @ -123,13 +124,21 @@ def isiterable(obj): | ||||||
| 
 | 
 | ||||||
| def assertrepr_compare(config, op, left, right): | def assertrepr_compare(config, op, left, right): | ||||||
|     """Return specialised explanations for some operators/operands""" |     """Return specialised explanations for some operators/operands""" | ||||||
|     maxsize = (80 - 15 - len(op) - 2) // 2  # 15 chars indentation, 1 space around op |     verbose = config.getoption("verbose") | ||||||
|     left_repr = saferepr(left, maxsize=maxsize) |     if verbose > 1: | ||||||
|     right_repr = saferepr(right, maxsize=maxsize) |         left_repr = safeformat(left) | ||||||
|  |         right_repr = safeformat(right) | ||||||
|  |     else: | ||||||
|  |         # XXX: "15 chars indentation" is wrong | ||||||
|  |         #      ("E       AssertionError: assert "); should use term width. | ||||||
|  |         maxsize = ( | ||||||
|  |             80 - 15 - len(op) - 2 | ||||||
|  |         ) // 2  # 15 chars indentation, 1 space around op | ||||||
|  |         left_repr = saferepr(left, maxsize=maxsize) | ||||||
|  |         right_repr = saferepr(right, maxsize=maxsize) | ||||||
| 
 | 
 | ||||||
|     summary = "{} {} {}".format(left_repr, op, right_repr) |     summary = "{} {} {}".format(left_repr, op, right_repr) | ||||||
| 
 | 
 | ||||||
|     verbose = config.getoption("verbose") |  | ||||||
|     explanation = None |     explanation = None | ||||||
|     try: |     try: | ||||||
|         if op == "==": |         if op == "==": | ||||||
|  |  | ||||||
|  | @ -190,11 +190,12 @@ class TestAssertionRewrite: | ||||||
|             pass |             pass | ||||||
| 
 | 
 | ||||||
|         msg = getmsg(f, {"cls": X}).splitlines() |         msg = getmsg(f, {"cls": X}).splitlines() | ||||||
|         if verbose > 0: |         if verbose > 1: | ||||||
| 
 |             assert msg == ["assert {!r} == 42".format(X), "  -{!r}".format(X), "  +42"] | ||||||
|  |         elif verbose > 0: | ||||||
|             assert msg == [ |             assert msg == [ | ||||||
|                 "assert <class 'test_...e.<locals>.X'> == 42", |                 "assert <class 'test_...e.<locals>.X'> == 42", | ||||||
|                 "  -<class 'test_assertrewrite.TestAssertionRewrite.test_name.<locals>.X'>", |                 "  -{!r}".format(X), | ||||||
|                 "  +42", |                 "  +42", | ||||||
|             ] |             ] | ||||||
|         else: |         else: | ||||||
|  | @ -206,9 +207,17 @@ class TestAssertionRewrite: | ||||||
|         def f(): |         def f(): | ||||||
|             assert "1234567890" * 5 + "A" == "1234567890" * 5 + "B" |             assert "1234567890" * 5 + "A" == "1234567890" * 5 + "B" | ||||||
| 
 | 
 | ||||||
|         assert getmsg(f).splitlines()[0] == ( |         msg = getmsg(f).splitlines()[0] | ||||||
|             "assert '123456789012...901234567890A' == '123456789012...901234567890B'" |         if request.config.getoption("verbose") > 1: | ||||||
|         ) |             assert msg == ( | ||||||
|  |                 "assert '12345678901234567890123456789012345678901234567890A' " | ||||||
|  |                 "== '12345678901234567890123456789012345678901234567890B'" | ||||||
|  |             ) | ||||||
|  |         else: | ||||||
|  |             assert msg == ( | ||||||
|  |                 "assert '123456789012...901234567890A' " | ||||||
|  |                 "== '123456789012...901234567890B'" | ||||||
|  |             ) | ||||||
| 
 | 
 | ||||||
|     def test_dont_rewrite_if_hasattr_fails(self, request): |     def test_dont_rewrite_if_hasattr_fails(self, request): | ||||||
|         class Y: |         class Y: | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue