From e1c944d723baaf562497314737711b8c41f04a7e Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 20 Aug 2014 21:55:36 +0200 Subject: Improve run_tests.py, allowing it to run only specific tests. --- run_tests.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- testall.py | 64 ------------------------------------------------------- 3 files changed, 70 insertions(+), 65 deletions(-) create mode 100755 run_tests.py delete mode 100755 testall.py 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("" % ( + "xmlns='http//andyet.net/protocol/tests'", + result.testsRun, len(result.errors), + len(result.failures), result.wasSuccessful())) + + sys.exit(not result.wasSuccessful()) diff --git a/setup.py b/setup.py index 5221fbdc..9e497059 100755 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ try: except ImportError: from distutils.core import setup -from testall import TestCommand +from run_tests import TestCommand from slixmpp.version import __version__ VERSION = __version__ diff --git a/testall.py b/testall.py deleted file mode 100755 index 462550b7..00000000 --- a/testall.py +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env python3 - -import sys - -import os -import logging -import unittest -import distutils.core - -from glob import glob -from os.path import splitext, basename, join as pjoin - - -def run_tests(): - """ - Find and run all tests in the tests/ directory. - - Excludes live tests (tests/live_*). - """ - testfiles = ['tests.test_overall'] - exclude = ['__init__.py', 'test_overall.py'] - for t in glob(pjoin('tests', '*.py')): - if True not in [t.endswith(ex) for ex in exclude]: - if basename(t).startswith('test_'): - testfiles.append('tests.%s' % splitext(basename(t))[0]) - - suites = [] - for file in testfiles: - __import__(file) - suites.append(sys.modules[file].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(distutils.core.Command): - - user_options = [ ] - - def initialize_options(self): - self._dir = os.getcwd() - - def finalize_options(self): - pass - - def run(self): - run_tests() - - -if __name__ == '__main__': - result = run_tests() - print("" % ( - "xmlns='http//andyet.net/protocol/tests'", - result.testsRun, len(result.errors), - len(result.failures), result.wasSuccessful())) -- cgit v1.2.3