Using ini-file option instead of cmd option.
This commit is contained in:
parent
0fb52416b1
commit
d638da5821
|
@ -23,11 +23,10 @@ def pytest_addoption(parser):
|
||||||
test modules on import to provide assert
|
test modules on import to provide assert
|
||||||
expression information.""",
|
expression information.""",
|
||||||
)
|
)
|
||||||
|
parser.addini(
|
||||||
group = parser.getgroup("experimental")
|
"enable_assertion_pass_hook",
|
||||||
group.addoption(
|
type="bool",
|
||||||
"--enable-assertion-pass-hook",
|
default="False",
|
||||||
action="store_true",
|
|
||||||
help="Enables the pytest_assertion_pass hook."
|
help="Enables the pytest_assertion_pass hook."
|
||||||
"Make sure to delete any previously generated pyc cache files.",
|
"Make sure to delete any previously generated pyc cache files.",
|
||||||
)
|
)
|
||||||
|
|
|
@ -604,6 +604,10 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.module_path = module_path
|
self.module_path = module_path
|
||||||
self.config = config
|
self.config = config
|
||||||
|
if config is not None:
|
||||||
|
self.enable_assertion_pass_hook = config.getini("enable_assertion_pass_hook")
|
||||||
|
else:
|
||||||
|
self.enable_assertion_pass_hook = False
|
||||||
|
|
||||||
def run(self, mod):
|
def run(self, mod):
|
||||||
"""Find all assert statements in *mod* and rewrite them."""
|
"""Find all assert statements in *mod* and rewrite them."""
|
||||||
|
@ -745,7 +749,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
format_dict = ast.Dict(keys, list(current.values()))
|
format_dict = ast.Dict(keys, list(current.values()))
|
||||||
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
|
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
|
||||||
name = "@py_format" + str(next(self.variable_counter))
|
name = "@py_format" + str(next(self.variable_counter))
|
||||||
if getattr(self.config._ns, "enable_assertion_pass_hook", False):
|
if self.enable_assertion_pass_hook:
|
||||||
self.format_variables.append(name)
|
self.format_variables.append(name)
|
||||||
self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
|
self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
|
||||||
return ast.Name(name, ast.Load())
|
return ast.Name(name, ast.Load())
|
||||||
|
@ -782,7 +786,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
self.variables = []
|
self.variables = []
|
||||||
self.variable_counter = itertools.count()
|
self.variable_counter = itertools.count()
|
||||||
|
|
||||||
if getattr(self.config._ns, "enable_assertion_pass_hook", False):
|
if self.enable_assertion_pass_hook:
|
||||||
self.format_variables = []
|
self.format_variables = []
|
||||||
|
|
||||||
self.stack = []
|
self.stack = []
|
||||||
|
@ -797,7 +801,7 @@ class AssertionRewriter(ast.NodeVisitor):
|
||||||
top_condition, module_path=self.module_path, lineno=assert_.lineno
|
top_condition, module_path=self.module_path, lineno=assert_.lineno
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if getattr(self.config._ns, "enable_assertion_pass_hook", False):
|
if self.enable_assertion_pass_hook:
|
||||||
### Experimental pytest_assertion_pass hook
|
### Experimental pytest_assertion_pass hook
|
||||||
negation = ast.UnaryOp(ast.Not(), top_condition)
|
negation = ast.UnaryOp(ast.Not(), top_condition)
|
||||||
msg = self.pop_format_context(ast.Str(explanation))
|
msg = self.pop_format_context(ast.Str(explanation))
|
||||||
|
|
|
@ -503,7 +503,7 @@ def pytest_assertion_pass(item, lineno, orig, expl):
|
||||||
This hook is still *experimental*, so its parameters or even the hook itself might
|
This hook is still *experimental*, so its parameters or even the hook itself might
|
||||||
be changed/removed without warning in any future pytest release.
|
be changed/removed without warning in any future pytest release.
|
||||||
|
|
||||||
It should be enabled using the `--enable-assertion-pass-hook` command line option.
|
It should be enabled using the `enable_assertion_pass_hook` ini-file option.
|
||||||
Remember to clean the .pyc files in your project directory and interpreter libraries.
|
Remember to clean the .pyc files in your project directory and interpreter libraries.
|
||||||
|
|
||||||
If you find this hook useful, please share your feedback opening an issue.
|
If you find this hook useful, please share your feedback opening an issue.
|
||||||
|
|
|
@ -1308,6 +1308,7 @@ class TestEarlyRewriteBailout:
|
||||||
|
|
||||||
|
|
||||||
class TestAssertionPass:
|
class TestAssertionPass:
|
||||||
|
|
||||||
def test_hook_call(self, testdir):
|
def test_hook_call(self, testdir):
|
||||||
testdir.makeconftest(
|
testdir.makeconftest(
|
||||||
"""
|
"""
|
||||||
|
@ -1315,6 +1316,12 @@ class TestAssertionPass:
|
||||||
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))
|
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
testdir.makeini("""
|
||||||
|
[pytest]
|
||||||
|
enable_assertion_pass_hook = True
|
||||||
|
""")
|
||||||
|
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
def test_simple():
|
def test_simple():
|
||||||
|
@ -1326,7 +1333,7 @@ class TestAssertionPass:
|
||||||
assert a+b == c+d
|
assert a+b == c+d
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
result = testdir.runpytest("--enable-assertion-pass-hook")
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines(
|
result.stdout.fnmatch_lines(
|
||||||
"*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*"
|
"*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*"
|
||||||
)
|
)
|
||||||
|
@ -1342,6 +1349,11 @@ class TestAssertionPass:
|
||||||
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass
|
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass
|
||||||
)
|
)
|
||||||
|
|
||||||
|
testdir.makeini("""
|
||||||
|
[pytest]
|
||||||
|
enable_assertion_pass_hook = True
|
||||||
|
""")
|
||||||
|
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
def test_simple():
|
def test_simple():
|
||||||
|
@ -1353,7 +1365,7 @@ class TestAssertionPass:
|
||||||
assert a+b == c+d
|
assert a+b == c+d
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
result = testdir.runpytest("--enable-assertion-pass-hook")
|
result = testdir.runpytest()
|
||||||
result.assert_outcomes(passed=1)
|
result.assert_outcomes(passed=1)
|
||||||
|
|
||||||
def test_hook_not_called_without_cmd_option(self, testdir, monkeypatch):
|
def test_hook_not_called_without_cmd_option(self, testdir, monkeypatch):
|
||||||
|
@ -1374,6 +1386,11 @@ class TestAssertionPass:
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
testdir.makeini("""
|
||||||
|
[pytest]
|
||||||
|
enable_assertion_pass_hook = False
|
||||||
|
""")
|
||||||
|
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
"""
|
"""
|
||||||
def test_simple():
|
def test_simple():
|
||||||
|
|
Loading…
Reference in New Issue