"""Runs test cases from the Python REPL."""__author__='Paul Landes'fromdataclassesimportdataclass,fieldfromunittestimportmainfromunittest.runnerimportTextTestResultimportsysimportloggingfrompathlibimportPathfromzensols.introspectimportClassImporterlogger=logging.getLogger(__name__)
[docs]@dataclassclassUnitTester(object):"""A class that runs :mod:`unittest` unit test cases for the rapid prototyping use case. It does this by reloading the unit test case module, and then runs it for every invocation of this :class:`typing.Callable`. """module_name:str=field()"""The name of the module, which is the Python source file name sans extension. """test_path:Path=field(default=Path('tests'))"""The path to the source file. This is added to :obj:`sys.path` if non-``None``. """
[docs]defrun(self)->TextTestResult:"""Run all test cases in the module identified by :obj:`module_name`."""iflogger.isEnabledFor(logging.INFO):logger.info(f'running tests in module: {self.module_name}')ifself.test_pathisnotNone:test_path:str=str(self.test_path)iftest_pathnotinsys.path:sys.path.append(str(test_path))ClassImporter.get_module(self.module_name,reload=True)test=main(module=self.module_name,exit=False)returntest.result
def__call__(self)->bool:"""Calls :meth:`run` and returns if it was successful."""res:TextTestResult=self.run()returnres.wasSuccessful()