1bd1be0e318bb84a4f6abd21a40f8ab795c255b9
[platform/upstream/pygobject2.git] / tests / runtests.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3
4 import os
5 import glob
6 import sys
7
8 import unittest
9
10 # provide missing unittest decorators and API for python 2.6; these decorators
11 # do not actually work, just avoid the syntax failure
12 if sys.version_info[:2] == (2, 6):
13     def skipUnless(condition, reason):
14         if not condition:
15             sys.stderr.write('[expected failure] ')
16         return lambda obj: obj
17
18     unittest.skipUnless = skipUnless
19     unittest.expectedFailure = lambda obj: obj
20
21     def skipIf(condition, reason):
22         if condition:
23             sys.stderr.write('[expected failure] ')
24         return lambda obj: obj
25
26     unittest.skipIf = skipUnless
27
28     def assertGreater(self, a, b, msg=None):
29         if not a > b:
30             self.fail('%s not greater than %s' % (repr(a), repr(b)))
31
32     def assertGreaterEqual(self, a, b, msg=None):
33         if not a >= b:
34             self.fail('%s not greater than or equal to %s' % (repr(a), repr(b)))
35
36     def assertLess(self, a, b, msg=None):
37         if not a < b:
38             self.fail('%s not less than %s' % (repr(a), repr(b)))
39
40     def assertLessEqual(self, a, b, msg=None):
41         if not a <= b:
42             self.fail('%s not less than or equal to %s' % (repr(a), repr(b)))
43
44     def assertIsInstance(self, obj, cls, msg=None):
45         if not isinstance(obj, cls):
46             self.fail('%s is not an instance of %r' % (repr(obj), cls))
47
48     unittest.TestCase.assertGreaterEqual = assertGreaterEqual
49     unittest.TestCase.assertGreater = assertGreater
50     unittest.TestCase.assertLessEqual = assertLessEqual
51     unittest.TestCase.assertLess = assertLess
52     unittest.TestCase.assertIsInstance = assertIsInstance
53
54 if sys.version_info[:2] == (2, 7):
55     unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
56
57 if '--help' in sys.argv:
58     print("Usage: ./runtests.py <testfiles>")
59     sys.exit(0)
60
61 mydir = os.path.dirname(os.path.abspath(__file__))
62 tests_builddir = os.path.abspath(os.environ.get('TESTS_BUILDDIR', os.path.dirname(__file__)))
63 builddir = os.path.dirname(tests_builddir)
64
65 # we have to do this here instead of Makefile.am so that the implicitly added
66 # directory for the source file comes after the builddir
67 sys.path.insert(0, tests_builddir)
68 sys.path.insert(0, builddir)
69
70 # force untranslated messages, as we check for them in some tests
71 os.environ['LC_MESSAGES'] = 'C'
72 os.environ['G_DEBUG'] = 'fatal-warnings fatal-criticals'
73
74 # make Gio able to find our gschemas.compiled in tests/. This needs to be set
75 # before importing Gio. Support a separate build tree, so look in build dir
76 # first.
77 os.environ['GSETTINGS_BACKEND'] = 'memory'
78 os.environ['GSETTINGS_SCHEMA_DIR'] = tests_builddir
79 os.environ['G_FILENAME_ENCODING'] = 'UTF-8'
80
81 # Load tests.
82 if 'TEST_NAMES' in os.environ:
83     names = os.environ['TEST_NAMES'].split()
84 elif 'TEST_FILES' in os.environ:
85     names = []
86     for filename in os.environ['TEST_FILES'].split():
87         names.append(filename[:-3])
88 elif len(sys.argv) > 1:
89     names = []
90     for filename in sys.argv[1:]:
91         names.append(filename.replace('.py', ''))
92 else:
93     names = []
94     for filename in glob.iglob(os.path.join(mydir, 'test_*.py')):
95         names.append(os.path.basename(filename)[:-3])
96
97 loader = unittest.TestLoader()
98 suite = loader.loadTestsFromNames(names)
99
100
101 # Run tests.
102 runner = unittest.TextTestRunner(verbosity=2)
103 result = runner.run(suite)
104 if not result.wasSuccessful():
105     sys.exit(1)  # exit code so "make check" reports error