Test rewriting assertion when __name__ fails
Pytest rewrites assertions so that the items on each side of a comoparison will have easier-to-read names in case of an assertion error. Before doing this, it checks to make sure the object doesn't have a __name__ attribute; however, it uses `hasattr` so if the objects __getattr__ is broken then the test failure message will be the stack trace for this failure instead of a rewritten assertion.
This commit is contained in:
		
							parent
							
								
									71a745270a
								
							
						
					
					
						commit
						acb3e8e8a7
					
				|  | @ -180,6 +180,27 @@ class TestAssertionRewrite(object): | ||||||
| 
 | 
 | ||||||
|         assert getmsg(f, {"cls": X}) == "assert cls == 42" |         assert getmsg(f, {"cls": X}) == "assert cls == 42" | ||||||
| 
 | 
 | ||||||
|  |     def test_dont_rewrite_if_hasattr_fails(self): | ||||||
|  |         class Y(object): | ||||||
|  |             """ A class whos getattr fails, but not with `AttributeError` """ | ||||||
|  | 
 | ||||||
|  |             def __getattr__(self, attribute_name): | ||||||
|  |                 raise KeyError() | ||||||
|  | 
 | ||||||
|  |             def __repr__(self): | ||||||
|  |                 return "Y" | ||||||
|  | 
 | ||||||
|  |             def __init__(self): | ||||||
|  |                 self.foo = 3 | ||||||
|  | 
 | ||||||
|  |         def f(): | ||||||
|  |             assert cls().foo == 2  # noqa | ||||||
|  | 
 | ||||||
|  |         message = getmsg(f, {"cls": Y}) | ||||||
|  |         assert "assert 3 == 2" in message | ||||||
|  |         assert "+  where 3 = Y.foo" in message | ||||||
|  |         assert "+    where Y = cls()" in message | ||||||
|  | 
 | ||||||
|     def test_assert_already_has_message(self): |     def test_assert_already_has_message(self): | ||||||
|         def f(): |         def f(): | ||||||
|             assert False, "something bad!" |             assert False, "something bad!" | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue