Refactor runtests to allow dotted path module discovery.

In addition to traditional file system path test discovery it is
useful to run a test by stating it's dotted python path. For example,
this will only run the cmdmod integration tests from the
module suite::

        $ runtests.py -n tests.integration.modules.cmdmod
This commit is contained in:
Dan Colish 2012-05-29 19:56:47 -07:00
parent f3e8d68cee
commit f5c8e242e4
8 changed files with 47 additions and 82 deletions

0
tests/__init__.py Normal file
View file

View file

View file

View file

View file

View file

View file

@ -21,6 +21,28 @@ TEST_DIR = os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
PNUM = 50
def run_suite(opts, path, display_name, suffix='[!_]*.py'):
loader = saltunittest.TestLoader()
if opts.name:
tests = loader.loadTestsFromName(opts.name)
else:
tests = loader.discover(path, suffix, TEST_DIR)
print('~' * PNUM)
print('Starting {0} Tests'.format(display_name))
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(tests)
else:
runner = saltunittest.TextTestRunner(
verbosity=opts.verbosity).run(tests)
return runner.wasSuccessful()
def run_integration_suite(opts, suite_folder, display_name):
path = os.path.join(TEST_DIR, 'integration', suite_folder)
return run_suite(opts, path, display_name)
def run_integration_tests(opts):
'''
Execute the integration tests suite
@ -29,78 +51,23 @@ def run_integration_tests(opts):
print('Setting up Salt daemons to execute tests')
print('~' * PNUM)
status = []
if not any([opts.client, opts.module, opts.runner,
opts.shell, opts.state, opts.name]):
return status
with TestDaemon():
if opts.name:
results = run_suite(opts, '', opts.name)
status.append(results)
if opts.module:
moduleloader = saltunittest.TestLoader()
moduletests = moduleloader.discover(
os.path.join(TEST_DIR, 'integration', 'modules'),
'*.py'
)
print('~' * PNUM)
print('Starting Module Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(moduletests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(moduletests)
status.append(runner.wasSuccessful())
status.append(run_integration_suite(opts, 'modules', 'Module'))
if opts.state:
stateloader = saltunittest.TestLoader()
statetests = stateloader.discover(
os.path.join(TEST_DIR, 'integration', 'states'),
'*.py'
)
print('~' * PNUM)
print('Starting State Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(statetests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(statetests)
status.append(runner.wasSuccessful())
status.append(run_integration_suite(opts, 'states', 'State'))
if opts.client:
clientloader = saltunittest.TestLoader()
clienttests = clientloader.discover(
os.path.join(TEST_DIR, 'integration', 'client'),
'*.py'
)
print('~' * PNUM)
print('Starting Client Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(clienttests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(clienttests)
status.append(runner.wasSuccessful())
status.append(run_integration_suite(opts, 'client', 'Client'))
if opts.shell:
shellloader = saltunittest.TestLoader()
shelltests = shellloader.discover(
os.path.join(TEST_DIR, 'integration', 'shell'),
'*.py'
)
print('~' * PNUM)
print('Starting Shell Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(shelltests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(shelltests)
status.append(runner.wasSuccessful())
status.append(run_integration_suite(opts, 'shell', 'Shell'))
if opts.runner:
runnerloader = saltunittest.TestLoader()
runnertests = runnerloader.discover(
os.path.join(TEST_DIR, 'integration', 'runners'),
'*.py'
)
print('~' * PNUM)
print('Starting Runner Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(runnertests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(runnertests)
status.append(runner.wasSuccessful())
status.append(run_integration_suite(opts, 'runners', 'Runner'))
return status
@ -111,16 +78,9 @@ def run_unit_tests(opts):
if not opts.unit:
return [True]
status = []
loader = saltunittest.TestLoader()
tests = loader.discover(os.path.join(TEST_DIR, 'unit'), '*_test.py')
print('~' * PNUM)
print('Starting Unit Tests')
print('~' * PNUM)
if opts.xmlout:
runner = xmlrunner.XMLTestRunner(output='test-reports').run(tests)
else:
runner = saltunittest.TextTestRunner(verbosity=opts.verbosity).run(tests)
status.append(runner.wasSuccessful())
results = run_suite(
opts, os.path.join(TEST_DIR, 'unit'), 'Unit', '*_test.py')
status.append(results)
return status
@ -175,18 +135,23 @@ def parse_opts():
default=1,
action='count',
help='Verbose test runner output')
parser.add_option('-x',
'--xml',
dest='xmlout',
default=False,
action='store_true',
help='Verbose test runner output')
help='XML test runner output')
parser.add_option('-n',
'--name',
dest='name',
default='',
help='Specific test name to run')
options, args = parser.parse_args()
if all((not options.module, not options.client,
not options.shell, not options.unit,
not options.state, not options.runner)):
options, _ = parser.parse_args()
if not any((options.module, options.client,
options.shell, options.unit,
options.state, options.runner,
options.name)):
options.module = True
options.client = True
options.shell = True

0
tests/unit/__init__.py Normal file
View file