diff --git a/CHANGELOG b/CHANGELOG index 104f15eac..641bbf708 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ Changes between 2.3.4 and 2.3.5dev ----------------------------------- +- fix issue171 - in assertion rewriting, show the repr of some + global variables + - fix option help for "-k" - move long description of distribution into README.rst @@ -119,7 +122,7 @@ Changes between 2.2.4 and 2.3.0 - fix issue202 - better automatic names for parametrized test functions - fix issue139 - introduce @pytest.fixture which allows direct scoping - and parametrization of funcarg factories. + and parametrization of funcarg factories. - fix issue198 - conftest fixtures were not found on windows32 in some circumstances with nested directory structures due to path manipulation issues - fix issue193 skip test functions with were parametrized with empty diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 990dd425e..9a0ebda3d 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -262,6 +262,9 @@ def rewrite_asserts(mod): _saferepr = py.io.saferepr from _pytest.assertion.util import format_explanation as _format_explanation +def _should_repr_global_name(obj): + return not hasattr(obj, "__name__") and not py.builtin.callable(obj) + def _format_boolop(explanations, is_or): return "(" + (is_or and " or " or " and ").join(explanations) + ")" @@ -473,11 +476,12 @@ class AssertionRewriter(ast.NodeVisitor): return self.statements def visit_Name(self, name): - # Check if the name is local or not. + # Display the repr of the name if it's a local variable or + # _should_repr_global_name() thinks it's acceptable. locs = ast.Call(self.builtin("locals"), [], [], None, None) - globs = ast.Call(self.builtin("globals"), [], [], None, None) - ops = [ast.In(), ast.IsNot()] - test = ast.Compare(ast.Str(name.id), ops, [locs, globs]) + inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs]) + dorepr = self.helper("should_repr_global_name", name) + test = ast.BoolOp(ast.Or(), [inlocs, dorepr]) expr = ast.IfExp(test, self.display(name), ast.Str(name.id)) return name, self.explanation_param(expr) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index d421d2101..9b73c337a 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -107,7 +107,15 @@ class TestAssertionRewrite: assert getmsg(f) == "assert False" def f(): assert a_global - assert getmsg(f, {"a_global" : False}) == "assert a_global" + assert getmsg(f, {"a_global" : False}) == "assert False" + def f(): + assert sys == 42 + assert getmsg(f, {"sys" : sys}) == "assert sys == 42" + def f(): + assert cls == 42 + class X(object): + pass + assert getmsg(f, {"cls" : X}) == "assert cls == 42" def test_assert_already_has_message(self): def f(): @@ -232,7 +240,7 @@ class TestAssertionRewrite: def test_attribute(self): class X(object): g = 3 - ns = {"X" : X, "x" : X()} + ns = {"x" : X} def f(): assert not x.g assert getmsg(f, ns) == """assert not 3