blob: 70e0ba7a9a59c1a6bec11529cc2f45a7bbbc0bf7 [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',
Xixuan Wu4ac56dd2017-10-12 11:59:30 -070025 'integration': '*_integration_test.py',
26 'sanity': '*sanity_test.py'}
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070027
xixuan82753172017-08-07 09:22:50 -070028
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070029def main(input_args):
30 """The main function to run unittest/integration tests.
Xixuan Wuf4e15c42017-08-24 14:06:03 -070031
32 Args:
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070033 input_args: the input args.
Xixuan Wuf4e15c42017-08-24 14:06:03 -070034
35 Returns:
36 a unittest.TextTestRunner object.
37 """
Xixuan Wu26d06e02017-09-20 14:50:28 -070038 if input_args.sdk_path != gae_import.GOOGLE_CLOUD_SDK_PATH:
39 gae_import.import_sdk_path(input_args.sdk_path)
xixuan82753172017-08-07 09:22:50 -070040
Xixuan Wu26d06e02017-09-20 14:50:28 -070041 gae_import.import_appengine_config()
xixuan82753172017-08-07 09:22:50 -070042
Xixuan Wu3dd37482017-08-21 14:18:36 -070043 # Discover and run tests.
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070044 if input_args.test_file:
45 suites = unittest.loader.TestLoader().discover(
46 input_args.test_path, input_args.test_file)
Xixuan Wuf4e15c42017-08-24 14:06:03 -070047 else:
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070048 suites = unittest.loader.TestLoader().discover(
49 input_args.test_path, TEST_PATTERN_MAP[input_args.test_type])
Xixuan Wuf4e15c42017-08-24 14:06:03 -070050
51 return unittest.TextTestRunner(verbosity=2).run(suites)
xixuan82753172017-08-07 09:22:50 -070052
xixuan82753172017-08-07 09:22:50 -070053
Xixuan Wu3dd37482017-08-21 14:18:36 -070054def _make_parser():
55 """Return unittest parser."""
56 parser = argparse.ArgumentParser(
57 description=__doc__,
58 formatter_class=argparse.RawDescriptionHelpFormatter)
59 parser.add_argument(
60 '--sdk_path',
Xixuan Wu26d06e02017-09-20 14:50:28 -070061 help='The path of Google App Engine SDK and Google Cloud SDK.',
62 default=gae_import.GOOGLE_CLOUD_SDK_PATH)
Xixuan Wu3dd37482017-08-21 14:18:36 -070063 parser.add_argument(
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070064 '--test_path',
Xixuan Wu3dd37482017-08-21 14:18:36 -070065 help='The path to look for tests, defaults to the current directory.',
66 default=os.getcwd())
Xixuan Wuf4e15c42017-08-24 14:06:03 -070067 group = parser.add_mutually_exclusive_group()
68 group.add_argument(
Xixuan Wu4ac56dd2017-10-12 11:59:30 -070069 '--test_type', choices=['unittest', 'integration', 'sanity'],
70 help=('The test type, including unittest, integration tests and sanity '
71 'tests, defaults to unittest'),
Xixuan Wu7e30c9d2017-09-05 18:46:00 -070072 default='unittest')
Xixuan Wuf4e15c42017-08-24 14:06:03 -070073 group.add_argument(
74 'test_file',
75 nargs='?',
76 help=('A single test module to test, default to empty string, which '
77 'means the runner will find test modules by test-pattern.'),
78 default='')
79 parser.add_argument(
80 '--debug',
81 action='store_true',
82 help='Display the logging in unittest.')
Xixuan Wu3dd37482017-08-21 14:18:36 -070083 return parser
xixuan82753172017-08-07 09:22:50 -070084
85
86if __name__ == '__main__':
Xixuan Wu3dd37482017-08-21 14:18:36 -070087 unittest_parser = _make_parser()
88 args = unittest_parser.parse_args()
Xixuan Wuf4e15c42017-08-24 14:06:03 -070089 if args.debug:
90 logging.getLogger().setLevel(logging.DEBUG)
91 else:
92 logging.getLogger().setLevel(logging.CRITICAL)
93
Xixuan Wu3dd37482017-08-21 14:18:36 -070094 try:
Xixuan Wu26d06e02017-09-20 14:50:28 -070095 gae_import.import_sdk_path(args.sdk_path)
Xixuan Wu3dd37482017-08-21 14:18:36 -070096 except ImportError:
97 args.sdk_path = raw_input('Cannot find google SDK in %s. Please specify '
Allen Li95c758c2018-09-27 17:54:32 -070098 'your Google Cloud SDK path: ' %
Xixuan Wu26d06e02017-09-20 14:50:28 -070099 gae_import.GOOGLE_CLOUD_SDK_PATH)
xixuan82753172017-08-07 09:22:50 -0700100
Xixuan Wu7e30c9d2017-09-05 18:46:00 -0700101 result = main(args)
Xixuan Wuf4e15c42017-08-24 14:06:03 -0700102
Xixuan Wu3dd37482017-08-21 14:18:36 -0700103 if not result.wasSuccessful():
104 sys.exit(1)