Skip to content

Commit

Permalink
Rewrite asserts in test-modules loaded very early in the startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Aug 2, 2016
1 parent ea6191a commit d2ff804
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
33 changes: 15 additions & 18 deletions _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import struct
import sys
import types
from fnmatch import fnmatch

import py
from _pytest.assertion import util
Expand Down Expand Up @@ -144,28 +145,24 @@ def _should_rewrite(self, name, fn_pypath, state):
if fn_pypath.basename == 'conftest.py':
state.trace("rewriting conftest file: %r" % (fn,))
return True
elif self.session is not None:

if self.session is not None:
if self.session.isinitpath(fn):
state.trace("matched test file (was specified on cmdline): %r" %
(fn,))
return True
else:
# modules not passed explicitly on the command line are only
# rewritten if they match the naming convention for test files
session = self.session # avoid a cycle here
self.session = None
try:
for pat in self.fnpats:
if fn_pypath.fnmatch(pat):
state.trace("matched test file %r" % (fn,))
return True
finally:
self.session = session
del session
else:
for marked in self._must_rewrite:
if marked.startswith(name):
return True

# modules not passed explicitly on the command line are only
# rewritten if they match the naming convention for test files
for pat in self.fnpats:
if fnmatch(fn_pypath.basename, pat):
state.trace("matched test file %r" % (fn,))
return True

for marked in self._must_rewrite:
if marked.startswith(name):
return True

return False

def mark_rewrite(self, *names):
Expand Down
10 changes: 10 additions & 0 deletions testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ def mywarn(code, msg):
hook.mark_rewrite('_pytest')
assert '_pytest' in warnings[0][1]

def test_rewrite_module_imported_from_conftest(self, testdir):
testdir.makeconftest('''
import test_rewrite_module_imported
''')
testdir.makepyfile(test_rewrite_module_imported='''
def test_rewritten():
assert "@py_builtins" in globals()
''')
assert testdir.runpytest_subprocess().ret == 0


class TestAssertionRewriteHookDetails(object):
def test_loader_is_package_false_for_module(self, testdir):
Expand Down

0 comments on commit d2ff804

Please sign in to comment.