PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/unit_tests/test_cases.py

https://bitbucket.org/jpellerin/nose/
Python | 274 lines | 212 code | 54 blank | 8 comment | 13 complexity | 963b7e6d68195feb636ff324698c68f2 MD5 | raw file
  1import unittest
  2import pdb
  3import sys
  4import nose.case
  5import nose.failure
  6from nose.pyversion import unbound_method
  7from nose.config import Config
  8from mock import ResultProxyFactory, ResultProxy
  9
 10class TestNoseCases(unittest.TestCase):
 11
 12    def test_function_test_case(self):
 13        res = unittest.TestResult()
 14        
 15        a = []
 16        def func(a=a):
 17            a.append(1)
 18
 19        case = nose.case.FunctionTestCase(func)
 20        case(res)
 21        assert a[0] == 1
 22
 23    def test_method_test_case(self):
 24        res = unittest.TestResult()
 25
 26        a = []
 27        class TestClass(object):
 28            def test_func(self, a=a):
 29                a.append(1)
 30
 31        case = nose.case.MethodTestCase(unbound_method(TestClass,
 32                                                       TestClass.test_func))
 33        case(res)
 34        assert a[0] == 1
 35
 36    def test_method_test_case_with_metaclass(self):
 37        res = unittest.TestResult()
 38        
 39        class TestType(type):
 40            def __new__(cls, name, bases, dct):
 41                return type.__new__(cls, name, bases, dct)
 42        a = []
 43        class TestClass(object):
 44            __metaclass__ = TestType
 45            def test_func(self, a=a):
 46                a.append(1)
 47
 48        case = nose.case.MethodTestCase(unbound_method(TestClass,
 49                                                       TestClass.test_func))
 50        case(res)
 51        assert a[0] == 1
 52
 53    def test_method_test_case_fixtures(self):        
 54        res = unittest.TestResult()
 55        called = []
 56        class TestClass(object):
 57            def setup(self):
 58                called.append('setup')
 59            def teardown(self):
 60                called.append('teardown')
 61            def test_func(self):
 62                called.append('test')
 63
 64        case = nose.case.MethodTestCase(unbound_method(TestClass,
 65                                                       TestClass.test_func))
 66        case(res)
 67        self.assertEqual(called, ['setup', 'test', 'teardown'])
 68
 69        class TestClassFailingSetup(TestClass):
 70            def setup(self):
 71                called.append('setup')
 72                raise Exception("failed")
 73        called[:] = []
 74        case = nose.case.MethodTestCase(unbound_method(TestClassFailingSetup,
 75                                            TestClassFailingSetup.test_func))
 76        case(res)
 77        self.assertEqual(called, ['setup'])        
 78
 79        class TestClassFailingTest(TestClass):
 80            def test_func(self):
 81                called.append('test')
 82                raise Exception("failed")
 83            
 84        called[:] = []
 85        case = nose.case.MethodTestCase(unbound_method(TestClassFailingTest,
 86                                            TestClassFailingTest.test_func))
 87        case(res)
 88        self.assertEqual(called, ['setup', 'test', 'teardown'])     
 89        
 90    def test_function_test_case_fixtures(self):
 91        from nose.tools import with_setup
 92        res = unittest.TestResult()
 93
 94        called = {}
 95
 96        def st():
 97            called['st'] = True
 98        def td():
 99            called['td'] = True
100
101        def func_exc():
102            called['func'] = True
103            raise TypeError("An exception")
104
105        func_exc = with_setup(st, td)(func_exc)
106        case = nose.case.FunctionTestCase(func_exc)
107        case(res)
108        assert 'st' in called
109        assert 'func' in called
110        assert 'td' in called
111
112    def test_failure_case(self):
113        res = unittest.TestResult()
114        f = nose.failure.Failure(ValueError, "No such test spam")
115        f(res)
116        assert res.errors
117
118
119class TestNoseTestWrapper(unittest.TestCase):
120    def test_case_fixtures_called(self):
121        """Instance fixtures are properly called for wrapped tests"""
122        res = unittest.TestResult()
123        called = []
124                        
125        class TC(unittest.TestCase):
126            def setUp(self):
127                print "TC setUp %s" % self
128                called.append('setUp')
129            def runTest(self):
130                print "TC runTest %s" % self
131                called.append('runTest')
132            def tearDown(self):
133                print "TC tearDown %s" % self
134                called.append('tearDown')
135
136        case = nose.case.Test(TC())
137        case(res)
138        assert not res.errors, res.errors
139        assert not res.failures, res.failures
140        self.assertEqual(called, ['setUp', 'runTest', 'tearDown'])
141
142    def test_result_proxy_used(self):
143        """A result proxy is used to wrap the result for all tests"""
144        class TC(unittest.TestCase):
145            def runTest(self):
146                raise Exception("error")
147            
148        ResultProxy.called[:] = []
149        res = unittest.TestResult()
150        config = Config()
151        case = nose.case.Test(TC(), config=config,
152                              resultProxy=ResultProxyFactory())
153
154        case(res)
155        assert not res.errors, res.errors
156        assert not res.failures, res.failures
157
158        calls = [ c[0] for c in ResultProxy.called ]
159        self.assertEqual(calls, ['beforeTest', 'startTest', 'addError',
160                                 'stopTest', 'afterTest'])
161
162    def test_address(self):
163        from nose.util import absfile, src
164        class TC(unittest.TestCase):
165            def runTest(self):
166                raise Exception("error")
167
168        def dummy(i):
169            pass
170
171        def test():
172            pass
173
174        class Test:
175            def test(self):
176                pass
177
178            def test_gen(self):
179                def tryit(i):
180                    pass
181                for i in range (0, 2):
182                    yield tryit, i
183
184            def try_something(self, a, b):
185                pass
186
187        fl = src(absfile(__file__))
188        case = nose.case.Test(TC())
189        self.assertEqual(case.address(), (fl, __name__, 'TC.runTest'))
190
191        case = nose.case.Test(nose.case.FunctionTestCase(test))
192        self.assertEqual(case.address(), (fl, __name__, 'test'))
193
194        case = nose.case.Test(nose.case.FunctionTestCase(
195            dummy, arg=(1,), descriptor=test))
196        self.assertEqual(case.address(), (fl, __name__, 'test'))
197
198        case = nose.case.Test(nose.case.MethodTestCase(
199                                  unbound_method(Test, Test.test)))
200        self.assertEqual(case.address(), (fl, __name__, 'Test.test'))
201
202        case = nose.case.Test(
203            nose.case.MethodTestCase(unbound_method(Test, Test.try_something),
204                                     arg=(1,2,),
205                                     descriptor=unbound_method(Test,
206                                                               Test.test_gen)))
207        self.assertEqual(case.address(),
208                         (fl, __name__, 'Test.test_gen'))
209
210        case = nose.case.Test(
211            nose.case.MethodTestCase(unbound_method(Test, Test.test_gen),
212                                     test=dummy, arg=(1,)))
213        self.assertEqual(case.address(),
214                         (fl, __name__, 'Test.test_gen'))
215
216    def test_context(self):
217        class TC(unittest.TestCase):
218            def runTest(self):
219                pass
220        def test():
221            pass
222
223        class Test:
224            def test(self):
225                pass
226
227        case = nose.case.Test(TC())
228        self.assertEqual(case.context, TC)
229
230        case = nose.case.Test(nose.case.FunctionTestCase(test))
231        self.assertEqual(case.context, sys.modules[__name__])
232
233        case = nose.case.Test(nose.case.MethodTestCase(unbound_method(Test,
234                                                           Test.test)))
235        self.assertEqual(case.context, Test)
236
237    def test_short_description(self):
238        class TC(unittest.TestCase):
239            def test_a(self):
240                """
241                This is the description
242                """
243                pass
244
245            def test_b(self):
246                """This is the description
247                """
248                pass
249
250            def test_c(self):
251                pass
252
253        case_a = nose.case.Test(TC('test_a'))
254        case_b = nose.case.Test(TC('test_b'))
255        case_c = nose.case.Test(TC('test_c'))
256
257        assert case_a.shortDescription().endswith("This is the description")
258        assert case_b.shortDescription().endswith("This is the description")
259        assert case_c.shortDescription() in (None, # pre 2.7
260                                             'test_c (test_cases.TC)') # 2.7
261
262    def test_unrepresentable_shortDescription(self):
263        class TC(unittest.TestCase):
264            def __str__(self):
265                # see issue 422
266                raise ValueError('simulate some mistake in this code')
267            def runTest(self):
268                pass
269
270        case = nose.case.Test(TC())
271        self.assertEqual(case.shortDescription(), None)
272
273if __name__ == '__main__':
274    unittest.main()