summaryrefslogtreecommitdiff
path: root/run_tests.py
diff options
context:
space:
mode:
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>2014-08-20 21:55:36 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-09-01 02:47:15 +0200
commite1c944d723baaf562497314737711b8c41f04a7e (patch)
treeca89b365b4fb355991b80a7df911d274028fce87 /run_tests.py
parent83442b9849b9129d9d6ce95851471ea13ce7b0e0 (diff)
downloadslixmpp-e1c944d723baaf562497314737711b8c41f04a7e.tar.gz
slixmpp-e1c944d723baaf562497314737711b8c41f04a7e.tar.bz2
slixmpp-e1c944d723baaf562497314737711b8c41f04a7e.tar.xz
slixmpp-e1c944d723baaf562497314737711b8c41f04a7e.zip
Improve run_tests.py, allowing it to run only specific tests.
Diffstat (limited to 'run_tests.py')
-rwxr-xr-xrun_tests.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/run_tests.py b/run_tests.py
new file mode 100755
index 00000000..3b9df045
--- /dev/null
+++ b/run_tests.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+
+import sys
+import logging
+import unittest
+
+from argparse import ArgumentParser
+from distutils.core import Command
+from importlib import import_module
+from pathlib import Path
+
+
+def run_tests(filenames=None):
+ """
+ Find and run all tests in the tests/ directory.
+
+ Excludes live tests (tests/live_*).
+ """
+ if not filenames:
+ filenames = [i for i in Path('tests').glob('test_*')]
+ else:
+ filenames = [Path(i) for i in filenames]
+
+ modules = ['.'.join(test.parts[:-1] + (test.stem,)) for test in filenames]
+
+ suites = []
+ for filename in modules:
+ module = import_module(filename)
+ suites.append(module.suite)
+
+ tests = unittest.TestSuite(suites)
+ runner = unittest.TextTestRunner(verbosity=2)
+
+ # Disable logging output
+ logging.basicConfig(level=100)
+ logging.disable(100)
+
+ result = runner.run(tests)
+ return result
+
+
+# Add a 'test' command for setup.py
+
+class TestCommand(Command):
+
+ user_options = []
+
+ def initialize_options(self):
+ pass
+
+ def finalize_options(self):
+ pass
+
+ def run(self):
+ run_tests()
+
+
+if __name__ == '__main__':
+ parser = ArgumentParser(description='Run unit tests.')
+ parser.add_argument('tests', metavar='TEST', nargs='*', help='list of tests to run, or nothing to run them all')
+ args = parser.parse_args()
+
+ result = run_tests(args.tests)
+ print("<tests %s ran='%s' errors='%s' fails='%s' success='%s'/>" % (
+ "xmlns='http//andyet.net/protocol/tests'",
+ result.testsRun, len(result.errors),
+ len(result.failures), result.wasSuccessful()))
+
+ sys.exit(not result.wasSuccessful())