-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-32071: Add unittest -k option #4496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import os | ||
import sys | ||
import subprocess | ||
from test import support | ||
import unittest | ||
import unittest.test | ||
|
@@ -409,6 +410,33 @@ def testParseArgsAbsolutePathsThatCannotBeConverted(self): | |
# 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for adding these tests. Do you think you could also create a small functional test? There is an example of such a test (using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a functional test but I'm not sure if that's the right place to put it. |
||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Added the ``-k`` command-line option to ``python -m unittest`` to run only | ||
tests that match the given pattern(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, why was this change necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactoring was necessary because I didn't want to repeat the code that sets
loader.testNamePatterns
from the CLI options. That code has to be executed BEFOREloader.discover
. Besides, I believe it makes sense to have a single place whereself.test
is set.