1"""
2pkg1
3pkg1.test
4pkg1.test.test_things
5pkg1.test.test_things.test_func
6pkg1.test.test_things.test_gen
7pkg1.test.test_things.test_gen:3
8pkg1.test.test_things.SomeTests
9pkg1.test.test_things.SomeTests.test_ok
10
11# generator method
12# generator method index
13
14# param func
15# param func index
16# param method
17# param method index
18
19"""
20import sys
21from nose2.tests._common import FunctionalTestCase, support_file
22
23
24class TestLoadTestsFromPackage(FunctionalTestCase):
25
26    def test_start_directory_inside_package(self):
27        proc = self.runIn(
28            'scenario/tests_in_package/pkg1/test',
29            '-v',
30            '-t',
31            support_file('scenario/tests_in_package'))
32        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
33        self.assertEqual(proc.poll(), 1)
34
35    def test_project_directory_inside_package(self):
36        proc = self.runIn(
37            'scenario/tests_in_package/pkg1/test',
38            '-v')
39        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
40        self.assertEqual(proc.poll(), 1)
41
42    def test_module_name(self):
43        proc = self.runIn(
44            'scenario/tests_in_package',
45            '-v',
46            'pkg1.test.test_things')
47        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
48        self.assertEqual(proc.poll(), 1)
49
50    def test_package_name(self):
51        proc = self.runIn(
52            'scenario/tests_in_package',
53            '-v',
54            'pkg1')
55        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
56        self.assertEqual(proc.poll(), 1)
57
58    def test_module_name_with_start_dir(self):
59        proc = self.runIn(
60            '.', '-v', '-s', support_file('scenario/tests_in_package'),
61            'pkg1.test.test_things')
62
63        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
64        self.assertEqual(proc.poll(), 1)
65
66    def test_package_name_with_start_dir(self):
67        proc = self.runIn(
68            '.', '-v', '-s', support_file('scenario/tests_in_package'), 'pkg1')
69        self.assertTestRunOutputMatches(proc, stderr='Ran 25 tests')
70        self.assertEqual(proc.poll(), 1)
71
72    def test_function_name(self):
73        proc = self.runIn(
74            'scenario/tests_in_package',
75            '-v',
76            'pkg1.test.test_things.test_func')
77        self.assertTestRunOutputMatches(
78            proc, stderr='test_func')
79        self.assertTestRunOutputMatches(
80            proc, stderr='Ran 1 test')
81        self.assertTestRunOutputMatches(
82            proc, stderr='OK')
83        self.assertEqual(proc.poll(), 0)
84
85    def test_function_string_repr(self):
86        proc = self.runIn(
87            'scenario/tests_in_package',
88            '-v',
89            'pkg1.test.test_things.test_func')
90        self.assertTestRunOutputMatches(
91            proc, stderr='pkg1.test.test_things.test_func \.\.\. ok')
92        self.assertEqual(proc.poll(), 0)
93
94    def test_generator_function_name(self):
95        proc = self.runIn(
96            'scenario/tests_in_package',
97            '-v',
98            'pkg1.test.test_things.test_gen')
99        self.assertTestRunOutputMatches(proc, stderr='test_gen')
100        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')
101        self.assertEqual(proc.poll(), 0)
102
103    def test_generator_function_index(self):
104        proc = self.runIn(
105            'scenario/tests_in_package',
106            '-v',
107            'pkg1.test.test_things.test_gen:3')
108        self.assertTestRunOutputMatches(proc, stderr='test_gen')
109        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
110        self.assertEqual(proc.poll(), 0)
111
112    def test_generator_function_index_1_based(self):
113        proc = self.runIn(
114            'scenario/tests_in_package',
115            '-v',
116            'pkg1.test.test_things.test_gen:1')
117
118        self.assertTestRunOutputMatches(proc, stderr='test_gen')
119        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
120        self.assertTestRunOutputMatches(proc, stderr='OK')
121        self.assertEqual(proc.poll(), 0)
122
123    def test_testcase_name(self):
124        proc = self.runIn(
125            'scenario/tests_in_package',
126            '-v',
127            'pkg1.test.test_things.SomeTests')
128        self.assertTestRunOutputMatches(proc, stderr='SomeTests')
129        self.assertTestRunOutputMatches(proc, stderr='Ran 8 tests')
130        self.assertEqual(proc.poll(), 1)
131
132    def test_testcase_method(self):
133        proc = self.runIn(
134            'scenario/tests_in_package',
135            '-v',
136            'pkg1.test.test_things.SomeTests.test_ok')
137        self.assertTestRunOutputMatches(proc, stderr='SomeTests')
138        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
139        self.assertTestRunOutputMatches(proc, stderr='OK')
140        self.assertEqual(proc.poll(), 0)
141
142    def test_generator_method(self):
143        proc = self.runIn(
144            'scenario/tests_in_package',
145            '-v',
146            'pkg1.test.test_things.SomeTests.test_gen_method')
147        self.assertTestRunOutputMatches(proc, stderr='test_gen_method')
148        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')
149        self.assertEqual(proc.poll(), 1)
150
151    def test_generator_method_index(self):
152        proc = self.runIn(
153            'scenario/tests_in_package',
154            '-v',
155            'pkg1.test.test_things.SomeTests.test_gen_method:1')
156        self.assertTestRunOutputMatches(proc, stderr='test_gen_method')
157        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
158        self.assertTestRunOutputMatches(proc, stderr='OK')
159        self.assertEqual(proc.poll(), 0)
160
161    def test_parameterized_method(self):
162        proc = self.runIn(
163            'scenario/tests_in_package',
164            '-v',
165            'pkg1.test.test_things.SomeTests.test_params_method')
166        self.assertTestRunOutputMatches(proc, stderr='test_params_method')
167        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')
168        self.assertEqual(proc.poll(), 1)
169
170    def test_parameterized_method_index(self):
171        proc = self.runIn(
172            'scenario/tests_in_package',
173            '-v',
174            'pkg1.test.test_things.SomeTests.test_params_method:1')
175        self.assertTestRunOutputMatches(proc, stderr='test_params_method')
176        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
177        self.assertTestRunOutputMatches(proc, stderr='OK')
178        self.assertEqual(proc.poll(), 0)
179
180    def test_parameterized_func(self):
181        proc = self.runIn(
182            'scenario/tests_in_package',
183            '-v',
184            'pkg1.test.test_things.test_params_func')
185        self.assertTestRunOutputMatches(proc, stderr='test_params_func')
186        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')
187        self.assertEqual(proc.poll(), 1)
188
189    def test_parameterized_func_index(self):
190        proc = self.runIn(
191            'scenario/tests_in_package',
192            '-v',
193            'pkg1.test.test_things.test_params_func:1')
194        self.assertTestRunOutputMatches(proc, stderr='test_params_func')
195        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
196        self.assertTestRunOutputMatches(proc, stderr='OK')
197        self.assertEqual(proc.poll(), 0)
198
199
200class TestLoadTestsOutsideOfPackage(FunctionalTestCase):
201
202    def test_module_name(self):
203        proc = self.runIn(
204            'scenario/package_in_lib',
205            '-v',
206            'tests')
207        self.assertTestRunOutputMatches(proc, stderr='Ran 3 tests')
208        self.assertEqual(proc.poll(), 1)
209
210    def test_function_name(self):
211        proc = self.runIn(
212            'scenario/package_in_lib',
213            '-v',
214            'tests.test')
215        self.assertTestRunOutputMatches(proc, stderr='test')
216        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
217        self.assertTestRunOutputMatches(proc, stderr='OK')
218        self.assertEqual(proc.poll(), 0)
219
220    def test_module_name_with_start_dir(self):
221        proc = self.runIn(
222            '.', '-v', '-s', support_file('scenario/package_in_lib'), 'tests')
223        self.assertTestRunOutputMatches(proc, stderr='Ran 3 tests')
224        self.assertEqual(proc.poll(), 1)
225
226
227class TestLoadingErrors(FunctionalTestCase):
228
229    def test_import_error_module(self):
230        proc = self.runIn(
231            'scenario/module_import_err',
232            '-v',
233            'test_import_err')
234        self.assertTestRunOutputMatches(proc, stderr='ImportError: booms')
235        self.assertTestRunOutputMatches(proc, stderr='Traceback \(most recent call last\):')
236        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
237        self.assertEqual(proc.poll(), 1)
238
239    def test_import_error_func(self):
240        proc = self.runIn(
241            'scenario/module_import_err',
242            '-v',
243            'test_import_err.test')
244        self.assertTestRunOutputMatches(proc, stderr='ImportError: booms')
245        self.assertTestRunOutputMatches(proc, stderr='Traceback \(most recent call last\):')
246        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
247        self.assertEqual(proc.poll(), 1)
248
249    def test_import_error_testcase(self):
250        proc = self.runIn(
251            'scenario/module_import_err',
252            '-v',
253            'test_import_err.Test')
254        self.assertTestRunOutputMatches(proc, stderr='ImportError: booms')
255        self.assertTestRunOutputMatches(proc, stderr='Traceback \(most recent call last\):')
256        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
257        self.assertEqual(proc.poll(), 1)
258
259    def test_import_error_testcase_method(self):
260        proc = self.runIn(
261            'scenario/module_import_err',
262            '-v',
263            'test_import_err.Test.test')
264        self.assertTestRunOutputMatches(proc, stderr='ImportError: booms')
265        self.assertTestRunOutputMatches(proc, stderr='Traceback \(most recent call last\):')
266        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
267        self.assertEqual(proc.poll(), 1)
268
269    def test_import_error_package(self):
270        proc = self.runIn(
271            'scenario/module_import_err',
272            '-v',
273            'pkg')
274        # In this case, there should not be an AttributeError since we only
275        # import pkg, which should work.
276        self.assertTestRunOutputMatches(proc, stderr='ImportError: Failed to import test module: pkg.test_import_err')
277        self.assertTestRunOutputMatches(proc, stderr='Traceback \(most recent call last\):')
278        self.assertTestRunOutputMatches(proc, stderr='ValueError: booms')
279        # 3 tests should run, and only one should fail. We're testing that a
280        # loading error does not prevent from running other tests.
281        self.assertTestRunOutputMatches(proc, stderr='Ran 3 tests')
282        self.assertTestRunOutputMatches(proc, stderr='FAILED \(errors=1\)')
283        self.assertEqual(proc.poll(), 1)
284
285    def test_import_error_module_in_package(self):
286        proc = self.runIn(
287            'scenario/module_import_err',
288            '-v',
289            'pkg.test_import_err')
290        # In this case, there should not be an ImportError. The import of
291        # pkg.test_import_err fails due with a `ValueError`, and this is the
292        # one we are expecting.
293        self.assertTestRunOutputMatches(proc, stderr='AttributeError: ')
294        self.assertTestRunOutputMatches(proc, stderr='ValueError: booms')
295        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
296        self.assertEqual(proc.poll(), 1)
297
298    def test_import_error_unknown_module_in_package(self):
299        proc = self.runIn(
300            'scenario/module_import_err',
301            '-v',
302            'pkg.test_does_not_exit')
303        # In this case, there should not be an ImportError. The import of
304        # pkg.test_import_err fails due with a `ValueError`, and this is the
305        # one we are expecting.
306        self.assertTestRunOutputMatches(proc, stderr='AttributeError: ')
307
308        # py3.6 introduced ModuleNotFoundError, a subclass of ImportError
309        if sys.version_info < (3, 6):
310            importerr = 'ImportError: No module named'
311        else:
312            importerr = 'ModuleNotFoundError: No module named'
313        self.assertTestRunOutputMatches(proc, stderr=importerr)
314
315        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
316        self.assertEqual(proc.poll(), 1)
317
318
319
320class TestTestClassLoading(FunctionalTestCase):
321
322    def test_load_testclass_by_name(self):
323        proc = self.runIn(
324            'scenario/test_classes',
325            '-v',
326            'test_classes.Test')
327        self.assertTestRunOutputMatches(proc, stderr='Ran 8 tests')
328        self.assertEqual(proc.poll(), 0)
329
330    def test_load_testclass_method_by_name(self):
331        proc = self.runIn(
332            'scenario/test_classes',
333            '-v',
334            'test_classes.Test.test')
335        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
336        self.assertEqual(proc.poll(), 0)
337
338    def test_load_testclass_generator_method_by_name(self):
339        proc = self.runIn(
340            'scenario/test_classes',
341            '-v',
342            'test_classes.Test.test_gen')
343        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')
344        self.assertEqual(proc.poll(), 0)
345
346    def test_load_testclass_params_method_by_name(self):
347        proc = self.runIn(
348            'scenario/test_classes',
349            '-v',
350            'test_classes.Test.test_params')
351        self.assertTestRunOutputMatches(proc, stderr='Ran 2 tests')
352        self.assertEqual(proc.poll(), 0)
353
354    def test_class_level_fixtures_supported(self):
355        proc = self.runIn(
356            'scenario/test_classes',
357            '-v',
358            'test_fixtures')
359        self.assertTestRunOutputMatches(proc, stderr='Ran 5 tests')
360        self.assertEqual(proc.poll(), 0)
361
362    def test_error_in_test_class(self):
363        proc = self.runIn(
364            'scenario/test_class_fail',
365            '-v',
366            'test_class_fail')
367        self.assertTestRunOutputMatches(proc, stderr='nose2.loader.LoadTestsFailure')
368        self.assertTestRunOutputMatches(proc, stderr='Ran 1 test')
369        self.assertTestRunOutputMatches(proc, stderr='FAILED')
370        self.assertEqual(proc.poll(), 1)
371
372    def test_expected_failures(self):
373        proc = self.runIn(
374            'scenario/expected_failures',
375            '-v',
376            'expected_failures')
377        self.assertTestRunOutputMatches(proc, stderr=r'FAILED \(failures=1, expected failures=1, unexpected successes=1\)')
378
379