PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bitbake/lib/toaster/contrib/tts/toasteruitest/run_toastertests.py

https://gitlab.com/actawithamana/poky
Python | 155 lines | 82 code | 43 blank | 30 comment | 18 complexity | 2ac63ed15bdc44d5cc779bb9d82d64c2 MD5 | raw file
  1. #!/usr/bin/python
  2. # Copyright
  3. # DESCRIPTION
  4. # This is script for running all selected toaster cases on
  5. # selected web browsers manifested in toaster_test.cfg.
  6. # 1. How to start toaster in yocto:
  7. # $ source poky/oe-init-build-env
  8. # $ source toaster start
  9. # $ bitbake core-image-minimal
  10. # 2. How to install selenium on Ubuntu:
  11. # $ sudo apt-get install scrot python-pip
  12. # $ sudo pip install selenium
  13. # 3. How to install selenium addon in firefox:
  14. # Download the lastest firefox addon from http://release.seleniumhq.org/selenium-ide/
  15. # Then install it. You can also install firebug and firepath addon
  16. # 4. How to start writing a new case:
  17. # All you need to do is to implement the function test_xxx() and pile it on.
  18. # 5. How to test with Chrome browser
  19. # Download/install chrome on host
  20. # Download chromedriver from https://code.google.com/p/chromedriver/downloads/list according to your host type
  21. # put chromedriver in PATH, (e.g. /usr/bin/, bear in mind to chmod)
  22. # For windows host, you may put chromedriver.exe in the same directory as chrome.exe
  23. import unittest, sys, os, platform
  24. import ConfigParser
  25. import argparse
  26. from toaster_automation_test import toaster_cases
  27. def get_args_parser():
  28. description = "Script that runs toaster auto tests."
  29. parser = argparse.ArgumentParser(description=description)
  30. parser.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False,
  31. help='Run all tests.')
  32. parser.add_argument('--run-suite', required=False, dest='run_suite', default=False,
  33. help='run suite (defined in cfg file)')
  34. return parser
  35. def get_tests():
  36. testslist = []
  37. prefix = 'toaster_automation_test.toaster_cases'
  38. for t in dir(toaster_cases):
  39. if t.startswith('test_'):
  40. testslist.append('.'.join((prefix, t)))
  41. return testslist
  42. def get_tests_from_cfg(suite=None):
  43. testslist = []
  44. config = ConfigParser.SafeConfigParser()
  45. config.read('toaster_test.cfg')
  46. if suite is not None:
  47. target_suite = suite.lower()
  48. # TODO: if suite is valid suite
  49. else:
  50. target_suite = platform.system().lower()
  51. try:
  52. tests_from_cfg = eval(config.get('toaster_test_' + target_suite, 'test_cases'))
  53. except:
  54. print('Failed to get test cases from cfg file. Make sure the format is correct.')
  55. return None
  56. prefix = 'toaster_automation_test.toaster_cases.test_'
  57. for t in tests_from_cfg:
  58. testslist.append(prefix + str(t))
  59. return testslist
  60. def main():
  61. # In case this script is called from other directory
  62. os.chdir(os.path.abspath(sys.path[0]))
  63. parser = get_args_parser()
  64. args = parser.parse_args()
  65. if args.run_all_tests:
  66. testslist = get_tests()
  67. elif args.run_suite:
  68. testslist = get_tests_from_cfg(args.run_suite)
  69. os.environ['TOASTER_SUITE'] = args.run_suite
  70. else:
  71. testslist = get_tests_from_cfg()
  72. if not testslist:
  73. print('Failed to get test cases.')
  74. exit(1)
  75. suite = unittest.TestSuite()
  76. loader = unittest.TestLoader()
  77. loader.sortTestMethodsUsing = None
  78. runner = unittest.TextTestRunner(verbosity=2, resultclass=buildResultClass(args))
  79. for test in testslist:
  80. try:
  81. suite.addTests(loader.loadTestsFromName(test))
  82. except:
  83. return 1
  84. result = runner.run(suite)
  85. if result.wasSuccessful():
  86. return 0
  87. else:
  88. return 1
  89. def buildResultClass(args):
  90. """Build a Result Class to use in the testcase execution"""
  91. class StampedResult(unittest.TextTestResult):
  92. """
  93. Custom TestResult that prints the time when a test starts. As toaster-auto
  94. can take a long time (ie a few hours) to run, timestamps help us understand
  95. what tests are taking a long time to execute.
  96. """
  97. def startTest(self, test):
  98. import time
  99. self.stream.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " - ")
  100. super(StampedResult, self).startTest(test)
  101. return StampedResult
  102. if __name__ == "__main__":
  103. try:
  104. ret = main()
  105. except:
  106. ret = 1
  107. import traceback
  108. traceback.print_exc()
  109. finally:
  110. if os.getenv('TOASTER_SUITE'):
  111. del os.environ['TOASTER_SUITE']
  112. sys.exit(ret)