• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2
3from unittest.test.support import LoggingResult
4
5
6class Test_FunctionTestCase(unittest.TestCase):
7
8    # "Return the number of tests represented by the this test object. For
9    # TestCase instances, this will always be 1"
10    def test_countTestCases(self):
11        test = unittest.FunctionTestCase(lambda: None)
12
13        self.assertEqual(test.countTestCases(), 1)
14
15    # "When a setUp() method is defined, the test runner will run that method
16    # prior to each test. Likewise, if a tearDown() method is defined, the
17    # test runner will invoke that method after each test. In the example,
18    # setUp() was used to create a fresh sequence for each test."
19    #
20    # Make sure the proper call order is maintained, even if setUp() raises
21    # an exception.
22    def test_run_call_order__error_in_setUp(self):
23        events = []
24        result = LoggingResult(events)
25
26        def setUp():
27            events.append('setUp')
28            raise RuntimeError('raised by setUp')
29
30        def test():
31            events.append('test')
32
33        def tearDown():
34            events.append('tearDown')
35
36        expected = ['startTest', 'setUp', 'addError', 'stopTest']
37        unittest.FunctionTestCase(test, setUp, tearDown).run(result)
38        self.assertEqual(events, expected)
39
40    # "When a setUp() method is defined, the test runner will run that method
41    # prior to each test. Likewise, if a tearDown() method is defined, the
42    # test runner will invoke that method after each test. In the example,
43    # setUp() was used to create a fresh sequence for each test."
44    #
45    # Make sure the proper call order is maintained, even if the test raises
46    # an error (as opposed to a failure).
47    def test_run_call_order__error_in_test(self):
48        events = []
49        result = LoggingResult(events)
50
51        def setUp():
52            events.append('setUp')
53
54        def test():
55            events.append('test')
56            raise RuntimeError('raised by test')
57
58        def tearDown():
59            events.append('tearDown')
60
61        expected = ['startTest', 'setUp', 'test', 'tearDown',
62                    'addError', 'stopTest']
63        unittest.FunctionTestCase(test, setUp, tearDown).run(result)
64        self.assertEqual(events, expected)
65
66    # "When a setUp() method is defined, the test runner will run that method
67    # prior to each test. Likewise, if a tearDown() method is defined, the
68    # test runner will invoke that method after each test. In the example,
69    # setUp() was used to create a fresh sequence for each test."
70    #
71    # Make sure the proper call order is maintained, even if the test signals
72    # a failure (as opposed to an error).
73    def test_run_call_order__failure_in_test(self):
74        events = []
75        result = LoggingResult(events)
76
77        def setUp():
78            events.append('setUp')
79
80        def test():
81            events.append('test')
82            self.fail('raised by test')
83
84        def tearDown():
85            events.append('tearDown')
86
87        expected = ['startTest', 'setUp', 'test', 'tearDown',
88                    'addFailure', 'stopTest']
89        unittest.FunctionTestCase(test, setUp, tearDown).run(result)
90        self.assertEqual(events, expected)
91
92    # "When a setUp() method is defined, the test runner will run that method
93    # prior to each test. Likewise, if a tearDown() method is defined, the
94    # test runner will invoke that method after each test. In the example,
95    # setUp() was used to create a fresh sequence for each test."
96    #
97    # Make sure the proper call order is maintained, even if tearDown() raises
98    # an exception.
99    def test_run_call_order__error_in_tearDown(self):
100        events = []
101        result = LoggingResult(events)
102
103        def setUp():
104            events.append('setUp')
105
106        def test():
107            events.append('test')
108
109        def tearDown():
110            events.append('tearDown')
111            raise RuntimeError('raised by tearDown')
112
113        expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
114                    'stopTest']
115        unittest.FunctionTestCase(test, setUp, tearDown).run(result)
116        self.assertEqual(events, expected)
117
118    # "Return a string identifying the specific test case."
119    #
120    # Because of the vague nature of the docs, I'm not going to lock this
121    # test down too much. Really all that can be asserted is that the id()
122    # will be a string (either 8-byte or unicode -- again, because the docs
123    # just say "string")
124    def test_id(self):
125        test = unittest.FunctionTestCase(lambda: None)
126
127        self.assertIsInstance(test.id(), str)
128
129    # "Returns a one-line description of the test, or None if no description
130    # has been provided. The default implementation of this method returns
131    # the first line of the test method's docstring, if available, or None."
132    def test_shortDescription__no_docstring(self):
133        test = unittest.FunctionTestCase(lambda: None)
134
135        self.assertEqual(test.shortDescription(), None)
136
137    # "Returns a one-line description of the test, or None if no description
138    # has been provided. The default implementation of this method returns
139    # the first line of the test method's docstring, if available, or None."
140    def test_shortDescription__singleline_docstring(self):
141        desc = "this tests foo"
142        test = unittest.FunctionTestCase(lambda: None, description=desc)
143
144        self.assertEqual(test.shortDescription(), "this tests foo")
145
146
147if __name__ == "__main__":
148    unittest.main()
149