Skip to content

Commit

Permalink
Revert "Added --test-pattern option"
Browse files Browse the repository at this point in the history
  • Loading branch information
CleanCut committed Apr 7, 2015
1 parent 7c9f0a6 commit ceb18e9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 96 deletions.
6 changes: 1 addition & 5 deletions green/cmdline.py
Expand Up @@ -62,11 +62,7 @@ def main(testing=False, coverage_testing=False):
if testing:
test_suite = None
else:
test_suite = loadTargets(
args.targets,
file_pattern = args.file_pattern,
test_pattern = args.test_pattern,
)
test_suite = loadTargets(args.targets, file_pattern = args.pattern)

# We didn't even load 0 tests...
if not test_suite:
Expand Down
9 changes: 3 additions & 6 deletions green/config.py
Expand Up @@ -52,8 +52,7 @@
verbose = 1,
failfast = False,
config = None, # Not in configs
file_pattern = 'test*.py',
test_pattern = None,
pattern = 'test*.py',
run_coverage = False,
omit = None,
completion_file = False,
Expand Down Expand Up @@ -205,12 +204,10 @@ def parseArguments(): # pragma: no cover
metavar='FILE', help="Use this config file instead of the one pointed "
"to by environment variable GREEN_CONFIG or the default ~/.green",
default=argparse.SUPPRESS))
store_opt(other_args.add_argument('-p', '--file-pattern', action='store',
store_opt(other_args.add_argument('-p', '--pattern', action='store',
metavar='PATTERN',
help="Pattern to match test files. Default is test*.py",
default=argparse.SUPPRESS))
store_opt(other_args.add_argument('-n', '--test-pattern', action='store',
metavar='PATTERN', help="Pattern to match test method names."))

cov_args = parser.add_argument_group(
"Coverage Options ({})".format(coverage_version))
Expand Down Expand Up @@ -352,7 +349,7 @@ def mergeConfig(args, testing=False, coverage_testing=False): # pragma: no cover
config_getter = config.getboolean
elif name in ['subprocesses', 'debug', 'verbose']:
config_getter = config.getint
elif name in ['omit', 'warnings', 'file_pattern', 'test_pattern']:
elif name in ['omit', 'warnings', 'pattern']:
config_getter = config.get
elif name in ['targets', 'help', 'config']:
pass # Some options only make sense coming on the command-line.
Expand Down
32 changes: 4 additions & 28 deletions green/loader.py
Expand Up @@ -239,7 +239,7 @@ def discover(current_path, file_pattern='test*.py'):
return ((suite.countTestCases() and suite) or None)


def loadTargets(targets, file_pattern='test*.py', test_pattern=None):
def loadTargets(targets, file_pattern='test*.py'):
# If a string was passed in, put it into a list.
if type(targets) != list:
targets = [targets]
Expand All @@ -261,36 +261,12 @@ def loadTargets(targets, file_pattern='test*.py', test_pattern=None):
debug("Found {} test{} for target '{}'".format(
num_tests, '' if (num_tests == 1) else 's', target))

if not suites:
return None

suite = GreenTestSuite(suites)
if test_pattern is None:
return suite
else:
name_regex = re.compile(test_pattern)
return name_filtered(suite, name_regex)

def name_filtered(suite, name_regex):
"""
Takes a GreenTestSuite and a compiled regex. Creates a new GreenTestSuite
consisting only of test methods having names that satisfy the regex.
Returns None if the new suite is empty.
"""
new_suite = GreenTestSuite()
for x in suite:
if isinstance(x, GreenTestSuite):
sub_suite = name_filtered(x, name_regex)
if sub_suite and sub_suite.countTestCases():
new_suite.addTest(sub_suite)
else:
if name_regex is None or name_regex.search(x._testMethodName):
new_suite.addTest(x)
if new_suite.countTestCases():
return new_suite
if suites:
return GreenTestSuite(suites)
else:
return None


def loadTarget(target, file_pattern='test*.py'):
"""
"""
Expand Down
58 changes: 1 addition & 57 deletions green/test/test_loader.py
Expand Up @@ -625,6 +625,7 @@ def test_explicit_filename_error(self):
tests = loader.loadTargets('mod_with_import_error.py')
self.assertEqual(tests.countTestCases(), 1)


def test_file_pattern(self):
"Specifying a file pattern causes only matching files to be loaded"
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
Expand Down Expand Up @@ -667,60 +668,3 @@ def testPasses(self):
tests = loader.loadTargets(pkg, file_pattern='*_tests.py')
self.assertEqual(tests.countTestCases(), 2)

def test_test_pattern(self):
"Specifying a test pattern causes only matching tests to be loaded"
sub_tmpdir = tempfile.mkdtemp(dir=self.tmpdir)
# pkg/__init__.py
fh = open(os.path.join(sub_tmpdir, '__init__.py'), 'w')
fh.write('\n')
fh.close()
# pkg/test/target1_tests.py
fh = open(os.path.join(sub_tmpdir, 'test_target1.py'), 'w')
fh.write("""
import unittest
class A(unittest.TestCase):
def testFoo(self):
pass
def testFooBar(self):
pass
def testBar(self):
pass""")
fh.close()
# pkg/test/target2_tests.py
fh = open(os.path.join(sub_tmpdir, 'test_target2.py'), 'w')
fh.write("""
import unittest
class B(unittest.TestCase):
def testFoolish(self):
pass
def testBarber(self):
pass""")
fh.close()
# pkg/test/test_target999.py: NOT a match.
fh = open(os.path.join(sub_tmpdir, 'test_target3.py'), 'w')
fh.write("""
import unittest
class C(unittest.TestCase):
def testPasses(self):
pass""")
fh.close()
# Load tests using various test patterns.
os.chdir(self.tmpdir)
pkg = os.path.basename(sub_tmpdir)
targets = [
pkg + '.' + 'test_target1',
pkg + '.' + 'test_target2',
pkg + '.' + 'test_target3',
]
scenarios = {
None: 6,
'Foo': 3,
'FooB': 1,
'Bar': 3,
'Bar$': 2,
'Foolish': 1,
}
for test_pattern, exp_n_tests in scenarios.items():
tests = loader.loadTargets(pkg, test_pattern=test_pattern)
self.assertEqual(tests.countTestCases(), exp_n_tests)

0 comments on commit ceb18e9

Please sign in to comment.