diff options
author | Jonas Haag <jonas@lophus.org> | 2017-11-25 16:23:52 +0100 |
---|---|---|
committer | Antoine Pitrou <pitrou@free.fr> | 2017-11-25 16:23:52 +0100 |
commit | 5b48dc638b7405fd9bde4d854bf477dfeaaddf44 (patch) | |
tree | 2e3b44b9193cc1a0e08a6e1d65dd324e76fb3ee6 /Lib/unittest/test/test_program.py | |
parent | 8d9bb11d8fcbf10cc9b1eb0a647bcf3658a4e3dd (diff) | |
download | cpython-git-5b48dc638b7405fd9bde4d854bf477dfeaaddf44.tar.gz |
bpo-32071: Add unittest -k option (#4496)
* bpo-32071: Add unittest -k option
Diffstat (limited to 'Lib/unittest/test/test_program.py')
-rw-r--r-- | Lib/unittest/test/test_program.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py index 1cfc17959e..4a62ae1b11 100644 --- a/Lib/unittest/test/test_program.py +++ b/Lib/unittest/test/test_program.py @@ -2,6 +2,7 @@ import io import os import sys +import subprocess from test import support import unittest import unittest.test @@ -409,6 +410,33 @@ class TestCommandLineArgs(unittest.TestCase): # for invalid filenames should we raise a useful error rather than # leaving the current error message (import of filename fails) in place? + def testParseArgsSelectedTestNames(self): + program = self.program + argv = ['progname', '-k', 'foo', '-k', 'bar', '-k', '*pat*'] + + program.createTests = lambda: None + program.parseArgs(argv) + + self.assertEqual(program.testNamePatterns, ['*foo*', '*bar*', '*pat*']) + + def testSelectedTestNamesFunctionalTest(self): + def run_unittest(args): + p = subprocess.Popen([sys.executable, '-m', 'unittest'] + args, + stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__)) + with p: + _, stderr = p.communicate() + return stderr.decode() + + t = '_test_warnings' + self.assertIn('Ran 7 tests', run_unittest([t])) + self.assertIn('Ran 7 tests', run_unittest(['-k', 'TestWarnings', t])) + self.assertIn('Ran 7 tests', run_unittest(['discover', '-p', '*_test*', '-k', 'TestWarnings'])) + self.assertIn('Ran 2 tests', run_unittest(['-k', 'f', t])) + self.assertIn('Ran 7 tests', run_unittest(['-k', 't', t])) + self.assertIn('Ran 3 tests', run_unittest(['-k', '*t', t])) + self.assertIn('Ran 7 tests', run_unittest(['-k', '*test_warnings.*Warning*', t])) + self.assertIn('Ran 1 test', run_unittest(['-k', '*test_warnings.*warning*', t])) + if __name__ == '__main__': unittest.main() |