| 1 |
import os |
|---|
| 2 |
import shutil |
|---|
| 3 |
import tempfile |
|---|
| 4 |
|
|---|
| 5 |
from math import ceil |
|---|
| 6 |
|
|---|
| 7 |
import _path_cheesecake |
|---|
| 8 |
from _helper_cheesecake import dump_str_to_file |
|---|
| 9 |
|
|---|
| 10 |
from cheesecake.cheesecake_index import IndexUnitTests |
|---|
| 11 |
from cheesecake import logger |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
test_contents = """ |
|---|
| 15 |
import some_module |
|---|
| 16 |
import different_module |
|---|
| 17 |
|
|---|
| 18 |
class TestSomeFunction: |
|---|
| 19 |
def test_some_function(self): |
|---|
| 20 |
value = some_module.some_function(4, 2) |
|---|
| 21 |
assert value is True |
|---|
| 22 |
|
|---|
| 23 |
def test_a_method(self): |
|---|
| 24 |
self.object = some_module.SomeClass() |
|---|
| 25 |
|
|---|
| 26 |
def test_different_module(self): |
|---|
| 27 |
self.object = different_module.NotTestedClass() |
|---|
| 28 |
different_module.some_module.some_function() |
|---|
| 29 |
""" |
|---|
| 30 |
|
|---|
| 31 |
class TestUnitTestsIndex(object): |
|---|
| 32 |
def setUp(self): |
|---|
| 33 |
self.project_dir = tempfile.mkdtemp() |
|---|
| 34 |
|
|---|
| 35 |
def tearDown(self): |
|---|
| 36 |
if os.path.exists(self.project_dir): |
|---|
| 37 |
shutil.rmtree(self.project_dir) |
|---|
| 38 |
|
|---|
| 39 |
def test_unit_tests_index(self): |
|---|
| 40 |
test_dir = os.path.join(self.project_dir, 'test') |
|---|
| 41 |
os.mkdir(test_dir) |
|---|
| 42 |
|
|---|
| 43 |
test_filename = os.path.join(test_dir, 'test_some_function.py') |
|---|
| 44 |
dump_str_to_file(test_contents, test_filename) |
|---|
| 45 |
|
|---|
| 46 |
logger.setconsumer('console', logger.STDOUT) |
|---|
| 47 |
console_log = logger.MultipleProducer('cheesecake console') |
|---|
| 48 |
|
|---|
| 49 |
class CheesecakeMockup(object): |
|---|
| 50 |
files_list = [test_filename] |
|---|
| 51 |
functions = ["some_module.some_function", |
|---|
| 52 |
"some_module.other_function"] |
|---|
| 53 |
classes = ["some_module.SomeClass", |
|---|
| 54 |
"some_module.NotTestedClass"] |
|---|
| 55 |
package_dir = self.project_dir |
|---|
| 56 |
log = console_log |
|---|
| 57 |
|
|---|
| 58 |
index = IndexUnitTests() |
|---|
| 59 |
|
|---|
| 60 |
index.compute_with(CheesecakeMockup()) |
|---|
| 61 |
|
|---|
| 62 |
print "Index: %d/%d -- %s" % (index.value, index.max_value, index.details) |
|---|
| 63 |
assert index.value == int(ceil(index.max_value * 2.0/4.0)) |
|---|