Add a simple test runner to execute test modules directly.

absltest requires running from main(), which isn't exactly compatible with stock unittest. Walk the package tree looking for modules under glazier named *_test and execute them inside subprocesses.

PiperOrigin-RevId: 240235406
This commit is contained in:
mattl
2019-03-25 15:54:13 -07:00
committed by TitoAldarondo
parent 4e073f736b
commit db5e9e0558
3 changed files with 65 additions and 1 deletions

View File

@@ -7,4 +7,4 @@ install:
- pip install -r requirements.txt
script:
- python -m unittest discover -s . -p '*_test.py'
- python -m testing.run_tests

13
testing/__init__.py Normal file
View File

@@ -0,0 +1,13 @@
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

51
testing/run_tests.py Normal file
View File

@@ -0,0 +1,51 @@
# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Locate *_test modules and run the tests in them."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pkgutil
import re
import subprocess
import sys
import glazier
FAILED_RE = re.compile(r'FAILED\s*\(errors=(\d*)\)')
def main():
results = {'codes': {0: 0, 1: 0}, 'errors': 0}
for _, test, _ in pkgutil.walk_packages(glazier.__path__,
glazier.__name__ + '.'):
if '_test' in test:
print('**** %s ****\n' % test)
proc = subprocess.Popen(['python', '-m', test], stderr=subprocess.PIPE)
_, err = proc.communicate()
print(err)
failed = FAILED_RE.search(err)
if failed:
results['errors'] += int(failed.group(1))
results['codes'][proc.returncode] = results['codes'].setdefault(
proc.returncode, 0) + 1
print('Success: %s' % results['codes'][0])
print('Failure: %s' % results['codes'][1])
sys.exit(results['codes'][1])
if __name__ == '__main__':
main()