garybernhardt / dingus

A record-then-assert mocking library.

Clone this repository (size: 73.7 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/garybernhardt/dingus/
commit 44: 7fc80f2aa446
parent 43: 5496f83fc348
branch: default
Added ability to exclude objects from dingusing (shown in Dingus screencast)
Gary Bernhardt
12 months ago

Changed (Δ780 bytes):

raw changeset »

dingus.py (9 lines added, 5 lines removed)

tests/test_case_fixture.py (3 lines added, 3 lines removed)

tests/test_dingus_test_case.py (18 lines added, 1 lines removed)

Up to file-list dingus.py:

@@ -7,10 +7,16 @@ import sys
7
7
import new
8
8
9
9
10
def DingusTestCase(*objects_under_test):
10
def DingusTestCase(object_under_test, *exclusions):
11
    def get_names_under_test():
12
        module = sys.modules[object_under_test.__module__]
13
        for name, value in module.__dict__.iteritems():
14
            if value is object_under_test or name in exclusions:
15
                yield name
16
11
17
    class TestCase(object):
12
18
        def setup(self):
13
            module_name = objects_under_test[0].__module__
19
            module_name = object_under_test.__module__
14
20
            self._dingus_module = sys.modules[module_name]
15
21
            self._dingus_replace_module_globals(self._dingus_module)
16
22
@@ -33,9 +39,7 @@ def DingusTestCase(*objects_under_test):
33
39
            module.__dict__.clear()
34
40
            module.__dict__.update(old_module_dict)
35
41
36
    names_under_test = [excluded_object.__name__
37
                        for excluded_object in objects_under_test]
38
42
    names_under_test = list(get_names_under_test())
39
43
    TestCase.__name__ = '%s_DingusTestCase' % '_'.join(names_under_test)
40
44
    return TestCase
41
45

Up to file-list tests/test_case_fixture.py:

1
1
atomic_value = 'foo'
2
2
3
callable_value_with_wrong_name = lambda: None
4
callable_value_with_wrong_name.__name__ = 'wrong'
5
6
3
class ClassUnderTest:
7
4
    pass
5
class Collaborator:
6
    pass
7

Up to file-list tests/test_dingus_test_case.py:

1
1
import sys
2
2
import new
3
3
from tests import test_case_fixture as module
4
from tests.test_case_fixture import ClassUnderTest
4
from tests.test_case_fixture import ClassUnderTest, Collaborator
5
5
6
6
from dingus import DingusTestCase, Dingus
7
7
8
8
9
class WhenObjectIsExcludedFromTest:
10
    def setup(self):
11
        class TestCase(DingusTestCase(module.ClassUnderTest, 'Collaborator')):
12
            pass
13
        self.test_case_instance = TestCase()
14
        self.test_case_instance.setup()
15
16
    def should_not_replace_it_with_dingus(self):
17
        assert module.Collaborator is Collaborator
18
19
    def teardown(self):
20
        self.test_case_instance.teardown()
21
22
9
23
class WhenCallingSetupFunction:
10
24
    def setup(self):
11
25
        class TestCase(DingusTestCase(module.ClassUnderTest)):
@@ -19,6 +33,9 @@ class WhenCallingSetupFunction:
19
33
    def should_replace_module_attributes(self):
20
34
        assert isinstance(module.atomic_value, Dingus)
21
35
36
    def should_replace_collaborating_classes(self):
37
        assert isinstance(module.Collaborator, Dingus)
38
22
39
    def should_leave_class_under_test_intact(self):
23
40
        assert module.ClassUnderTest is ClassUnderTest
24
41