blob: 919b2c74d06a680ce0efc8f96b0d91235b0457fe [file] [log] [blame]
xixuan82753172017-08-07 09:22:50 -07001# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for unittest framework.
6
7This program handles properly importing the App Engine SDK so that test modules
8can use google.appengine.* APIs and the Google App Engine testbed.
9
10Example invocation:
11 $ python runner.py ~/google-cloud-sdk [your sdk path]
12"""
13
14import argparse
15import os
16import sys
17import unittest
18
19
20def fixup_paths(path):
21 """Adds GAE SDK path to system path and appends it to the google path
22 if that already exists."""
23 # Not all Google packages are inside namespace packages, which means
24 # there might be another non-namespace package named `google` already on
25 # the path and simply appending the App Engine SDK to the path will not
26 # work since the other package will get discovered and used first.
27 # This emulates namespace packages by first searching if a `google` package
28 # exists by importing it, and if so appending to its module search path.
29 try:
30 import google
31 google.__path__.append("{0}/google".format(path))
32 except ImportError:
33 pass
34
35 sys.path.insert(0, path)
36
37
38def main(sdk_path, test_path, test_pattern):
39 # If the SDK path points to a Google Cloud SDK installation
40 # then we should alter it to point to the GAE platform location.
41 if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
42 sdk_path = os.path.join(sdk_path, 'platform/google_appengine')
43
44 # Make sure google.appengine.* modules are importable.
45 fixup_paths(sdk_path)
46
47 # Make sure all bundled third-party packages are available.
48 import dev_appserver
49 dev_appserver.fix_sys_path()
50
51 # Loading appengine_config from the current project ensures that any
52 # changes to configuration there are available to all tests (e.g.
53 # sys.path modifications, namespaces, etc.)
54 try:
55 import appengine_config
56 (appengine_config)
57 except ImportError:
58 print('Note: unable to import appengine_config.')
59
60 # Discover and run tests.
61 suite = unittest.loader.TestLoader().discover(test_path, test_pattern)
62 return unittest.TextTestRunner(verbosity=2).run(suite)
63
64
65if __name__ == '__main__':
66 parser = argparse.ArgumentParser(
67 description=__doc__,
68 formatter_class=argparse.RawDescriptionHelpFormatter)
69 parser.add_argument(
70 'sdk_path',
71 help='The path to the Google App Engine SDK or the Google Cloud SDK.')
72 parser.add_argument(
73 '--test-path',
74 help='The path to look for tests, defaults to the current directory.',
75 default=os.getcwd())
76 parser.add_argument(
77 '--test-pattern',
78 help='The file pattern for test modules, defaults to *_test.py.',
79 default='*_test.py')
80
81 args = parser.parse_args()
82
83 result = main(args.sdk_path, args.test_path, args.test_pattern)
84
85 if not result.wasSuccessful():
86 sys.exit(1)