>>> FileFilter().should_test('file.py') True >>> FileFilter().should_test('/home/user/code/file.py') True >>> FileFilter().should_test('rel/path/file.py') True >>> FileFilter().should_test('adir') False >>> FileFilter().should_test('/abs/dir') False >>> FileFilter().should_test('config,yaml') False >>> FileFilter('.rb').should_test('what.rb') True """ return filename.endswith(self.ext) def __repr__(self): return "FileFilter(%r)" % (self.ext) filter_map = { 'python': FileFilter(), 'ruby': FileFilter('rb') } from_config = importutil.from_config_factory( 'file_filter', 'get_filter', filter_map)
self.ext = ext def should_test(self, filename): """Return True if the file should be tested. >>> FileFilter().should_test('file.py') True >>> FileFilter().should_test('/home/user/code/file.py') True >>> FileFilter().should_test('rel/path/file.py') True >>> FileFilter().should_test('adir') False >>> FileFilter().should_test('/abs/dir') False >>> FileFilter().should_test('config,yaml') False >>> FileFilter('.rb').should_test('what.rb') True """ return filename.endswith(self.ext) def __repr__(self): return "FileFilter(%r)" % (self.ext) filter_map = {'python': FileFilter(), 'ruby': FileFilter('rb')} from_config = importutil.from_config_factory('file_filter', 'get_filter', filter_map)
class UnitTest2Runner(ModuleRunner): default_command = ['unit2', '-v',] class DocTestRunner(FileTestRunner): default_command = ['python', '-m', 'doctest'] class PyTestRunner(FileTestRunner): default_command = ['py.test'] class FullSuiteRunner(FileTestRunner): def run_test(self, test_name): subprocess.call(self.command) test_runner_map = { 'file': FileTestRunner, 'unittest': ModuleRunner, 'doctest': DocTestRunner, 'testify': TestifyRunner, 'unittest2': UnitTest2Runner, 'pytest': PyTestRunner, 'full_suite': FullSuiteRunner, } from_config = importutil.from_config_factory( 'test_runner', 'get_runner', test_runner_map)
"""Map the filename to the name of the test for that file. Performs: - removes base path - swap the package name for 'tests' - replaces file extension of '.py' with '_test.py' >>> m = StandardMapper('/base') >>> m.get_test_filename('/base/package/more/file.py') 'tests/more/file_test.py' >>> m.get_test_filename('/base/tests/more/file_test.py') 'tests/more/file_test.py' """ filename = self.strip_basepath(filename) if self.is_testfile(filename): return filename filename = self.swap_base_package(filename) return self.replace_extension(filename) def __str__(self): return "StandardMapper(basepath=%s, test_package=%s)" % (self.basepath, self.test_package) mapper_name_map = { 'doctest': DocTestMapper, 'standard': StandardMapper, } from_config = importutil.from_config_factory( 'test_mapper', 'get_mapper', mapper_name_map)
'unit2', '-v', ] class DocTestRunner(FileTestRunner): default_command = ['python', '-m', 'doctest'] class PyTestRunner(FileTestRunner): default_command = ['py.test'] class FullSuiteRunner(FileTestRunner): def run_test(self, test_name): subprocess.call(self.command) test_runner_map = { 'file': FileTestRunner, 'unittest': ModuleRunner, 'doctest': DocTestRunner, 'testify': TestifyRunner, 'unittest2': UnitTest2Runner, 'pytest': PyTestRunner, 'full_suite': FullSuiteRunner, } from_config = importutil.from_config_factory('test_runner', 'get_runner', test_runner_map)