cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Chris Liechti | 7c032f1 | 2015-10-27 23:02:00 +0100 | [diff] [blame] | 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 |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 7 | """\ |
| 8 | UnitTest runner. This one searches for all files named test_*.py and collects |
| 9 | all test cases from these files. Finally it runs all tests and prints a |
| 10 | summary. |
| 11 | """ |
| 12 | |
| 13 | import unittest |
| 14 | import sys |
| 15 | import os |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 16 | |
cliechti | 82282e0 | 2009-08-07 18:49:40 +0000 | [diff] [blame] | 17 | # inject local copy to avoid testing the installed version instead of the |
Chris Liechti | 418a222 | 2015-08-03 17:20:12 +0200 | [diff] [blame] | 18 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
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() |
Chris Liechti | 418a222 | 2015-08-03 17:20:12 +0200 | [diff] [blame] | 29 | for modulename in [ |
| 30 | os.path.splitext(x)[0] |
Chris Liechti | d73344d | 2015-08-06 17:52:51 +0200 | [diff] [blame] | 31 | for x in os.listdir(os.path.dirname(__file__) or '.') |
cliechti | 1f4abd7 | 2010-07-01 12:56:38 +0000 | [diff] [blame] | 32 | if x != __file__ and x.startswith("test") and x.endswith(".py") |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 33 | ]: |
| 34 | try: |
| 35 | module = __import__(modulename) |
| 36 | except ImportError: |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 37 | print("skipping %s" % (modulename,)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 38 | else: |
| 39 | module.PORT = PORT |
| 40 | testsuite = unittest.findTestCases(module) |
cliechti | e30868d | 2013-10-16 15:35:11 +0000 | [diff] [blame] | 41 | print("found %s tests in %r" % (testsuite.countTestCases(), modulename)) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 42 | mainsuite.addTest(testsuite) |
| 43 | |
| 44 | verbosity = 1 |
| 45 | if '-v' in sys.argv[1:]: |
| 46 | verbosity = 2 |
Chris Liechti | 8e37ba9 | 2016-02-03 01:22:22 +0100 | [diff] [blame^] | 47 | print('-' * 78) |
cliechti | 5eb4b89 | 2009-08-07 18:21:58 +0000 | [diff] [blame] | 48 | |
| 49 | # run the collected tests |
| 50 | testRunner = unittest.TextTestRunner(verbosity=verbosity) |
| 51 | #~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity) |
| 52 | result = testRunner.run(mainsuite) |
| 53 | |
| 54 | # set exit code accordingly to test results |
| 55 | sys.exit(not result.wasSuccessful()) |