blob: 4a027b23981235457efe057a11f58da546d331cd [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
Xixuan Wuf4e15c42017-08-24 14:06:03 -070015import logging
xixuan82753172017-08-07 09:22:50 -070016import os
17import sys
18import unittest
19
Xixuan Wu26d06e02017-09-20 14:50:28 -070020import gae_import
21
Xixuan Wu3dd37482017-08-21 14:18:36 -070022
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070023# Mapping between test type and test files' pattern.
24TEST_PATTERN_MAP = {'unittest': '*_unittest.py',
25 'integration': '*_integration_test.py'}
26
xixuan82753172017-08-07 09:22:50 -070027
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070028def main(input_args):
29 """The main function to run unittest/integration tests.
Xixuan Wuf4e15c42017-08-24 14:06:03 -070030
31 Args:
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070032 input_args: the input args.
Xixuan Wuf4e15c42017-08-24 14:06:03 -070033
34 Returns:
35 a unittest.TextTestRunner object.
36 """
Xixuan Wu26d06e02017-09-20 14:50:28 -070037 if input_args.sdk_path != gae_import.GOOGLE_CLOUD_SDK_PATH:
38 gae_import.import_sdk_path(input_args.sdk_path)
xixuan82753172017-08-07 09:22:50 -070039
Xixuan Wu26d06e02017-09-20 14:50:28 -070040 gae_import.import_appengine_config()
xixuan82753172017-08-07 09:22:50 -070041
Xixuan Wu3dd37482017-08-21 14:18:36 -070042 # Discover and run tests.
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070043 if input_args.test_file:
44 suites = unittest.loader.TestLoader().discover(
45 input_args.test_path, input_args.test_file)
Xixuan Wuf4e15c42017-08-24 14:06:03 -070046 else:
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070047 suites = unittest.loader.TestLoader().discover(
48 input_args.test_path, TEST_PATTERN_MAP[input_args.test_type])
Xixuan Wuf4e15c42017-08-24 14:06:03 -070049
50 return unittest.TextTestRunner(verbosity=2).run(suites)
xixuan82753172017-08-07 09:22:50 -070051
xixuan82753172017-08-07 09:22:50 -070052
Xixuan Wu3dd37482017-08-21 14:18:36 -070053def _make_parser():
54 """Return unittest parser."""
55 parser = argparse.ArgumentParser(
56 description=__doc__,
57 formatter_class=argparse.RawDescriptionHelpFormatter)
58 parser.add_argument(
59 '--sdk_path',
Xixuan Wu26d06e02017-09-20 14:50:28 -070060 help='The path of Google App Engine SDK and Google Cloud SDK.',
61 default=gae_import.GOOGLE_CLOUD_SDK_PATH)
Xixuan Wu3dd37482017-08-21 14:18:36 -070062 parser.add_argument(
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070063 '--test_path',
Xixuan Wu3dd37482017-08-21 14:18:36 -070064 help='The path to look for tests, defaults to the current directory.',
65 default=os.getcwd())
Xixuan Wuf4e15c42017-08-24 14:06:03 -070066 group = parser.add_mutually_exclusive_group()
67 group.add_argument(
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070068 '--test_type', choices=['unittest', 'integration'],
69 help=('The test type, including unittest and integration test, defaults '
70 'to unittest'),
71 default='unittest')
Xixuan Wuf4e15c42017-08-24 14:06:03 -070072 group.add_argument(
73 'test_file',
74 nargs='?',
75 help=('A single test module to test, default to empty string, which '
76 'means the runner will find test modules by test-pattern.'),
77 default='')
78 parser.add_argument(
79 '--debug',
80 action='store_true',
81 help='Display the logging in unittest.')
Xixuan Wu3dd37482017-08-21 14:18:36 -070082 return parser
xixuan82753172017-08-07 09:22:50 -070083
84
85if __name__ == '__main__':
Xixuan Wu3dd37482017-08-21 14:18:36 -070086 unittest_parser = _make_parser()
87 args = unittest_parser.parse_args()
Xixuan Wuf4e15c42017-08-24 14:06:03 -070088 if args.debug:
89 logging.getLogger().setLevel(logging.DEBUG)
90 else:
91 logging.getLogger().setLevel(logging.CRITICAL)
92
Xixuan Wu3dd37482017-08-21 14:18:36 -070093 try:
Xixuan Wu26d06e02017-09-20 14:50:28 -070094 gae_import.import_sdk_path(args.sdk_path)
Xixuan Wu3dd37482017-08-21 14:18:36 -070095 except ImportError:
96 args.sdk_path = raw_input('Cannot find google SDK in %s. Please specify '
Xixuan Wu26d06e02017-09-20 14:50:28 -070097 'your google SDK path: ' %
98 gae_import.GOOGLE_CLOUD_SDK_PATH)
xixuan82753172017-08-07 09:22:50 -070099
Xixuan Wu7e30c9d2017-09-05 18:46:00 -0700100 result = main(args)
Xixuan Wuf4e15c42017-08-24 14:06:03 -0700101
Xixuan Wu3dd37482017-08-21 14:18:36 -0700102 if not result.wasSuccessful():
103 sys.exit(1)