xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | This program handles properly importing the App Engine SDK so that test modules |
| 8 | can use google.appengine.* APIs and the Google App Engine testbed. |
| 9 | |
| 10 | Example invocation: |
| 11 | $ python runner.py ~/google-cloud-sdk [your sdk path] |
| 12 | """ |
| 13 | |
| 14 | import argparse |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 15 | import logging |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 16 | import os |
| 17 | import sys |
| 18 | import unittest |
| 19 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 20 | import gae_import |
| 21 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 22 | |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 23 | # Mapping between test type and test files' pattern. |
| 24 | TEST_PATTERN_MAP = {'unittest': '*_unittest.py', |
Xixuan Wu | 4ac56dd | 2017-10-12 11:59:30 -0700 | [diff] [blame] | 25 | 'integration': '*_integration_test.py', |
| 26 | 'sanity': '*sanity_test.py'} |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 27 | |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 28 | |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 29 | def main(input_args): |
| 30 | """The main function to run unittest/integration tests. |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 31 | |
| 32 | Args: |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 33 | input_args: the input args. |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 34 | |
| 35 | Returns: |
| 36 | a unittest.TextTestRunner object. |
| 37 | """ |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 38 | if input_args.sdk_path != gae_import.GOOGLE_CLOUD_SDK_PATH: |
| 39 | gae_import.import_sdk_path(input_args.sdk_path) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 40 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 41 | gae_import.import_appengine_config() |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 42 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 43 | # Discover and run tests. |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 44 | if input_args.test_file: |
| 45 | suites = unittest.loader.TestLoader().discover( |
| 46 | input_args.test_path, input_args.test_file) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 47 | else: |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 48 | suites = unittest.loader.TestLoader().discover( |
| 49 | input_args.test_path, TEST_PATTERN_MAP[input_args.test_type]) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 50 | |
| 51 | return unittest.TextTestRunner(verbosity=2).run(suites) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 52 | |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 53 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 54 | def _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 Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 61 | help='The path of Google App Engine SDK and Google Cloud SDK.', |
Ned Nguyen | b5ec6f2 | 2020-06-29 20:35:45 +0000 | [diff] [blame] | 62 | default=gae_import.GOOGLE_CLOUD_SDK_PATH) |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 63 | parser.add_argument( |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 64 | '--test_path', |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 65 | help='The path to look for tests, defaults to the current directory.', |
| 66 | default=os.getcwd()) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 67 | group = parser.add_mutually_exclusive_group() |
| 68 | group.add_argument( |
Xixuan Wu | 4ac56dd | 2017-10-12 11:59:30 -0700 | [diff] [blame] | 69 | '--test_type', choices=['unittest', 'integration', 'sanity'], |
| 70 | help=('The test type, including unittest, integration tests and sanity ' |
| 71 | 'tests, defaults to unittest'), |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 72 | default='unittest') |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 73 | 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 Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 83 | return parser |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 87 | unittest_parser = _make_parser() |
| 88 | args = unittest_parser.parse_args() |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 89 | if args.debug: |
| 90 | logging.getLogger().setLevel(logging.DEBUG) |
| 91 | else: |
| 92 | logging.getLogger().setLevel(logging.CRITICAL) |
| 93 | |
Nghia Nguyen | b11aba8 | 2020-06-30 16:30:04 -0600 | [diff] [blame] | 94 | # Normalize to full path in case user input path is in abbreviated form. |
| 95 | args.sdk_path = os.path.abspath(os.path.expanduser(args.sdk_path)) |
| 96 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 97 | try: |
Ned Nguyen | b5ec6f2 | 2020-06-29 20:35:45 +0000 | [diff] [blame] | 98 | gae_import.import_sdk_path(args.sdk_path) |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 99 | except ImportError: |
Nghia Nguyen | b11aba8 | 2020-06-30 16:30:04 -0600 | [diff] [blame] | 100 | print('Cannot find google SDK in %s. Please specify a correct Google ' |
| 101 | 'Cloud SDK path through --sdk_path flag.' % args.sdk_path) |
| 102 | sys.exit(1) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 103 | |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 104 | result = main(args) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 105 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 106 | if not result.wasSuccessful(): |
| 107 | sys.exit(1) |