bpo-30540: regrtest: add --matchfile option (#1909) · python/cpython@ef8320c

@@ -159,9 +159,24 @@ def test_match(self):

159159

for opt in '-m', '--match':

160160

with self.subTest(opt=opt):

161161

ns = libregrtest._parse_args([opt, 'pattern'])

162-

self.assertEqual(ns.match_tests, 'pattern')

162+

self.assertEqual(ns.match_tests, ['pattern'])

163163

self.checkError([opt], 'expected one argument')

164164165+

ns = libregrtest._parse_args(['-m', 'pattern1',

166+

'-m', 'pattern2'])

167+

self.assertEqual(ns.match_tests, ['pattern1', 'pattern2'])

168+169+

self.addCleanup(support.unlink, support.TESTFN)

170+

with open(support.TESTFN, "w") as fp:

171+

print('matchfile1', file=fp)

172+

print('matchfile2', file=fp)

173+174+

filename = os.path.abspath(support.TESTFN)

175+

ns = libregrtest._parse_args(['-m', 'match',

176+

'--matchfile', filename])

177+

self.assertEqual(ns.match_tests,

178+

['match', 'matchfile1', 'matchfile2'])

179+165180

def test_failfast(self):

166181

for opt in '-G', '--failfast':

167182

with self.subTest(opt=opt):

@@ -275,7 +290,6 @@ def test_forever(self):

275290

ns = libregrtest._parse_args([opt])

276291

self.assertTrue(ns.forever)

277292278-279293

def test_unrecognized_argument(self):

280294

self.checkError(['--xxx'], 'usage:')

281295

@@ -457,7 +471,6 @@ def run_command(self, args, input=None, exitcode=0, **kw):

457471

self.fail(msg)

458472

return proc

459473460-461474

def run_python(self, args, **kw):

462475

args = [sys.executable, '-X', 'faulthandler', '-I', *args]

463476

proc = self.run_command(args, **kw)

@@ -823,6 +836,52 @@ def test_crashed(self):

823836

self.check_executed_tests(output, tests, failed=crash_test,

824837

randomize=True)

825838839+

def parse_methods(self, output):

840+

regex = re.compile("^(test[^ ]+).*ok$", flags=re.MULTILINE)

841+

return [match.group(1) for match in regex.finditer(output)]

842+843+

def test_matchfile(self):

844+

# Any code which causes a crash

845+

code = textwrap.dedent("""

846+

import unittest

847+848+

class Tests(unittest.TestCase):

849+

def test_method1(self):

850+

pass

851+

def test_method2(self):

852+

pass

853+

def test_method3(self):

854+

pass

855+

def test_method4(self):

856+

pass

857+

""")

858+

all_methods = ['test_method1', 'test_method2',

859+

'test_method3', 'test_method4']

860+

testname = self.create_test(code=code)

861+862+

# by default, all methods should be run

863+

output = self.run_tests("-v", testname)

864+

methods = self.parse_methods(output)

865+

self.assertEqual(methods, all_methods)

866+867+

# only run a subset

868+

filename = support.TESTFN

869+

self.addCleanup(support.unlink, filename)

870+871+

subset = [

872+

# only match the method name

873+

'test_method1',

874+

# match the full identifier

875+

'%s.Tests.test_method3' % testname]

876+

with open(filename, "w") as fp:

877+

for name in subset:

878+

print(name, file=fp)

879+880+

output = self.run_tests("-v", "--matchfile", filename, testname)

881+

methods = self.parse_methods(output)

882+

subset = ['test_method1', 'test_method3']

883+

self.assertEqual(methods, subset)

884+826885827886

if __name__ == '__main__':

828887

unittest.main()