blob: e7f115d964a21c7f3b602b3aded963003ff82ddc [file] [log] [blame]
cliechti5eb4b892009-08-07 18:21:58 +00001#! /usr/bin/env python
2
3"""\
4UnitTest runner. This one searches for all files named test_*.py and collects
5all test cases from these files. Finally it runs all tests and prints a
6summary.
7"""
8
9import unittest
10import sys
11import os
12import time
13
cliechti82282e02009-08-07 18:49:40 +000014# inject local copy to avoid testing the installed version instead of the
cliechtie30868d2013-10-16 15:35:11 +000015# 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).
17if sys.version_info < (3, 0):
18 sys.path.insert(0, '..')
cliechti82282e02009-08-07 18:49:40 +000019
cliechti59dff672010-01-02 03:03:29 +000020import serial
cliechtie30868d2013-10-16 15:35:11 +000021print("Patching sys.path to test local version. Testing Version: %s" % (serial.VERSION,))
cliechti59dff672010-01-02 03:03:29 +000022
cliechti5eb4b892009-08-07 18:21:58 +000023PORT = 'loop://'
cliechti59dff672010-01-02 03:03:29 +000024if len(sys.argv) > 1:
25 PORT = sys.argv[1]
cliechti5eb4b892009-08-07 18:21:58 +000026
27# find files and the tests in them
28mainsuite = unittest.TestSuite()
29for modulename in [os.path.splitext(x)[0]
30 for x in os.listdir('.')
cliechti1f4abd72010-07-01 12:56:38 +000031 if x != __file__ and x.startswith("test") and x.endswith(".py")
cliechti5eb4b892009-08-07 18:21:58 +000032]:
33 try:
34 module = __import__(modulename)
35 except ImportError:
cliechtie30868d2013-10-16 15:35:11 +000036 print("skipping %s" % (modulename,))
cliechti5eb4b892009-08-07 18:21:58 +000037 else:
38 module.PORT = PORT
39 testsuite = unittest.findTestCases(module)
cliechtie30868d2013-10-16 15:35:11 +000040 print("found %s tests in %r" % (testsuite.countTestCases(), modulename))
cliechti5eb4b892009-08-07 18:21:58 +000041 mainsuite.addTest(testsuite)
42
43verbosity = 1
44if '-v' in sys.argv[1:]:
45 verbosity = 2
46
47# run the collected tests
48testRunner = unittest.TextTestRunner(verbosity=verbosity)
49#~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity)
50result = testRunner.run(mainsuite)
51
52# set exit code accordingly to test results
53sys.exit(not result.wasSuccessful())