run_tests 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # Copyright 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Wrapper to run pytest with the right settings."""
  17. from __future__ import print_function
  18. import errno
  19. import os
  20. import subprocess
  21. import sys
  22. def run_pytest(cmd, argv):
  23. """Run the unittests via |cmd|."""
  24. try:
  25. subprocess.check_call([cmd] + argv)
  26. return 0
  27. except OSError as e:
  28. if e.errno == errno.ENOENT:
  29. print('%s: unable to run `%s`: %s' % (__file__, cmd, e), file=sys.stderr)
  30. print('%s: Try installing pytest: sudo apt-get install python-pytest' %
  31. (__file__,), file=sys.stderr)
  32. return 1
  33. else:
  34. raise
  35. def main(argv):
  36. """The main entry."""
  37. # Add the repo tree to PYTHONPATH as the tests expect to be able to import
  38. # modules directly.
  39. topdir = os.path.dirname(os.path.realpath(__file__))
  40. pythonpath = os.environ.get('PYTHONPATH', '')
  41. os.environ['PYTHONPATH'] = '%s:%s' % (topdir, pythonpath)
  42. return run_pytest('pytest', argv)
  43. if __name__ == '__main__':
  44. sys.exit(main(sys.argv[1:]))