cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """\ |
| 4 | UnitTest runner. This one searches for all files named test_*.py and collects |
| 5 | all test cases from these files. Finally it runs all tests and prints a |
| 6 | summary. |
| 7 | """ |
| 8 | |
| 9 | import unittest |
| 10 | import sys |
| 11 | import os |
| 12 | import time |
| 13 | |
cliechti | 82282e0 | 2009-08-07 18:49:40 +0000 | [diff] [blame] | 14 | # inject local copy to avoid testing the installed version instead of the |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 15 | # working copy (only for 2.x as the sources would need to be translated with |
| 16 | # 2to3 for Python 3, use installed module instead for Python 3). |
| 17 | if sys.version_info < (3, 0): |
| 18 | sys.path.insert(0, '..') |
cliechti | 82282e0 | 2009-08-07 18:49:40 +0000 | [diff] [blame] | 19 | |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 20 | import serial |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 21 | print("Patching sys.path to test local version. Testing Version: %s" % (serial.VERSION,)) |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 22 | |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 23 | PORT = 'loop://' |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 24 | if len(sys.argv) > 1: |
| 25 | PORT = sys.argv[1] |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 26 | |
| 27 | # find files and the tests in them |
| 28 | mainsuite = unittest.TestSuite() |
| 29 | for modulename in [os.path.splitext(x)[0] |
| 30 | for x in os.listdir('.') |
cliechti | 1f4abd7 | 2010-07-01 12:56:38 +0000 | [diff] [blame] | 31 | if x != __file__ and x.startswith("test") and x.endswith(".py") |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 32 | ]: |
| 33 | try: |
| 34 | module = __import__(modulename) |
| 35 | except ImportError: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 36 | print("skipping %s" % (modulename,)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 37 | else: |
| 38 | module.PORT = PORT |
| 39 | testsuite = unittest.findTestCases(module) |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 40 | print("found %s tests in %r" % (testsuite.countTestCases(), modulename)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 41 | mainsuite.addTest(testsuite) |
| 42 | |
| 43 | verbosity = 1 |
| 44 | if '-v' in sys.argv[1:]: |
| 45 | verbosity = 2 |
| 46 | |
| 47 | # run the collected tests |
| 48 | testRunner = unittest.TextTestRunner(verbosity=verbosity) |
| 49 | #~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity) |
| 50 | result = testRunner.run(mainsuite) |
| 51 | |
| 52 | # set exit code accordingly to test results |
| 53 | sys.exit(not result.wasSuccessful()) |