1#! /usr/bin/env python
2#
3# This file is part of pySerial - Cross platform serial port support for Python
4# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
5#
6# SPDX-License-Identifier:    BSD-3-Clause
7"""\
8UnitTest runner. This one searches for all files named test_*.py and collects
9all test cases from these files. Finally it runs all tests and prints a
10summary.
11"""
12
13import unittest
14import sys
15import os
16
17# inject local copy to avoid testing the installed version instead of the one in the repo
18sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
19
20import serial  # noqa
21print("Patching sys.path to test local version. Testing Version: {}".format(serial.VERSION))
22
23PORT = 'loop://'
24if len(sys.argv) > 1:
25    PORT = sys.argv[1]
26
27# find files and the tests in them
28mainsuite = unittest.TestSuite()
29for modulename in [
30        os.path.splitext(x)[0]
31        for x in os.listdir(os.path.dirname(__file__) or '.')
32        if x != __file__ and x.startswith("test") and x.endswith(".py")
33]:
34    try:
35        module = __import__(modulename)
36    except ImportError:
37        print("skipping {}".format(modulename))
38    else:
39        module.PORT = PORT
40        testsuite = unittest.findTestCases(module)
41        print("found {} tests in {!r}".format(testsuite.countTestCases(), modulename))
42        mainsuite.addTest(testsuite)
43
44verbosity = 1
45if '-v' in sys.argv[1:]:
46    verbosity = 2
47    print('-' * 78)
48
49# run the collected tests
50testRunner = unittest.TextTestRunner(verbosity=verbosity)
51#~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity)
52result = testRunner.run(mainsuite)
53
54# set exit code accordingly to test results
55sys.exit(not result.wasSuccessful())
56