10236.5.1
by Jeroen Vermeulen
Adding FakeMethod helper. |
1 |
# Copyright 2010 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3 |
||
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
4 |
from unittest import ( |
5 |
TestCase, |
|
6 |
TestLoader, |
|
7 |
)
|
|
10236.5.1
by Jeroen Vermeulen
Adding FakeMethod helper. |
8 |
|
9 |
from lp.testing.fakemethod import FakeMethod |
|
10 |
||
11 |
||
12 |
class RealActualClass: |
|
13 |
"""A class that's hard to test."""
|
|
14 |
||
15 |
doing_impossible_stuff = False |
|
16 |
doing_possible_stuff = False |
|
17 |
||
18 |
def impossibleMethod(self): |
|
19 |
"""A method that you can't afford to invoke in your test.
|
|
20 |
||
21 |
This is the method you're going to want to avoid calling. The
|
|
22 |
way to do that is to replace this method with a fake method.
|
|
23 |
"""
|
|
24 |
self.doing_impossible_stuff = True |
|
10236.5.3
by Jeroen Vermeulen
Lint. |
25 |
raise AssertionError("Trying to do impossible stuff.") |
10236.5.1
by Jeroen Vermeulen
Adding FakeMethod helper. |
26 |
|
27 |
def testableMethod(self): |
|
28 |
"""This part of the class logic that you do want to exercise."""
|
|
29 |
self.doing_possible_stuff = True |
|
30 |
||
31 |
def doComplicatedThings(self, argument): |
|
32 |
"""This is the top-level method you want to test.
|
|
33 |
||
34 |
Unfortunately this invokes impossibleMethod, making it hard.
|
|
35 |
"""
|
|
36 |
self.impossibleMethod() |
|
37 |
self.testableMethod() |
|
38 |
return argument |
|
39 |
||
40 |
||
41 |
class CustomException(Exception): |
|
42 |
"""Some specific error that you want raised."""
|
|
43 |
||
44 |
||
45 |
class TestFakeMethod(TestCase): |
|
46 |
def test_fakeMethod(self): |
|
47 |
# A class that you're testing can continue normally despite some
|
|
48 |
# of its methods being stubbed.
|
|
49 |
thing = RealActualClass() |
|
50 |
thing.impossibleMethod = FakeMethod() |
|
51 |
||
52 |
result = thing.doComplicatedThings(99) |
|
53 |
||
54 |
self.assertEqual(99, result) |
|
55 |
self.assertFalse(thing.doing_impossible_stuff) |
|
56 |
self.assertTrue(thing.doing_possible_stuff) |
|
57 |
||
58 |
def test_raiseFailure(self): |
|
59 |
# A FakeMethod can raise an exception you specify.
|
|
60 |
ouch = CustomException("Ouch!") |
|
61 |
func = FakeMethod(failure=ouch) |
|
62 |
self.assertRaises(CustomException, func) |
|
63 |
||
64 |
def test_returnResult(self): |
|
65 |
# A FakeMethod can return a value you specify.
|
|
66 |
value = "Fixed return value." |
|
67 |
func = FakeMethod(result=value) |
|
68 |
self.assertEqual(value, func()) |
|
69 |
||
70 |
def test_countCalls(self): |
|
71 |
# A FakeMethod counts the number of times it's been invoked.
|
|
72 |
func = FakeMethod() |
|
73 |
for count in xrange(3): |
|
74 |
self.assertEqual(count, func.call_count) |
|
75 |
func() |
|
76 |
self.assertEqual(count + 1, func.call_count) |
|
77 |
||
78 |
def test_takeArguments(self): |
|
79 |
# A FakeMethod invocation accepts any arguments it gets.
|
|
80 |
func = FakeMethod() |
|
81 |
func() |
|
82 |
func(1) |
|
83 |
func(2, kwarg=3) |
|
84 |
self.assertEqual(3, func.call_count) |
|
85 |
||
86 |
||
87 |
def test_suite(): |
|
88 |
return TestLoader().loadTestsFromName(__name__) |