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