Changeset 97

Show
Ignore:
Timestamp:
07/20/06 13:19:07 (2 years ago)
Author:
mk
Message:

Search for classes that define special methods (like setUp/tearDown) and files that match test_* or *_test and assume they contain unit tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/mk/cheesecake/cheesecake_index.py

    r96 r97  
    7676    >>> has_extension("foobar.pyc", ".py") 
    7777    False 
    78     """ 
    79     return os.path.splitext(filename)[1] == ext 
     78 
     79    This function is case insensitive. 
     80        >>> has_extension("FOOBAR.PY", ".py") 
     81        True 
     82    """ 
     83    return os.path.splitext(filename.lower())[1] == ext.lower() 
    8084 
    8185def discover_file_type(filename): 
     
    102106    >>> discover_file_type('examples/runthis.py') 
    103107    'demo' 
     108 
     109    >>> test_files = ['ut/test_this_and_that.py', 
     110    ...               'another_test.py', 
     111    ...               'TEST_MY_MODULE.PY'] 
     112    >>> for filename in test_files: 
     113    ...     assert discover_file_type(filename) == 'test', filename 
     114 
     115    >>> discover_file_type('this_is_not_a_test_really.py') 
     116    'module' 
    104117    """ 
    105118    dirs = filename.split(os.path.sep) 
     
    119132            elif dir in ['doc', 'docs', 'demo', 'example', 'examples']: 
    120133                return 'demo' 
     134 
     135        # Most test frameworks look for files starting with "test_". 
     136        # py.test also looks at files with trailing "_test". 
     137        if filename.lower().startswith('test_') or \ 
     138               os.path.splitext(filename)[0].lower().endswith('_test'): 
     139            return 'test' 
     140 
    121141        return 'module' 
    122142 
     
    832852    max_value = 30 
    833853 
    834     def compute(self, doctests_count): 
     854    def compute(self, doctests_count, files_list, classes, methods): 
     855        frameworks_found = False 
     856 
    835857        if doctests_count > 0: 
     858            frameworks_found = True 
     859 
     860        if get_files_of_type(files_list, 'test'): 
     861            frameworks_found = True 
     862 
     863        for method in methods: 
     864            if self._is_test_method(method): 
     865                frameworks_found = True 
     866                break 
     867 
     868        if frameworks_found: 
    836869            self.value = self.max_value 
    837             self.details = "found doctests" 
     870            self.details = "use one or more of known test frameworks" 
    838871        else: 
    839872            self.value = 0 
    840             self.details = "doesn't use any of known test frameworks" 
     873            self.details = "don't use any of known test frameworks" 
    841874 
    842875        return self.value 
     876 
     877    def _is_test_method(self, method): 
     878        nose_methods = ['setup', 'setup_package', 'setup_module', 'setUp', 
     879                        'setUpPackage', 'setUpModule', 
     880                        'teardown', 'teardown_package', 'teardown_module', 
     881                        'tearDown', 'tearDownModule', 'tearDownPackage'] 
     882 
     883        for test_method in nose_methods: 
     884            if method.endswith(test_method): 
     885                return True 
     886        return False 
    843887 
    844888class IndexPyLint(Index): 
     
    13151359                              'files_list', 
    13161360                              'functions', 
     1361                              'classes', 
     1362                              'methods', 
    13171363                              'object_cnt', 
    13181364                              'package_dir']) 
     
    13351381          classes : list 
    13361382              List of all classes defined in package sources. 
     1383          methods : list 
     1384              List of all methods defined in package sources. 
    13371385          object_cnt : int 
    13381386              Number of documentable objects found in all package modules. 
     
    13501398        self.functions = [] 
    13511399        self.classes = [] 
     1400        self.methods = [] 
    13521401 
    13531402        # Parse all application files and count objects 
     
    13621411            self.functions += code.functions 
    13631412            self.classes += code.classes 
     1413            self.methods += code.methods 
    13641414            self.doctests_count += code.doctests_count 
    13651415 
  • branches/mk/tests/unit/_helper_cheesecake.py

    r71 r97  
    1717    def __call__(self, *args, **kwds): 
    1818        pass 
     19 
     20 
     21def create_empty_file(file_path): 
     22    fd = file(file_path, "w") 
     23    fd.close() 
     24 
     25def create_empty_files_in_directory(files, directory): 
     26    for filename in files: 
     27        create_empty_file(os.path.join(directory, filename)) 
  • branches/mk/tests/unit/_mockup_cheesecake.py

    r80 r97  
    77from cheesecake.cheesecake_index import Cheesecake 
    88from cheesecake.cheesecake_index import CheesecakeIndex 
     9 
     10from _helper_cheesecake import create_empty_files_in_directory 
    911 
    1012 
     
    5456 
    5557    def create_empty_files(self, files): 
    56         for f in files: 
    57             fd = file(os.path.join(self.temp_top_dir, f), "w") 
    58             fd.close() 
     58        create_empty_files_in_directory(files, self.temp_top_dir) 
    5959 
    6060    def prefix_with_package_name(self, files): 
  • branches/mk/tests/unit/test_index_use_test_framework.py

    r89 r97  
    1010from cheesecake.cheesecake_index import IndexUseTestFramework 
    1111from cheesecake import logger 
     12 
     13from _helper_cheesecake import create_empty_files_in_directory 
    1214 
    1315 
     
    3335""" 
    3436 
     37test_contents = """ 
     38class TestSomeModule: 
     39    def setUp(self): 
     40        pass 
     41    def test_this(self): 
     42        pass 
     43    def test_that(self): 
     44        pass 
     45""" 
     46 
    3547class TestIndexUseTestFramework(object): 
    3648    def setUp(self): 
     
    4153            shutil.rmtree(self.sandbox_dir) 
    4254 
    43     def test_use_test_framework(self): 
    44         "Test use_test_framework index." 
     55    def test_doctest(self): 
     56        "Test use_test_framework index with package that uses doctest." 
     57        def setup(project_dir): 
     58            main_filename = os.path.join(project_dir, 'main.py') 
     59            dump_str_to_file(main_contents, main_filename) 
     60 
     61        def asserts(cheesecake): 
     62            assert cheesecake.functions == ['main.function_without_docstring'] 
     63            assert cheesecake.classes == ['main.SomeClass'] 
     64 
     65        self._run_it(setup, asserts) 
     66 
     67    def _run_it(self, setup=None, asserts=None): 
    4568        package_name = 'index_test' 
    4669 
     
    4871        os.mkdir(project_dir) 
    4972 
    50         main_filename = os.path.join(project_dir, 'main.py') 
    51         dump_str_to_file(main_contents, main_filename
     73        if setup: 
     74            setup(project_dir
    5275 
    5376        logger.setconsumer('console', logger.STDOUT) 
     
    6891        cheesecake.walk_pkg() 
    6992 
    70         assert cheesecake.functions == ['main.function_without_docstring'] 
    71         assert cheesecake.classes == ['main.SomeClass'] 
     93        if asserts: 
     94            asserts(cheesecake) 
    7295 
    7396        index = IndexUseTestFramework() 
    7497        index.compute_with(cheesecake) 
    7598 
    76         # Use of doctest should be discovered and package should get maximum score. 
     99        # Use of test framework should be discovered and package should get maximum score. 
    77100        print "Index: %d/%d -- %s" % (index.value, index.max_value, index.details) 
    78101        assert index.value == index.max_value 
     102 
     103    def test_special_filenames_1(self): 
     104        "Test use_test_framework index with packages that uses test_* filenames." 
     105        def setup(project_dir): 
     106            files = ['some_module.py', 'README', 'test_some_module.py'] 
     107            create_empty_files_in_directory(files, project_dir) 
     108 
     109        self._run_it(setup) 
     110 
     111    def test_special_filenames_2(self): 
     112        "Test use_test_framework index with packages that uses *_test filenames." 
     113        def setup(project_dir): 
     114            files = ['some_module.py', 'README', 'some_module_test.py'] 
     115            create_empty_files_in_directory(files, project_dir) 
     116 
     117        self._run_it(setup) 
     118 
     119    def test_special_methods(self): 
     120        "Test use_test_framework index with packages that uses setUp/tearDown methods." 
     121        def setup(project_dir): 
     122            test_filename = os.path.join(project_dir, 'do_checks.py') 
     123            dump_str_to_file(test_contents, test_filename) 
     124 
     125        self._run_it(setup)