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

Revision 89, 1.9 kB (checked in by mk, 6 years ago)

Search for doctests inside docstrings (closes ticket #45).

  • 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 IndexUnitTests
10 from cheesecake import logger
11
12
13 def dump_str_to_file(string, filename):
14     fd = file(filename, 'w')
15     fd.write(string)
16     fd.close()
17
18
19 test_contents = """
20 import some_module
21 import different_module
22
23 class TestSomeFunction:
24     def test_some_function(self):
25         value = some_module.some_function(4, 2)
26         assert value is True
27
28     def test_a_method(self):
29         self.object = some_module.SomeClass()
30
31     def test_different_module(self):
32         self.object = different_module.NotTestedClass()
33         different_module.some_module.some_function()
34 """
35
36 class TestUnitTestsIndex(object):
37     def setUp(self):
38         self.project_dir = tempfile.mkdtemp()
39
40     def tearDown(self):
41         if os.path.exists(self.project_dir):
42             shutil.rmtree(self.project_dir)
43
44     def test_unit_tests_index(self):
45         test_dir = os.path.join(self.project_dir, 'test')
46         os.mkdir(test_dir)
47
48         test_filename = os.path.join(test_dir, 'test_some_function.py')
49         dump_str_to_file(test_contents, test_filename)
50
51         logger.setconsumer('console', logger.STDOUT)
52         console_log = logger.MultipleProducer('cheesecake console')
53
54         class CheesecakeMockup(object):
55             files_list = [test_filename]
56             functions = ["some_module.some_function",
57                          "some_module.other_function"]
58             classes = ["some_module.SomeClass",
59                        "some_module.NotTestedClass"]
60             package_dir = self.project_dir
61             log = console_log
62
63         index = IndexUnitTests()
64
65         index.compute_with(CheesecakeMockup())
66
67         print "Index: %d/%d -- %s" % (index.value, index.max_value, index.details)
68         assert index.value == int(ceil(index.max_value * 2.0/4.0))
Note: See TracBrowser for help on using the browser.