mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Inspect test classes to avoid running setUpClass
and tearDownClass
If a test class has all it's tests marked to be skipped, we mark the class to be skipped so that `setUpClass` and `tearDownClass` doesn't even run.
This commit is contained in:
parent
1e16375702
commit
b7f99d180f
1 changed files with 39 additions and 1 deletions
|
@ -21,12 +21,13 @@
|
|||
"""
|
||||
# pylint: disable=unused-import,blacklisted-module,deprecated-method
|
||||
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
from unittest import TestCase as _TestCase
|
||||
from unittest import TestLoader as _TestLoader
|
||||
from unittest import TestResult
|
||||
|
@ -118,6 +119,43 @@ class TestSuite(_TestSuite):
|
|||
)
|
||||
delattr(previousClass, attr)
|
||||
|
||||
def _handleModuleFixture(self, test, result):
|
||||
# We override _handleModuleFixture so that we can inspect all test classes in the module.
|
||||
# If all tests in a test class are going to be skipped, mark the class to skip.
|
||||
# This avoids running setUpClass and tearDownClass unnecessarily
|
||||
currentModule = test.__class__.__module__
|
||||
try:
|
||||
module = sys.modules[currentModule]
|
||||
except KeyError:
|
||||
return
|
||||
for attr in dir(module):
|
||||
klass = getattr(module, attr)
|
||||
if not inspect.isclass(klass):
|
||||
# Not even a class? Carry on...
|
||||
continue
|
||||
if klass.__module__ != currentModule:
|
||||
# This class is not defined in the module being tested? Carry on...
|
||||
continue
|
||||
if not issubclass(klass, TestCase):
|
||||
# This class is not a subclass of TestCase, carry on
|
||||
continue
|
||||
|
||||
skip_klass = True
|
||||
test_functions = [name for name in dir(klass) if name.startswith("test_")]
|
||||
for name in test_functions:
|
||||
func = getattr(klass, name)
|
||||
if not isinstance(func, types.FunctionType):
|
||||
# Not even a function, carry on
|
||||
continue
|
||||
if getattr(func, "__unittest_skip__", False) is False:
|
||||
# At least one test is not going to be skipped.
|
||||
# Stop searching.
|
||||
skip_klass = False
|
||||
break
|
||||
if skip_klass is True:
|
||||
klass.__unittest_skip__ = True
|
||||
return super(TestSuite, self)._handleModuleFixture(test, result)
|
||||
|
||||
|
||||
class TestLoader(_TestLoader):
|
||||
# We're just subclassing to make sure tha tour TestSuite class is the one used
|
||||
|
|
Loading…
Add table
Reference in a new issue