File: discover_tests.py

package info (click to toggle)
python-future 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,316 kB
  • sloc: python: 42,522; makefile: 141; sh: 41
file content (58 lines) | stat: -rw-r--r-- 1,696 bytes parent folder | download | duplicates (3)
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
"""
Simple auto test discovery.

From http://stackoverflow.com/a/17004409
"""
import os
import sys
import unittest

if not hasattr(unittest.defaultTestLoader, 'discover'):
    try:
        import unittest2 as unittest
    except ImportError:
        raise ImportError('The unittest2 module is required to run tests on Python 2.6')

def additional_tests():
    setup_file = sys.modules['__main__'].__file__
    setup_dir = os.path.abspath(os.path.dirname(setup_file))
    test_dir = os.path.join(setup_dir, 'tests')
    test_suite = unittest.defaultTestLoader.discover(test_dir)
    blacklist = []
    if '/home/travis' in __file__:
        # Skip some tests that fail on travis-ci
        blacklist.append('test_command')
    return exclude_tests(test_suite, blacklist)


class SkipCase(unittest.TestCase):
    def skeleton_run_test(self):
        raise unittest.SkipTest("Test fails spuriously on travis-ci")


def exclude_tests(suite, blacklist):
    """
    Example:
    
    blacklist = [
        'test_some_test_that_should_be_skipped',
        'test_another_test_that_should_be_skipped'
    ]
    """
    new_suite = unittest.TestSuite()
    
    for test_group in suite._tests:
        for test in test_group:
            if not hasattr(test, '_tests'):
                # e.g. ModuleImportFailure
                new_suite.addTest(test)
                continue
            for subtest in test._tests:
                method = subtest._testMethodName
                if method in blacklist:
                    setattr(test,
                            method,
                            getattr(SkipCase(), 'skeleton_run_test'))
            new_suite.addTest(test)
    return new_suite