def write_bare_doctest(module, output_filename): total_tests_written = 0 outfile = cStringIO.StringIO() screenshots_rel = os.path.relpath( SCREENSHOTS_ROOT, os.path.dirname(output_filename)) import_path, import_name = find_import_name(module.filename) module_rel = os.path.relpath( import_path, os.path.dirname(output_filename)) outfile.write(dedent(r''' #!/usr/bin/env python # coding=utf-8 """ This file contains regression tests automatically generated by ``stbt auto-selftest``. These tests are intended to capture the behaviour of Frame Objects (and other helper functions that operate on a video-frame). Commit this file to git, re-run ``stbt auto-selftest`` whenever you make a change to your Frame Objects, and use ``git diff`` to see how your changes affect the behaviour of the Frame Object. NOTE: THE OUTPUT OF THE DOCTESTS BELOW IS NOT NECESSARILY "CORRECT" -- it merely documents the behaviour at the time that ``stbt auto-selftest`` was run. """ # pylint: disable=line-too-long import os import sys sys.path.insert(0, os.path.join( os.path.dirname(__file__), {module_rel})) from {name} import * # isort:skip pylint: disable=wildcard-import, import-error _FRAME_CACHE = {{}} def f(name): img = _FRAME_CACHE.get(name) if img is None: import cv2 filename = os.path.join(os.path.dirname(__file__), {screenshots_rel}, name) img = cv2.imread(filename) assert img is not None, "Failed to load %s" % filename img.flags.writeable = False _FRAME_CACHE[name] = img return img '''.format(name=import_name, screenshots_rel=repr(screenshots_rel), module_rel=repr(module_rel)))) for x in module.items: total_tests_written += write_test_for_class(x, outfile) if total_tests_written > 0: with open(output_filename, 'w') as f: f.write(outfile.getvalue()) return total_tests_written
def _import_by_filename(filename_): from importlib import import_module import_dir, import_name = find_import_name(filename_) sys.path.insert(0, import_dir) try: module = import_module(import_name) finally: # If the test function is not in a module we will need to leave # PYTHONPATH modified here so one python file in the test-pack can # import other files from the same directory. We also have to be # careful of modules that mess with sys.path: if '.' in import_name and sys.path[0] == import_dir: sys.path.pop(0) return module
def _import_by_filename(filename): from importlib import import_module import_dir, import_name = find_import_name(filename) sys.path.insert(0, import_dir) return import_module(import_name)