blob: 39114aff3fda2a8e744e37012e317ecfe2846ea5 [file] [log] [blame]
cliechti5eb4b892009-08-07 18:21:58 +00001#! /usr/bin/env python
Chris Liechti7c032f12015-10-27 23:02:00 +01002#
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
cliechti5eb4b892009-08-07 18:21:58 +00007"""\
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
cliechti5eb4b892009-08-07 18:21:58 +000016
cliechti82282e02009-08-07 18:49:40 +000017# inject local copy to avoid testing the installed version instead of the
Chris Liechti418a2222015-08-03 17:20:12 +020018sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
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()
Chris Liechti418a2222015-08-03 17:20:12 +020029for modulename in [
30 os.path.splitext(x)[0]
Chris Liechtid73344d2015-08-06 17:52:51 +020031 for x in os.listdir(os.path.dirname(__file__) or '.')
cliechti1f4abd72010-07-01 12:56:38 +000032 if x != __file__ and x.startswith("test") and x.endswith(".py")
cliechti5eb4b892009-08-07 18:21:58 +000033]:
34 try:
35 module = __import__(modulename)
36 except ImportError:
cliechtie30868d2013-10-16 15:35:11 +000037 print("skipping %s" % (modulename,))
cliechti5eb4b892009-08-07 18:21:58 +000038 else:
39 module.PORT = PORT
40 testsuite = unittest.findTestCases(module)
cliechtie30868d2013-10-16 15:35:11 +000041 print("found %s tests in %r" % (testsuite.countTestCases(), modulename))
cliechti5eb4b892009-08-07 18:21:58 +000042 mainsuite.addTest(testsuite)
43
44verbosity = 1
45if '-v' in sys.argv[1:]:
46 verbosity = 2
Chris Liechtid73344d2015-08-06 17:52:51 +020047 print('-'*78)
cliechti5eb4b892009-08-07 18:21:58 +000048
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())