Improve run_tests.py, allowing it to run only specific tests.

This commit is contained in:
Emmanuel Gil Peyrot 2014-08-20 21:55:36 +02:00 committed by Florent Le Coz
parent 83442b9849
commit e1c944d723
2 changed files with 25 additions and 20 deletions

View file

@ -1,33 +1,32 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import os
import logging import logging
import unittest import unittest
import distutils.core
from glob import glob from argparse import ArgumentParser
from os.path import splitext, basename, join as pjoin from distutils.core import Command
from importlib import import_module
from pathlib import Path
def run_tests(): def run_tests(filenames=None):
""" """
Find and run all tests in the tests/ directory. Find and run all tests in the tests/ directory.
Excludes live tests (tests/live_*). Excludes live tests (tests/live_*).
""" """
testfiles = ['tests.test_overall'] if not filenames:
exclude = ['__init__.py', 'test_overall.py'] filenames = [i for i in Path('tests').glob('test_*')]
for t in glob(pjoin('tests', '*.py')): else:
if True not in [t.endswith(ex) for ex in exclude]: filenames = [Path(i) for i in filenames]
if basename(t).startswith('test_'):
testfiles.append('tests.%s' % splitext(basename(t))[0]) modules = ['.'.join(test.parts[:-1] + (test.stem,)) for test in filenames]
suites = [] suites = []
for file in testfiles: for filename in modules:
__import__(file) module = import_module(filename)
suites.append(sys.modules[file].suite) suites.append(module.suite)
tests = unittest.TestSuite(suites) tests = unittest.TestSuite(suites)
runner = unittest.TextTestRunner(verbosity=2) runner = unittest.TextTestRunner(verbosity=2)
@ -42,12 +41,12 @@ def run_tests():
# Add a 'test' command for setup.py # Add a 'test' command for setup.py
class TestCommand(distutils.core.Command): class TestCommand(Command):
user_options = [] user_options = []
def initialize_options(self): def initialize_options(self):
self._dir = os.getcwd() pass
def finalize_options(self): def finalize_options(self):
pass pass
@ -57,8 +56,14 @@ class TestCommand(distutils.core.Command):
if __name__ == '__main__': if __name__ == '__main__':
result = run_tests() 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'/>" % ( print("<tests %s ran='%s' errors='%s' fails='%s' success='%s'/>" % (
"xmlns='http//andyet.net/protocol/tests'", "xmlns='http//andyet.net/protocol/tests'",
result.testsRun, len(result.errors), result.testsRun, len(result.errors),
len(result.failures), result.wasSuccessful())) len(result.failures), result.wasSuccessful()))
sys.exit(not result.wasSuccessful())

View file

@ -13,7 +13,7 @@ try:
except ImportError: except ImportError:
from distutils.core import setup from distutils.core import setup
from testall import TestCommand from run_tests import TestCommand
from slixmpp.version import __version__ from slixmpp.version import __version__
VERSION = __version__ VERSION = __version__