Files
android_development/gsi/gsi_util/run_test.py
Bowgo Tsai a1f4058845 gsi_util: adding cmd_utils
Adding a run_command function to execute a host command.
Also support reading the stdout and/or stderr of the result if desired,
which might be useful for further parsing and flow decision.

A typical usage will be passing a command sequence:
  cmd_utils.run_command(['echo', '123'])

It also supports running a 'shell' command, by passing a single string:
  cmd_utils.run_command('echo 123', shell=True)

To get the stdout and/or stderr data, just add 'read_stdout=True' and/or
'read_stderr=True':
  cmd_utils.run_command('echo 123', shell=True,
                        read_stdout=True, read_stderr=True),

which returns a namedtuple:
  ('CommandResult', 'returncode stdoutdata, stderrdata').

Note that other keyword arguments will be passed to subprocess.Popen().
e.g., the following command will change current directory to
'my_working_dir' prior to execute the command:
  cmd_utils.run_command('echo 123', shell=True, cwd=my_working_dir)

More usage examples can be found in cmd_utils_unittest.py.

Also adds a run_test.py to search then run ./*tests/*_unittest.py files.

Bug: 70477387
Test: make gsi_util
Test: ./run_test.py
Change-Id: Id3aae935f941818fe7415798937fd07dbbe6ba33
2017-12-29 17:11:27 +08:00

46 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python
#
# Copyright 2017 - The Android Open Source Project
#
# 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.
"""Script to run */tests/*_unittest.py files."""
from multiprocessing import Process
import os
import runpy
def get_unittest_files():
matches = []
for dirpath, _, filenames in os.walk('.'):
if os.path.basename(dirpath) == 'tests':
matches.extend(os.path.join(dirpath, f)
for f in filenames if f.endswith('_unittest.py'))
return matches
def run_test(unittest_file):
runpy.run_path(unittest_file, run_name='__main__')
if __name__ == '__main__':
for path in get_unittest_files():
# Forks a process to run the unittest.
# Otherwise, it only runs one unittest.
p = Process(target=run_test, args=(path,))
p.start()
p.join()
if p.exitcode != 0:
break # stops on any failure unittest