root/branches/mk/tests/unit/test_index_unit_tested.py

Revision 136, 3.9 kB (checked in by mk, 6 years ago)

Renamed test_index_use_test_framework to test_index_unit_tested.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 import os
2 import shutil
3 import tempfile
4
5 from math import ceil
6
7 import _path_cheesecake
8
9 from cheesecake.cheesecake_index import Cheesecake
10 from cheesecake.cheesecake_index import IndexUnitTested
11 from cheesecake import logger
12
13 from _helper_cheesecake import create_empty_files_in_directory
14
15
16 def dump_str_to_file(string, filename):
17     fd = file(filename, 'w')
18     fd.write(string)
19     fd.close()
20
21
22 main_contents = """
23 class SomeClass(object):
24     '''This is a docstring with doctests.
25
26     >>> print "Hello"
27     Hello
28     '''
29     def method(self):
30         'No doctest here.'
31         pass
32
33 def function_without_docstring(x):
34     return x**x
35 """
36
37 test_contents = """
38 class TestSomeModule:
39     def setUp(self):
40         pass
41     def test_this(self):
42         pass
43     def test_that(self):
44         pass
45 """
46
47 unittest_test_contents = """
48 class TestThisAndThat(unittest.TestCase):
49     def test_this(self):
50         pass
51 """
52
53 class TestIndexUnitTested(object):
54     def setUp(self):
55         self.sandbox_dir = tempfile.mkdtemp()
56
57     def tearDown(self):
58         if os.path.exists(self.sandbox_dir):
59             shutil.rmtree(self.sandbox_dir)
60
61     def test_doctest(self):
62         "Test unit_tested index with package that uses doctest."
63         def setup(project_dir):
64             main_filename = os.path.join(project_dir, 'main.py')
65             dump_str_to_file(main_contents, main_filename)
66
67         def asserts(cheesecake):
68             assert cheesecake.functions == ['main.function_without_docstring']
69             assert cheesecake.classes == ['main.SomeClass']
70
71         self._run_it(setup, asserts)
72
73     def _run_it(self, setup=None, asserts=None):
74         package_name = 'index_test'
75
76         project_dir = os.path.join(self.sandbox_dir, package_name)
77         os.mkdir(project_dir)
78
79         if setup:
80             setup(project_dir)
81
82         logger.setconsumer('console', logger.STDOUT)
83         console_log = logger.MultipleProducer('cheesecake console')
84
85         # Aliasing package_name because of Python optimizations.
86         #     ("package_name = package_name" will simply not work)
87         pkg_name = package_name
88
89         class CheesecakeMockup(Cheesecake):
90             def __init__(self):
91                 pass
92             sandbox = self.sandbox_dir
93             package_name = pkg_name
94             log = console_log
95
96         cheesecake = CheesecakeMockup()
97         cheesecake.walk_pkg()
98
99         if asserts:
100             asserts(cheesecake)
101
102         index = IndexUnitTested()
103         index.compute_with(cheesecake)
104
105         # Unit tests presence should be discovered and package should get maximum score.
106         print "Index: %d/%d -- %s" % (index.value, index.max_value, index.details)
107         assert index.value == index.max_value
108
109     def test_special_filenames_1(self):
110         "Test unit_tested index with package that uses test_* filenames."
111         def setup(project_dir):
112             files = ['some_module.py', 'README', 'test_some_module.py']
113             create_empty_files_in_directory(files, project_dir)
114
115         self._run_it(setup)
116
117     def test_special_filenames_2(self):
118         "Test unit_tested index with package that uses *_test filenames."
119         def setup(project_dir):
120             files = ['some_module.py', 'README', 'some_module_test.py']
121             create_empty_files_in_directory(files, project_dir)
122
123         self._run_it(setup)
124
125     def test_special_methods(self):
126         "Test unit_tested index with package that uses setUp/tearDown methods."
127         def setup(project_dir):
128             test_filename = os.path.join(project_dir, 'do_checks.py')
129             dump_str_to_file(test_contents, test_filename)
130
131         self._run_it(setup)
132
133     def test_unittest_classes(self):
134         "Test unit_tested index with package that uses unittest library."
135         def setup(project_dir):
136             test_filename = os.path.join(project_dir, 'do_checks.py')
137             dump_str_to_file(unittest_test_contents, test_filename)
138
139         self._run_it(setup)
Note: See TracBrowser for help on using the browser.