From c4430e435476f19b6c46557253aa1bdcf9c2cef5 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 6 Mar 2018 10:32:41 +0100 Subject: [PATCH] extract _warn_about_missing_assertion into freestanding function --- _pytest/config.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index fe6e324a5..cdd996896 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -54,7 +54,7 @@ def main(args=None, plugins=None): tw = py.io.TerminalWriter(sys.stderr) for line in traceback.format_exception(*e.excinfo): tw.line(line.rstrip(), red=True) - tw.line("ERROR: could not load %s\n" % (e.path), red=True) + tw.line("ERROR: could not load %s\n" % (e.path,), red=True) return 4 else: try: @@ -1016,7 +1016,7 @@ class Config(object): mode = 'plain' else: self._mark_plugins_for_rewrite(hook) - self._warn_about_missing_assertion(mode) + _warn_about_missing_assertion(mode) def _mark_plugins_for_rewrite(self, hook): """ @@ -1043,23 +1043,6 @@ class Config(object): for name in _iter_rewritable_modules(package_files): hook.mark_rewrite(name) - def _warn_about_missing_assertion(self, mode): - try: - assert False - except AssertionError: - pass - else: - if mode == 'plain': - sys.stderr.write("WARNING: ASSERTIONS ARE NOT EXECUTED" - " and FAILING TESTS WILL PASS. Are you" - " using python -O?") - else: - sys.stderr.write("WARNING: assertions not in test modules or" - " plugins will be ignored" - " because assert statements are not executed " - "by the underlying Python interpreter " - "(are you using python -O?)\n") - def _preparse(self, args, addopts=True): if addopts: args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args @@ -1233,6 +1216,29 @@ class Config(object): return self.getoption(name, skip=True) +def _assertion_supported(): + try: + assert False + except AssertionError: + return True + else: + return False + + +def _warn_about_missing_assertion(mode): + if not _assertion_supported(): + if mode == 'plain': + sys.stderr.write("WARNING: ASSERTIONS ARE NOT EXECUTED" + " and FAILING TESTS WILL PASS. Are you" + " using python -O?") + else: + sys.stderr.write("WARNING: assertions not in test modules or" + " plugins will be ignored" + " because assert statements are not executed " + "by the underlying Python interpreter " + "(are you using python -O?)\n") + + def exists(path, ignore=EnvironmentError): try: return path.check()