Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5f99ced0
Commit
5f99ced0
authored
Mar 12, 2012
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor unittest command line handling to always use optparse
parent
518cf94a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
59 deletions
+32
-59
Lib/unittest/main.py
Lib/unittest/main.py
+32
-42
Lib/unittest/test/test_program.py
Lib/unittest/test/test_program.py
+0
-17
No files found.
Lib/unittest/main.py
View file @
5f99ced0
"""Unittest main program"""
import
sys
import
optparse
import
os
from
.
import
loader
,
runner
...
...
@@ -76,6 +77,7 @@ def _convert_name(name):
def
_convert_names
(
names
):
return
[
_convert_name
(
name
)
for
name
in
names
]
class
TestProgram
(
object
):
"""A command-line program that runs a set of tests; this is primarily
for making test modules conveniently executable.
...
...
@@ -142,33 +144,9 @@ class TestProgram(object):
self
.
_do_discovery
(
argv
[
2
:])
return
import
getopt
long_opts
=
[
'help'
,
'verbose'
,
'quiet'
,
'failfast'
,
'catch'
,
'buffer'
]
try
:
options
,
args
=
getopt
.
getopt
(
argv
[
1
:],
'hHvqfcb'
,
long_opts
)
except
getopt
.
error
as
msg
:
self
.
usageExit
(
msg
)
return
for
opt
,
value
in
options
:
if
opt
in
(
'-h'
,
'-H'
,
'--help'
):
self
.
usageExit
()
if
opt
in
(
'-q'
,
'--quiet'
):
self
.
verbosity
=
0
if
opt
in
(
'-v'
,
'--verbose'
):
self
.
verbosity
=
2
if
opt
in
(
'-f'
,
'--failfast'
):
if
self
.
failfast
is
None
:
self
.
failfast
=
True
# Should this raise an exception if -f is not valid?
if
opt
in
(
'-c'
,
'--catch'
):
if
self
.
catchbreak
is
None
:
self
.
catchbreak
=
True
# Should this raise an exception if -c is not valid?
if
opt
in
(
'-b'
,
'--buffer'
):
if
self
.
buffer
is
None
:
self
.
buffer
=
True
# Should this raise an exception if -b is not valid?
parser
=
self
.
_getOptParser
()
options
,
args
=
parser
.
parse_args
(
argv
[
1
:])
self
.
_setAttributesFromOptions
(
options
)
if
len
(
args
)
==
0
and
self
.
module
is
None
:
# this allows "python -m unittest -v" to still work for
...
...
@@ -196,14 +174,14 @@ class TestProgram(object):
self
.
test
=
self
.
testLoader
.
loadTestsFromNames
(
self
.
testNames
,
self
.
module
)
def
_do_discovery
(
self
,
argv
,
Loader
=
loader
.
TestLoader
):
# handle command line args for test discovery
self
.
progName
=
'%s discover'
%
self
.
progName
import
optparse
def
_getOptParser
(
self
):
parser
=
optparse
.
OptionParser
()
parser
.
prog
=
self
.
progName
parser
.
add_option
(
'-v'
,
'--verbose'
,
dest
=
'verbose'
,
default
=
False
,
help
=
'Verbose output'
,
action
=
'store_true'
)
parser
.
add_option
(
'-q'
,
'--quiet'
,
dest
=
'quiet'
,
default
=
False
,
help
=
'Quiet output'
,
action
=
'store_true'
)
if
self
.
failfast
!=
False
:
parser
.
add_option
(
'-f'
,
'--failfast'
,
dest
=
'failfast'
,
default
=
False
,
help
=
'Stop on first fail or error'
,
...
...
@@ -216,6 +194,28 @@ class TestProgram(object):
parser
.
add_option
(
'-b'
,
'--buffer'
,
dest
=
'buffer'
,
default
=
False
,
help
=
'Buffer stdout and stderr during tests'
,
action
=
'store_true'
)
return
parser
def
_setAttributesFromOptions
(
self
,
options
):
# only set options from the parsing here
# if they weren't set explicitly in the constructor
if
self
.
failfast
is
None
:
self
.
failfast
=
options
.
failfast
if
self
.
catchbreak
is
None
:
self
.
catchbreak
=
options
.
catchbreak
if
self
.
buffer
is
None
:
self
.
buffer
=
options
.
buffer
if
options
.
verbose
:
self
.
verbosity
=
2
elif
options
.
quiet
:
self
.
verbosity
=
0
def
_do_discovery
(
self
,
argv
,
Loader
=
loader
.
TestLoader
):
# handle command line args for test discovery
self
.
progName
=
'%s discover'
%
self
.
progName
parser
=
self
.
_getOptParser
()
parser
.
add_option
(
'-s'
,
'--start-directory'
,
dest
=
'start'
,
default
=
'.'
,
help
=
"Directory to start discovery ('.' default)"
)
parser
.
add_option
(
'-p'
,
'--pattern'
,
dest
=
'pattern'
,
default
=
'test*.py'
,
...
...
@@ -230,17 +230,7 @@ class TestProgram(object):
for
name
,
value
in
zip
((
'start'
,
'pattern'
,
'top'
),
args
):
setattr
(
options
,
name
,
value
)
# only set options from the parsing here
# if they weren't set explicitly in the constructor
if
self
.
failfast
is
None
:
self
.
failfast
=
options
.
failfast
if
self
.
catchbreak
is
None
:
self
.
catchbreak
=
options
.
catchbreak
if
self
.
buffer
is
None
:
self
.
buffer
=
options
.
buffer
if
options
.
verbose
:
self
.
verbosity
=
2
self
.
_setAttributesFromOptions
(
options
)
start_dir
=
options
.
start
pattern
=
options
.
pattern
...
...
Lib/unittest/test/test_program.py
View file @
5f99ced0
...
...
@@ -131,23 +131,6 @@ class TestCommandLineArgs(unittest.TestCase):
FakeRunner
.
test
=
None
FakeRunner
.
raiseError
=
False
def
testHelpAndUnknown
(
self
):
program
=
self
.
program
def
usageExit
(
msg
=
None
):
program
.
msg
=
msg
program
.
exit
=
True
program
.
usageExit
=
usageExit
for
opt
in
'-h'
,
'-H'
,
'--help'
:
program
.
exit
=
False
program
.
parseArgs
([
None
,
opt
])
self
.
assertTrue
(
program
.
exit
)
self
.
assertIsNone
(
program
.
msg
)
program
.
parseArgs
([
None
,
'-$'
])
self
.
assertTrue
(
program
.
exit
)
self
.
assertIsNotNone
(
program
.
msg
)
def
testVerbosity
(
self
):
program
=
self
.
program
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment