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 |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 12 | |
cliechti | 82282e0 | 2009-08-07 18:49:40 +0000 | [diff] [blame] | 13 | # inject local copy to avoid testing the installed version instead of the |
Chris Liechti | 418a222 | 2015-08-03 17:20:12 +0200 | [diff] [blame] | 14 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
cliechti | 82282e0 | 2009-08-07 18:49:40 +0000 | [diff] [blame] | 15 | |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 16 | import serial |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 17 | print("Patching sys.path to test local version. Testing Version: %s" % (serial.VERSION,)) |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 18 | |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 19 | PORT = 'loop://' |
cliechti | 59dff67 | 2010-01-02 03:03:29 +0000 | [diff] [blame] | 20 | if len(sys.argv) > 1: |
| 21 | PORT = sys.argv[1] |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 22 | |
| 23 | # find files and the tests in them |
| 24 | mainsuite = unittest.TestSuite() |
Chris Liechti | 418a222 | 2015-08-03 17:20:12 +0200 | [diff] [blame] | 25 | for modulename in [ |
| 26 | os.path.splitext(x)[0] |
Chris Liechti | d73344d | 2015-08-06 17:52:51 +0200 | [diff] [blame] | 27 | for x in os.listdir(os.path.dirname(__file__) or '.') |
cliechti | 1f4abd7 | 2010-07-01 12:56:38 +0000 | [diff] [blame] | 28 | if x != __file__ and x.startswith("test") and x.endswith(".py") |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 29 | ]: |
| 30 | try: |
| 31 | module = __import__(modulename) |
| 32 | except ImportError: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 33 | print("skipping %s" % (modulename,)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 34 | else: |
| 35 | module.PORT = PORT |
| 36 | testsuite = unittest.findTestCases(module) |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 37 | print("found %s tests in %r" % (testsuite.countTestCases(), modulename)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 38 | mainsuite.addTest(testsuite) |
| 39 | |
| 40 | verbosity = 1 |
| 41 | if '-v' in sys.argv[1:]: |
| 42 | verbosity = 2 |
Chris Liechti | d73344d | 2015-08-06 17:52:51 +0200 | [diff] [blame] | 43 | print('-'*78) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 44 | |
| 45 | # run the collected tests |
| 46 | testRunner = unittest.TextTestRunner(verbosity=verbosity) |
| 47 | #~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity) |
| 48 | result = testRunner.run(mainsuite) |
| 49 | |
| 50 | # set exit code accordingly to test results |
| 51 | sys.exit(not result.wasSuccessful()) |