Skip to content

Commit

Permalink
[UnitTests] Changed accidental use of pytest fixtures to a NameError.
Browse files Browse the repository at this point in the history
- Previously, a fixture function defined in a module was accessible
  through the global scope, and the function definition is accessible
  if a test function uses that name but fails to declare the fixture
  as a parameter.  Now, it will result in a NameError instead.
  • Loading branch information
Lunderberg committed Jun 7, 2021
1 parent 692c50b commit 9b59706
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def pytest_generate_tests(metafunc):

def pytest_collection_modifyitems(config, items):
tvm.testing._count_num_fixture_uses(items)
tvm.testing._remove_global_fixture_definitions(items)


def pytest_sessionfinish(session, exitstatus):
Expand Down
17 changes: 17 additions & 0 deletions python/tvm/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,23 @@ def _count_num_fixture_uses(items):
fixturedef.func.num_tests_use_this += 1


def _remove_global_fixture_definitions(items):
# Helper function, removes fixture definitions from the global
# variables of the modules they were defined in. This is intended
# to improve readability of error messages by giving a NameError
# if a test function accesses a pytest fixture but doesn't include
# it as an argument. Should be called from
# pytest_collection_modifyitems().

modules = set(item.module for item in items)

for module in modules:
for name in dir(module):
obj = getattr(module, name)
if hasattr(obj, "_pytestfixturefunction"):
delattr(module, name)


def identity_after(x, sleep):
"""Testing function to return identity after sleep
Expand Down

0 comments on commit 9b59706

Please sign in to comment.