def _get_expected_file(self, subname=None, strip_suffix=None): def remove_prefix_suffix(text, prefix, suffix): if text.startswith(prefix): text = text[len(prefix):] if suffix is not None and text.endswith(suffix): text = text[:len(text) - len(suffix)] return text # NB: we take __file__ from the module that defined the test # class, so we place the expect directory where the test script # lives, NOT where test/common_utils.py lives. module_id = self.__class__.__module__ munged_id = remove_prefix_suffix(self.id(), module_id + ".", strip_suffix) # Determine expected file based on environment expected_file_base = get_relative_path( os.path.realpath(sys.modules[module_id].__file__), "expect") # Set expected_file based on subname. expected_file = os.path.join(expected_file_base, munged_id) if subname: expected_file += "_" + subname expected_file += "_expect.pkl" if not ACCEPT and not os.path.exists(expected_file): raise RuntimeError( f"No expect file exists for {os.path.basename(expected_file)} in {expected_file}; " "to accept the current output, run:\n" f"python {__main__.__file__} {munged_id} --accept") return expected_file
def _get_expected_file(name=None): # Determine expected file based on environment expected_file_base = get_relative_path(os.path.realpath(__file__), "expect") # Note: for legacy reasons, the reference file names all had "ModelTest.test_" in their names # We hardcode it here to avoid having to re-generate the reference files expected_file = expected_file = os.path.join(expected_file_base, 'ModelTester.test_' + name) expected_file += "_expect.pkl" if not ACCEPT and not os.path.exists(expected_file): raise RuntimeError( f"No expect file exists for {os.path.basename(expected_file)} in {expected_file}; " "to accept the current output, re-run the failing test after setting the EXPECTTEST_ACCEPT " "env variable. For example: EXPECTTEST_ACCEPT=1 pytest test/test_models.py -k alexnet" ) return expected_file
def _get_expected_file(self, name=None): # NB: we take __file__ from the module that defined the test # class, so we place the expect directory where the test script # lives, NOT where test/common_utils.py lives. module_id = self.__class__.__module__ # Determine expected file based on environment expected_file_base = get_relative_path( os.path.realpath(sys.modules[module_id].__file__), "expect") # Note: for legacy reasons, the reference file names all had "ModelTest.test_" in their names # We hardcode it here to avoid having to re-generate the reference files expected_file = expected_file = os.path.join( expected_file_base, 'ModelTester.test_' + name) expected_file += "_expect.pkl" if not ACCEPT and not os.path.exists(expected_file): raise RuntimeError( f"No expect file exists for {os.path.basename(expected_file)} in {expected_file}; " "to accept the current output, run:\n" f"python {__main__.__file__} {munged_id} --accept") return expected_file