Skip to content

Instantly share code, notes, and snippets.

@L3viathan
Created September 21, 2023 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L3viathan/241f092983524f8b94190fa2666e351d to your computer and use it in GitHub Desktop.
Save L3viathan/241f092983524f8b94190fa2666e351d to your computer and use it in GitHub Desktop.
Tiny pytest plugin that allows collecting any file regardless of extension. If you don't manually specify test files, this will probably crash.
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
import pytest
class ModuleWithoutExtension(pytest.Module):
def _importtestmodule(self):
print(self.path, type(self.path))
spec = spec_from_loader(self.path.name, SourceFileLoader(self.path.name, str(self.path)))
mod = module_from_spec(spec)
spec.loader.exec_module(mod)
self.config.pluginmanager.consider_module(mod)
return mod
def pytest_collect_file(file_path, path, parent):
return ModuleWithoutExtension.from_parent(parent, path=file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment