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', |
| 25 | 'integration': '*_integration_test.py'} |
| 26 | |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 27 | |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 28 | def main(input_args): |
| 29 | """The main function to run unittest/integration tests. |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 30 | |
| 31 | Args: |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 32 | input_args: the input args. |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 33 | |
| 34 | Returns: |
| 35 | a unittest.TextTestRunner object. |
| 36 | """ |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 37 | if input_args.sdk_path != gae_import.GOOGLE_CLOUD_SDK_PATH: |
| 38 | gae_import.import_sdk_path(input_args.sdk_path) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 39 | |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 40 | gae_import.import_appengine_config() |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 41 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 42 | # Discover and run tests. |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 43 | if input_args.test_file: |
| 44 | suites = unittest.loader.TestLoader().discover( |
| 45 | input_args.test_path, input_args.test_file) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 46 | else: |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 47 | suites = unittest.loader.TestLoader().discover( |
| 48 | input_args.test_path, TEST_PATTERN_MAP[input_args.test_type]) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 49 | |
| 50 | return unittest.TextTestRunner(verbosity=2).run(suites) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 51 | |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 52 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 53 | def _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 Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 60 | help='The path of Google App Engine SDK and Google Cloud SDK.', |
| 61 | default=gae_import.GOOGLE_CLOUD_SDK_PATH) |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 62 | parser.add_argument( |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 63 | '--test_path', |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 64 | help='The path to look for tests, defaults to the current directory.', |
| 65 | default=os.getcwd()) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 66 | group = parser.add_mutually_exclusive_group() |
| 67 | group.add_argument( |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 68 | '--test_type', choices=['unittest', 'integration'], |
| 69 | help=('The test type, including unittest and integration test, defaults ' |
| 70 | 'to unittest'), |
| 71 | default='unittest') |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 72 | 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 Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 82 | return parser |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 86 | unittest_parser = _make_parser() |
| 87 | args = unittest_parser.parse_args() |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 88 | if args.debug: |
| 89 | logging.getLogger().setLevel(logging.DEBUG) |
| 90 | else: |
| 91 | logging.getLogger().setLevel(logging.CRITICAL) |
| 92 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 93 | try: |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 94 | gae_import.import_sdk_path(args.sdk_path) |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 95 | except ImportError: |
| 96 | args.sdk_path = raw_input('Cannot find google SDK in %s. Please specify ' |
Xixuan Wu | 26d06e0 | 2017-09-20 14:50:28 -0700 | [diff] [blame] | 97 | 'your google SDK path: ' % |
| 98 | gae_import.GOOGLE_CLOUD_SDK_PATH) |
xixuan | 8275317 | 2017-08-07 09:22:50 -0700 | [diff] [blame] | 99 | |
Xixuan Wu | 7e30c9d | 2017-09-05 18:46:00 -0700 | [diff] [blame] | 100 | result = main(args) |
Xixuan Wu | f4e15c4 | 2017-08-24 14:06:03 -0700 | [diff] [blame] | 101 | |
Xixuan Wu | 3dd3748 | 2017-08-21 14:18:36 -0700 | [diff] [blame] | 102 | if not result.wasSuccessful(): |
| 103 | sys.exit(1) |