-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_loader.py
883 lines (752 loc) · 27.2 KB
/
test_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
from __future__ import unicode_literals
import os
from os.path import dirname
import platform
import shutil
import sys
import tempfile
from textwrap import dedent
import unittest
try:
from unittest.mock import MagicMock
except:
from mock import MagicMock
from green import loader
class TestToProtoTestList(unittest.TestCase):
def test_moduleImportFailure(self):
"""
toProtoTestList() raises import errors normally
"""
suite = MagicMock()
suite.__class__.__name__ = str('ModuleImportFailure')
suite.__str__.return_value = "exception_method other_stuff"
suite.exception_method.side_effect = AttributeError
self.assertRaises(AttributeError, loader.toProtoTestList, (suite,))
def test_moduleImportFailureIgnored(self):
"""
toProtoTestList() does not raise errors when doing completions
"""
suite = MagicMock()
suite.__class__.__name__ = str('ModuleImportFailure')
suite.__str__.return_value = "exception_method other_stuff"
suite.exception_method.side_effect = AttributeError
self.assertEqual(loader.toProtoTestList(suite, doing_completions=True), [])
class TestToParallelTargets(unittest.TestCase):
def setUp(self):
super(TestToParallelTargets, self).setUp()
class FakeModule(object):
pass
self._fake_module_name = "my_test_module"
sys.modules[self._fake_module_name] = FakeModule
def tearDown(self):
del sys.modules[self._fake_module_name]
super(TestToParallelTargets, self).tearDown()
def test_methods_with_no_constraints(self):
"""
toParallelTargets() returns only module names.
"""
class NormalTestCase(unittest.TestCase):
def runTest(self):
pass
NormalTestCase.__module__ = self._fake_module_name
targets = loader.toParallelTargets(NormalTestCase(), [])
self.assertEqual(targets, ["my_test_module"])
def test_methods_with_constraints(self):
"""
toParallelTargets() returns test names when constrained.
"""
class NormalTestCase(unittest.TestCase):
def runTest(self):
pass
NormalTestCase.__module__ = self._fake_module_name
full_name = "my_test_module.NormalTestCase.runTest"
targets = loader.toParallelTargets(NormalTestCase(), [full_name])
self.assertEqual(targets, [full_name])
class TestCompletions(unittest.TestCase):
def test_completionBad(self):
"""
Bad match generates no completions
"""
self.assertEqual('', loader.getCompletions('garbage.in'))
def test_completionExact(self):
"""
Correct completions are generated for an exact match.
"""
c = set(loader.getCompletions('green').split('\n'))
self.assertIn('green', c)
self.assertIn('green.test', c)
self.assertIn('green.test.test_loader', c)
self.assertIn('green.test.test_loader.TestCompletions', c)
self.assertIn(
'green.test.test_loader.TestCompletions.test_completionExact', c)
def test_completionPartialShort(self):
"""
Correct completions generated for short partial match.
"""
cwd = os.getcwd()
green_parent = dirname(dirname(dirname(os.path.abspath(__file__))))
os.chdir(green_parent)
self.addCleanup(os.chdir, cwd)
c = set(loader.getCompletions('gre').split('\n'))
self.assertIn('green', c)
self.assertIn('green.test', c)
self.assertIn('green.test.test_loader', c)
self.assertIn('green.test.test_loader.TestCompletions', c)
self.assertIn(
'green.test.test_loader.TestCompletions.test_completionPartialShort', c)
def test_completionPartial(self):
"""
Correct completions generated for partial match. 2nd target ignored.
"""
c = set(loader.getCompletions(['green.te', 'green']).split('\n'))
self.assertIn('green.test', c)
self.assertIn('green.test.test_loader', c)
self.assertIn('green.test.test_loader.TestCompletions', c)
self.assertIn(
'green.test.test_loader.TestCompletions.test_completionPartial', c)
self.assertNotIn('green', c)
def test_completionEmpty(self):
"""
An empty target generates completions for the whole directory
"""
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
os.chdir(tmpdir)
self.addCleanup(os.chdir, cwd)
os.mkdir('the_pkg')
fh = open(os.path.join('the_pkg', '__init__.py'), 'w')
fh.write('')
fh.close()
fh = open(os.path.join('the_pkg', 'test_things.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testOne(self):
pass
def testTwo(self):
pass
"""))
fh.close()
c = set(loader.getCompletions('').split('\n'))
self.assertIn('the_pkg', c)
self.assertIn('the_pkg.test_things', c)
self.assertIn('the_pkg.test_things.A.testOne', c)
self.assertIn('the_pkg.test_things.A.testTwo', c)
def test_completionDot(self):
"""
A '.' target generates completions for the whole directory
"""
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
os.chdir(tmpdir)
self.addCleanup(os.chdir, cwd)
os.mkdir('my_pkg')
fh = open(os.path.join('my_pkg', '__init__.py'), 'w')
fh.write('')
fh.close()
fh = open(os.path.join('my_pkg', 'test_things.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testOne(self):
pass
def testTwo(self):
pass
"""))
fh.close()
c = set(loader.getCompletions('.').split('\n'))
self.assertIn('my_pkg', c)
self.assertIn('my_pkg.test_things', c)
self.assertIn('my_pkg.test_things.A.testOne', c)
self.assertIn('my_pkg.test_things.A.testTwo', c)
def test_completionIgnoresErrors(self):
"""
Errors in one module don't block the remaining completions
"""
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
os.chdir(tmpdir)
self.addCleanup(os.chdir, cwd)
os.mkdir('my_pkg2')
fh = open(os.path.join('my_pkg2', '__init__.py'), 'w')
fh.write('')
fh.close()
fh = open(os.path.join('my_pkg2', 'test_crash01.py'), 'w')
contents = dedent(
"""
import unittest
class A(unittest.TestCase):
def testOne(self):
pass
def testTwo(self):
pass
""")
fh.write(contents)
fh.close()
fh = open(os.path.join('my_pkg2', 'test_crash02.py'), 'w')
fh.write("import moocow")
fh.close()
fh = open(os.path.join('my_pkg2', 'test_crash03.py'), 'w')
fh.write(contents)
fh.close()
c = set(loader.getCompletions('.').split('\n'))
self.assertIn('my_pkg2', c)
self.assertIn('my_pkg2.test_crash01', c)
self.assertIn('my_pkg2.test_crash01.A.testOne', c)
self.assertIn('my_pkg2.test_crash01.A.testTwo', c)
self.assertIn('my_pkg2.test_crash03', c)
self.assertIn('my_pkg2.test_crash03.A.testOne', c)
self.assertIn('my_pkg2.test_crash03.A.testTwo', c)
class TestIsPackage(unittest.TestCase):
def test_yes(self):
"""
A package is identified.
"""
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
fh = open(os.path.join(tmpdir, '__init__.py'), 'w')
fh.write('pass\n')
fh.close()
self.assertTrue(loader.isPackage(tmpdir))
def test_no(self):
"""
A non-package is identified
"""
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
self.assertFalse(loader.isPackage(tmpdir))
class TestDottedModule(unittest.TestCase):
def test_bad_path(self):
"""
A bad path causes an exception
"""
self.assertRaises(
ValueError,
loader.findDottedModuleAndParentDir, tempfile.tempdir)
def test_good_path(self):
"""
A good path gets (dotted_module, parent) properly returned
"""
tmpdir = tempfile.mkdtemp()
os.makedirs(os.path.join(tmpdir, 'a', 'b', 'c', 'd'))
package_init = os.path.join(tmpdir, 'a', 'b', 'c', '__init__.py')
subpkg_init = os.path.join(tmpdir, 'a', 'b', 'c', 'd', '__init__.py')
module_name = 'stuff.py'
module = os.path.join(tmpdir, 'a', 'b', 'c', 'd', module_name)
for filename in [package_init, subpkg_init, module]:
fh = open(filename, 'w')
fh.write('pass\n')
fh.close()
self.assertEqual(loader.findDottedModuleAndParentDir(module),
('c.d.stuff', os.path.join(tmpdir, 'a', 'b')))
class TestLoadFromTestCase(unittest.TestCase):
def test_runTest(self):
"""
When a testcase has no matching method names, but does have a runTest,
use that instead.
"""
class MyTestCase(unittest.TestCase):
def helper1(self):
pass
def helper2(self):
pass
def runTest(self):
pass
suite = loader.loadFromTestCase(MyTestCase)
self.assertEqual(suite.countTestCases(), 1)
self.assertEqual(suite._tests[0]._testMethodName, 'runTest')
def test_normal(self):
"""
Normal test methods get loaded
"""
class Normal(unittest.TestCase):
def test_method1(self):
pass
def test_method2(self):
pass
suite = loader.loadFromTestCase(Normal)
self.assertEqual(suite.countTestCases(), 2)
self.assertEqual(set([x._testMethodName for x in suite._tests]),
set(['test_method1', 'test_method2']))
def test_isTestCaseDisabled(self):
"""
TestCases disabled by nose generators don't get loaded
"""
class HasDisabled(unittest.TestCase):
def test_method(self):
pass
test_method.__test__ = False
suite = loader.loadFromTestCase(HasDisabled)
self.assertEqual(suite.countTestCases(), 0)
class TestLoadFromModuleFilename(unittest.TestCase):
def test_skipped_module(self):
"""
A module that wants to be skipped gets skipped
"""
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
filename = os.path.join(tmpdir, 'skipped_module.py')
fh = open(filename, 'w')
fh.write(dedent(
"""
import unittest
raise unittest.case.SkipTest
class NotReached(unittest.TestCase):
def test_one(self):
pass
def test_two(self):
pass
"""))
fh.close()
suite = loader.loadFromModuleFilename(filename)
self.assertEqual(suite.countTestCases(), 1)
self.assertRaises(unittest.case.SkipTest,
getattr(suite._tests[0], suite._tests[0]._testMethodName))
class TestDiscover(unittest.TestCase):
def test_bad_input(self):
"""
discover() raises ImportError when passed a non-directory
"""
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
self.assertRaises(ImportError, loader.discover,
os.path.join(tmpdir, 'garbage_in'))
filename = os.path.join(tmpdir, 'some_file.py')
fh = open(filename, 'w')
fh.write('pass\n')
fh.close()
self.assertRaises(ImportError, loader.discover, filename)
def test_bad_pkg_name(self):
"""
If the directory is an invalid package name, don't bother looking in it.
"""
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
startdir = os.getcwd()
os.chdir(tmpdir)
self.addCleanup(os.chdir, startdir)
bad_pkg_name = '1badname'
os.mkdir(bad_pkg_name)
tmp_subdir = os.path.join(tmpdir, bad_pkg_name)
fh = open(os.path.join(tmp_subdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
named_module = os.path.join(os.path.basename(tmp_subdir),
'named_module.py')
fh = open(named_module, 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
self.assertEqual(loader.discover(tmpdir), None)
def test_symlink(self):
"""
If the directory is a symlink, it should be skipped.
"""
if platform.system() == 'Windows': # pragma: no cover
self.skipTest('This test is for posix-specific behavior')
tmpdir = tempfile.mkdtemp()
tmpdir2 = tempfile.mkdtemp()
os.symlink(tmpdir, os.path.join(tmpdir2, 'link'))
self.addCleanup(shutil.rmtree, tmpdir)
startdir = os.getcwd()
os.chdir(tmpdir)
self.addCleanup(os.chdir, startdir)
pkg_name = 'realpkg'
os.mkdir(pkg_name)
tmp_subdir = os.path.join(tmpdir, pkg_name)
fh = open(os.path.join(tmp_subdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
named_module = os.path.join(os.path.basename(tmp_subdir),
'test_module.py')
fh = open(named_module, 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
self.assertEqual(loader.discover(tmpdir2), None)
class TestLoadTargets(unittest.TestCase):
# Setup
@classmethod
def setUpClass(cls):
cls.startdir = os.getcwd()
cls.container_dir = tempfile.mkdtemp()
@classmethod
def tearDownClass(cls):
if os.getcwd() != cls.startdir:
os.chdir(cls.startdir)
cls.startdir = None
shutil.rmtree(cls.container_dir)
def setUp(self):
os.chdir(self.container_dir)
self.tmpdir = tempfile.mkdtemp(dir=self.container_dir)
def tearDown(self):
os.chdir(self.container_dir)
shutil.rmtree(self.tmpdir)
# Tests
def test_returnIsLoadable(self):
"""
Results returned by toParallelTargets should be loadable by
loadTargets(), even if they aren't directly loadable through a package
relative to the current working directory.
"""
tests_dir = tempfile.mkdtemp(dir=self.tmpdir)
# No __init__.py in the directory!
fh = open(os.path.join(tests_dir, 'test_not_in_pkg.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""
))
fh.close()
# Discover stuff
suite = loader.loadTargets('.')
# This should resolve it to the module that's not importable from here
test = loader.toParallelTargets(suite, [])[0]
loader.loadTargets(test)
def test_emptyDirAbsolute(self):
"""
Absolute path to empty directory returns None
"""
tests = loader.loadTargets(self.tmpdir)
self.assertTrue(tests == None)
def test_emptyDirRelative(self):
"""
Relative path to empty directory returns None
"""
os.chdir(self.tmpdir)
os.chdir('..')
tests = loader.loadTargets(os.path.dirname(self.tmpdir))
self.assertEqual(tests, None)
def test_emptyDirDot(self):
"""
'.' while in an empty directory returns None
"""
os.chdir(self.tmpdir)
tests = loader.loadTargets('.')
self.assertTrue(tests == None)
def test_relativeDotDir(self):
"""
Dotted relative path to empty directory returns None
"""
os.chdir(self.tmpdir)
os.chdir('..')
target = os.path.join('.', os.path.basename(self.tmpdir))
tests = loader.loadTargets(target)
self.assertTrue(tests == None)
def test_BigDirWithAbsoluteImports(self):
"""
Big dir discovers tests and doesn't crash on absolute import
"""
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
pkg_name = os.path.basename(sub_tmpdir)
# Child setup
# pkg/__init__.py
fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
# pkg/target_module.py
fh = open(os.path.join(sub_tmpdir, 'target_module.py'), 'w')
fh.write('a = 1\n')
fh.close()
# pkg/test/__init__.py
os.mkdir(os.path.join(sub_tmpdir, 'test'))
fh = open(os.path.join(sub_tmpdir, 'test', '__init__.py'), 'w')
fh.write('\n')
fh.close()
# pkg/test/test_target_module.py
fh = open(os.path.join(sub_tmpdir, 'test', 'test_target_module.py'), 'w')
fh.write(dedent(
"""
import unittest
import {}.target_module
class A(unittest.TestCase):
def testPass(self):
pass
""".format(pkg_name)))
fh.close()
# Load the tests
os.chdir(self.tmpdir)
test_suite = loader.loadTargets(pkg_name)
self.assertEqual(test_suite.countTestCases(), 1)
# Dotted name should start with the package!
self.assertEqual(
pkg_name + '.test.test_target_module.A.testPass',
loader.toProtoTestList(test_suite)[0].dotted_name)
def test_DirWithInit(self):
"""
Dir empty other than blank __init__.py returns None
"""
# Parent directory setup
os.chdir(self.tmpdir)
os.chdir('..')
# Child setup
target = os.path.join(self.tmpdir, '__init__.py')
fh = open(target, 'w')
fh.write('\n')
fh.close()
fh = open(os.path.join(self.tmpdir, 'test_module_with_init.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
# Load the tests
module_name = os.path.basename(self.tmpdir)
tests = loader.loadTargets(module_name)
self.assertEqual(tests.countTestCases(), 1)
def test_DottedName(self):
"""
Importing a module via dotted name loads the tests.
"""
# Parent directory setup
os.chdir(self.tmpdir)
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
basename = os.path.basename(sub_tmpdir)
# Child setup
fh = open(os.path.join(basename, '__init__.py'), 'w')
fh.write('\n')
fh.close()
fh = open(os.path.join(basename, 'test_module_dotted_name.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
# Load the tests
module_name = basename + ".test_module_dotted_name"
tests = loader.loadTargets(module_name)
self.assertEqual(tests.countTestCases(), 1)
def test_DottedNamePackageFromPath(self):
"""
Importing a package from path loads the tests.
"""
# Child setup
tmp_subdir = tempfile.mkdtemp(dir=self.tmpdir)
fh = open(os.path.join(tmp_subdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
fh = open(os.path.join(tmp_subdir, 'test_module.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
# Go somewhere else, but setup the path
os.chdir(self.startdir)
sys.path.insert(0, self.tmpdir)
# Load the tests
tests = loader.loadTargets(os.path.basename(tmp_subdir))
sys.path.remove(self.tmpdir)
self.assertTrue(tests.countTestCases(), 1)
def test_ModuleByName(self):
"""
A module in a package can be loaded by filename.
"""
os.chdir(self.tmpdir)
tmp_subdir = tempfile.mkdtemp(dir=self.tmpdir)
fh = open(os.path.join(tmp_subdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
named_module = os.path.join(os.path.basename(tmp_subdir),
'named_module.py')
fh = open(named_module, 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
# Load the tests
tests = loader.loadTargets(named_module)
try:
self.assertEqual(tests.countTestCases(), 1)
except:
raise
finally:
shutil.rmtree(tmp_subdir)
def test_MalformedModuleByName(self):
"""
Importing malformed module by name creates test that raises ImportError.
"""
fh = open(os.path.join(self.tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
malformed_module = os.path.join(os.path.basename(self.tmpdir),
'malformed_module.py')
fh = open(malformed_module, 'w')
fh.write("This is a malformed module.")
fh.close()
# Load the tests
tests = loader.loadTargets(malformed_module)
self.assertEqual(tests.countTestCases(), 1)
test = tests._tests[0]._tests[0]
test_method = getattr(test, test._testMethodName)
self.assertRaises(ImportError, test_method)
def test_partiallyGoodName(self):
"""
Don't crash loading module.object with existing module but not object
"""
# Parent directory setup
os.chdir(self.tmpdir)
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
basename = os.path.basename(sub_tmpdir)
# Child setup
fh = open(os.path.join(basename, '__init__.py'), 'w')
fh.write('\n')
fh.close()
fh = open(os.path.join(basename, 'existing_module.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPass(self):
pass
"""))
fh.close()
# Load the tests
module_name = basename + ".existing_module.nonexistant_object"
tests = loader.loadTargets(module_name)
self.assertEqual(tests, None)
def test_multiple_targets(self):
"""
Specifying multiple targets causes them all to be tested
"""
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
# pkg/__init__.py
fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
# pkg/test/test_target1.py
fh = open(os.path.join(sub_tmpdir, 'test_target1.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
# pkg/test/test_target2.py
fh = open(os.path.join(sub_tmpdir, 'test_target2.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
# Load the tests
os.chdir(self.tmpdir)
pkg = os.path.basename(sub_tmpdir)
tests = loader.loadTargets([pkg + '.' + 'test_target1',
pkg + '.' + 'test_target2'])
self.assertEqual(tests.countTestCases(), 2)
def test_duplicate_targets(self):
"""
Specifying duplicate targets does not cause duplicate loading.
"""
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
fh = open(os.path.join(sub_tmpdir, 'test_dupe_target.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
os.chdir(self.tmpdir)
pkg = os.path.basename(sub_tmpdir)
tests = loader.loadTargets([pkg + '.' + 'test_dupe_target',
pkg + '.' + 'test_dupe_target',
pkg + '.' + 'test_dupe_target'])
self.assertEqual(tests.countTestCases(), 1)
def test_explicit_filename_error(self):
"""
Loading a module by name with a syntax error produces a failure, not a
silent absence of its tests.
"""
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
fh = open(os.path.join(sub_tmpdir, 'mod_with_import_error.py'), 'w')
fh.write('this is a syntax error')
fh.close()
os.chdir(sub_tmpdir)
tests = loader.loadTargets('mod_with_import_error.py')
self.assertEqual(tests.countTestCases(), 1)
def test_file_pattern(self):
"""
Specifying a file pattern causes only matching files to be loaded
"""
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
# pkg/__init__.py
fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
# pkg/test/target1_tests.py
fh = open(os.path.join(sub_tmpdir, 'target1_tests.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
# pkg/test/target2_tests.py
fh = open(os.path.join(sub_tmpdir, 'target2_tests.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
# pkg/test/test_target999.py: NOT a match.
fh = open(os.path.join(sub_tmpdir, 'test_target999.py'), 'w')
fh.write(dedent(
"""
import unittest
class A(unittest.TestCase):
def testPasses(self):
pass
"""))
fh.close()
# Load the tests
os.chdir(self.tmpdir)
pkg = os.path.basename(sub_tmpdir)
tests = loader.loadTargets(pkg, file_pattern='*_tests.py')
self.assertEqual(tests.countTestCases(), 2)