From d6f5c3a7291f775f353e851c718e54e6eeee2fad Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sat, 13 Jul 2013 16:32:46 -0400 Subject: [PATCH] setup.py: Add test --only option Allows running only tests that match the passed string. --- setup.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index c5b8818be..f5fd58708 100755 --- a/setup.py +++ b/setup.py @@ -358,12 +358,15 @@ class configure(Command): class TestBaseCommand(Command): user_options = [ ('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): self.debug = 0 self.coverage = 0 + self.only = None self._testfiles = [] self._dir = os.getcwd() @@ -378,7 +381,6 @@ class TestBaseCommand(Command): except: use_cov = False - if use_cov: omit = ["/usr/*", "/*/tests/*"] cov = coverage.coverage(omit=omit) @@ -388,15 +390,32 @@ class TestBaseCommand(Command): import tests as testsmodule testsmodule.cov = cov - tests = unittest.TestLoader().loadTestsFromNames(self._testfiles) - t = unittest.TextTestRunner(verbosity=1) - if hasattr(unittest, "installHandler"): try: unittest.installHandler() except: 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: result = t.run(tests) except KeyboardInterrupt: @@ -416,10 +435,11 @@ class TestBaseCommand(Command): class TestCommand(TestBaseCommand): description = "Runs a quick unit test suite" - user_options = TestBaseCommand.user_options + \ - [("testfile=", None, "Specific test file to run (e.g " - "validation, storage, ...)"), - ("skipcli", None, "Skip CLI tests")] + user_options = TestBaseCommand.user_options + [ + ("testfile=", None, "Specific test file to run (e.g " + "validation, storage, ...)"), + ("skipcli", None, "Skip CLI tests"), + ] def initialize_options(self): TestBaseCommand.initialize_options(self)