Using ini-file option instead of cmd option.

This commit is contained in:
Victor Maryama 2019-06-26 17:08:09 +02:00
parent 0fb52416b1
commit d638da5821
4 changed files with 31 additions and 11 deletions

View File

@ -23,11 +23,10 @@ def pytest_addoption(parser):
test modules on import to provide assert
expression information.""",
)
group = parser.getgroup("experimental")
group.addoption(
"--enable-assertion-pass-hook",
action="store_true",
parser.addini(
"enable_assertion_pass_hook",
type="bool",
default="False",
help="Enables the pytest_assertion_pass hook."
"Make sure to delete any previously generated pyc cache files.",
)

View File

@ -604,6 +604,10 @@ class AssertionRewriter(ast.NodeVisitor):
super().__init__()
self.module_path = module_path
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):
"""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()))
form = ast.BinOp(expl_expr, ast.Mod(), format_dict)
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.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form))
return ast.Name(name, ast.Load())
@ -782,7 +786,7 @@ class AssertionRewriter(ast.NodeVisitor):
self.variables = []
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.stack = []
@ -797,7 +801,7 @@ class AssertionRewriter(ast.NodeVisitor):
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
negation = ast.UnaryOp(ast.Not(), top_condition)
msg = self.pop_format_context(ast.Str(explanation))

View File

@ -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
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.
If you find this hook useful, please share your feedback opening an issue.

View File

@ -1308,6 +1308,7 @@ class TestEarlyRewriteBailout:
class TestAssertionPass:
def test_hook_call(self, testdir):
testdir.makeconftest(
"""
@ -1315,6 +1316,12 @@ class TestAssertionPass:
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))
"""
)
testdir.makeini("""
[pytest]
enable_assertion_pass_hook = True
""")
testdir.makepyfile(
"""
def test_simple():
@ -1326,7 +1333,7 @@ class TestAssertionPass:
assert a+b == c+d
"""
)
result = testdir.runpytest("--enable-assertion-pass-hook")
result = testdir.runpytest()
result.stdout.fnmatch_lines(
"*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
)
testdir.makeini("""
[pytest]
enable_assertion_pass_hook = True
""")
testdir.makepyfile(
"""
def test_simple():
@ -1353,7 +1365,7 @@ class TestAssertionPass:
assert a+b == c+d
"""
)
result = testdir.runpytest("--enable-assertion-pass-hook")
result = testdir.runpytest()
result.assert_outcomes(passed=1)
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(
"""
def test_simple():