From 119bed52fa77294ad810dc9cc29dcdb933b89c3f Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 28 Feb 2017 02:44:25 +0100 Subject: [PATCH] osmoutil: add pick_test() to pick unittest tests by name Change-Id: I92f90c334169f31920c63dd5c5ac8dac215065e6 --- osmopy/osmoutil.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/osmopy/osmoutil.py b/osmopy/osmoutil.py index 01f74cc..87203d5 100755 --- a/osmopy/osmoutil.py +++ b/osmopy/osmoutil.py @@ -19,6 +19,7 @@ import os import sys import importlib import time +import unittest """Run a command, with stdout and stderr directed to devnull""" @@ -69,3 +70,24 @@ def importappconf_or_quit(dirname, confname, p_set): else: print >> sys.stderr, "set osmoappdesc location with -p " sys.exit(1) + + +def pick_tests(suite, *name_snippets): + '''for unittest: Non-standard way of picking only selected tests to run, + by name. Kind of stupid of python unittest to not provide this feature + more easily.''' + + new_tests = [] + for t in suite._tests: + if isinstance(t, unittest.suite.TestSuite): + pick_tests(t, *name_snippets) + new_tests.append(t) + continue + + if not isinstance(t, unittest.TestCase): + new_tests.append(t) + continue + + if any([n.lower() in t._testMethodName.lower() for n in name_snippets]): + new_tests.append(t) + suite._tests = new_tests