setup.py: Add test --only option

Allows running only tests that match the passed string.
This commit is contained in:
Cole Robinson 2013-07-13 16:32:46 -04:00
parent 5962fde3e6
commit d6f5c3a729

View File

@ -358,12 +358,15 @@ class configure(Command):
class TestBaseCommand(Command): class TestBaseCommand(Command):
user_options = [ user_options = [
('debug', 'd', 'Show debug output'), ('debug', 'd', 'Show debug output'),
('coverage', 'c', 'Show coverage report') ('coverage', 'c', 'Show coverage report'),
("only=", None,
"Run only testcases whose name contains the passed string"),
] ]
def initialize_options(self): def initialize_options(self):
self.debug = 0 self.debug = 0
self.coverage = 0 self.coverage = 0
self.only = None
self._testfiles = [] self._testfiles = []
self._dir = os.getcwd() self._dir = os.getcwd()
@ -378,7 +381,6 @@ class TestBaseCommand(Command):
except: except:
use_cov = False use_cov = False
if use_cov: if use_cov:
omit = ["/usr/*", "/*/tests/*"] omit = ["/usr/*", "/*/tests/*"]
cov = coverage.coverage(omit=omit) cov = coverage.coverage(omit=omit)
@ -388,15 +390,32 @@ class TestBaseCommand(Command):
import tests as testsmodule import tests as testsmodule
testsmodule.cov = cov testsmodule.cov = cov
tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
t = unittest.TextTestRunner(verbosity=1)
if hasattr(unittest, "installHandler"): if hasattr(unittest, "installHandler"):
try: try:
unittest.installHandler() unittest.installHandler()
except: except:
print "installHandler hack failed" print "installHandler hack failed"
tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
if self.only:
newtests = []
for suite1 in tests:
for suite2 in suite1:
for testcase in suite2:
if self.only in str(testcase):
newtests.append(testcase)
if not newtests:
print "--only didn't find any tests"
sys.exit(1)
tests = unittest.TestSuite(newtests)
print "Running only:"
for test in newtests:
print "%s" % test
print
t = unittest.TextTestRunner(verbosity=1)
try: try:
result = t.run(tests) result = t.run(tests)
except KeyboardInterrupt: except KeyboardInterrupt:
@ -416,10 +435,11 @@ class TestBaseCommand(Command):
class TestCommand(TestBaseCommand): class TestCommand(TestBaseCommand):
description = "Runs a quick unit test suite" description = "Runs a quick unit test suite"
user_options = TestBaseCommand.user_options + \ user_options = TestBaseCommand.user_options + [
[("testfile=", None, "Specific test file to run (e.g " ("testfile=", None, "Specific test file to run (e.g "
"validation, storage, ...)"), "validation, storage, ...)"),
("skipcli", None, "Skip CLI tests")] ("skipcli", None, "Skip CLI tests"),
]
def initialize_options(self): def initialize_options(self):
TestBaseCommand.initialize_options(self) TestBaseCommand.initialize_options(self)