Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
37d120ae
Kaydet (Commit)
37d120ae
authored
Ara 04, 2010
tarafından
Michael Foord
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 10620: Specifying test modules by path instead of module name to 'python -m unittest'
üst
e2bb4eb7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
114 additions
and
5 deletions
+114
-5
unittest.rst
Doc/library/unittest.rst
+10
-0
main.py
Lib/unittest/main.py
+18
-1
test_program.py
Lib/unittest/test/test_program.py
+79
-0
NEWS
Misc/NEWS
+7
-4
No files found.
Doc/library/unittest.rst
Dosyayı görüntüle @
37d120ae
...
...
@@ -204,6 +204,16 @@ modules, classes or even individual test methods::
You can pass in a list with any combination of module names, and fully
qualified class or method names.
Test modules can be specified by file path as well::
python -m unittest tests/test_something.py
This allows you to use the shell filename completion to specify the test module.
The file specified must still be importable as a module. The path is converted
to a module name by removing the '.py' and converting path separators into '.'.
If you want to execute a test file that isn't importable as a module you should
execute the file directly instead.
You can run tests with more detail (higher verbosity) by passing in the -v flag::
python -m unittest -v test_module
...
...
Lib/unittest/main.py
Dosyayı görüntüle @
37d120ae
...
...
@@ -58,7 +58,24 @@ Examples:
in MyTestCase
"""
def
_convert_name
(
name
):
# on Linux / Mac OS X 'foo.PY' is not importable, but on
# Windows it is. Simpler to do a case insensitive match
# a better check would be to check that the name is a
# valid Python module name.
if
os
.
path
.
isfile
(
name
)
and
name
.
lower
()
.
endswith
(
'.py'
):
if
os
.
path
.
isabs
(
name
):
rel_path
=
os
.
path
.
relpath
(
name
,
os
.
getcwd
())
if
os
.
path
.
isabs
(
rel_path
)
or
rel_path
.
startswith
(
os
.
pardir
):
return
name
name
=
rel_path
# on Windows both '\' and '/' are used as path
# separators. Better to replace both than rely on os.path.sep
return
name
[:
-
3
]
.
replace
(
'
\\
'
,
'.'
)
.
replace
(
'/'
,
'.'
)
return
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
...
...
@@ -153,7 +170,7 @@ class TestProgram(object):
# createTests will load tests from self.module
self
.
testNames
=
None
elif
len
(
args
)
>
0
:
self
.
testNames
=
args
self
.
testNames
=
_convert_names
(
args
)
if
__name__
==
'__main__'
:
# to support python -m unittest ...
self
.
module
=
None
...
...
Lib/unittest/test/test_program.py
Dosyayı görüntüle @
37d120ae
...
...
@@ -273,6 +273,85 @@ class TestCommandLineArgs(unittest.TestCase):
program
.
runTests
()
self
.
assertTrue
(
self
.
installed
)
def
_patch_isfile
(
self
,
names
,
exists
=
True
):
def
isfile
(
path
):
return
path
in
names
original
=
os
.
path
.
isfile
os
.
path
.
isfile
=
isfile
def
restore
():
os
.
path
.
isfile
=
original
self
.
addCleanup
(
restore
)
def
testParseArgsFileNames
(
self
):
# running tests with filenames instead of module names
program
=
self
.
program
argv
=
[
'progname'
,
'foo.py'
,
'bar.Py'
,
'baz.PY'
,
'wing.txt'
]
self
.
_patch_isfile
(
argv
)
program
.
createTests
=
lambda
:
None
program
.
parseArgs
(
argv
)
# note that 'wing.txt' is not a Python file so the name should
# *not* be converted to a module name
expected
=
[
'foo'
,
'bar'
,
'baz'
,
'wing.txt'
]
self
.
assertEqual
(
program
.
testNames
,
expected
)
def
testParseArgsFilePaths
(
self
):
program
=
self
.
program
argv
=
[
'progname'
,
'foo/bar/baz.py'
,
'green
\\
red.py'
]
self
.
_patch_isfile
(
argv
)
program
.
createTests
=
lambda
:
None
program
.
parseArgs
(
argv
)
expected
=
[
'foo.bar.baz'
,
'green.red'
]
self
.
assertEqual
(
program
.
testNames
,
expected
)
def
testParseArgsNonExistentFiles
(
self
):
program
=
self
.
program
argv
=
[
'progname'
,
'foo/bar/baz.py'
,
'green
\\
red.py'
]
self
.
_patch_isfile
([])
program
.
createTests
=
lambda
:
None
program
.
parseArgs
(
argv
)
self
.
assertEqual
(
program
.
testNames
,
argv
[
1
:])
def
testParseArgsAbsolutePathsThatCanBeConverted
(
self
):
cur_dir
=
os
.
getcwd
()
program
=
self
.
program
def
_join
(
name
):
return
os
.
path
.
join
(
cur_dir
,
name
)
argv
=
[
'progname'
,
_join
(
'foo/bar/baz.py'
),
_join
(
'green
\\
red.py'
)]
self
.
_patch_isfile
(
argv
)
program
.
createTests
=
lambda
:
None
program
.
parseArgs
(
argv
)
expected
=
[
'foo.bar.baz'
,
'green.red'
]
self
.
assertEqual
(
program
.
testNames
,
expected
)
def
testParseArgsAbsolutePathsThatCannotBeConverted
(
self
):
program
=
self
.
program
# will this test work on Windows? (is '/...' considered absolute?)
argv
=
[
'progname'
,
'/foo/bar/baz.py'
,
'/green/red.py'
]
self
.
_patch_isfile
(
argv
)
program
.
createTests
=
lambda
:
None
program
.
parseArgs
(
argv
)
self
.
assertEqual
(
program
.
testNames
,
argv
[
1
:])
# it may be better to use platform specific functions to normalise paths
# rather than accepting '.PY' and '\' as file seprator on Linux / Mac
# it would also be better to check that a filename is a valid module
# identifier (we have a regex for this in loader.py)
# for invalid filenames should we raise a useful error rather than
# leaving the current error message (import of filename fails) in place?
if
__name__
==
'__main__'
:
unittest
.
main
()
Misc/NEWS
Dosyayı görüntüle @
37d120ae
...
...
@@ -37,6 +37,13 @@ Core and Builtins
Library
-------
- Issue 10620: `python -m unittest` can accept file paths instead of module
names for running specific tests.
- Issue #9424: Deprecate the `unittest.TestCase` methods `assertEquals`,
`assertNotEquals`, `assertAlmostEquals`, `assertNotAlmostEquals` and `assert_`
and replace them with the correct methods in the Python test suite.
- Issue #10272: The ssl module now raises socket.timeout instead of a generic
SSLError on socket timeouts.
...
...
@@ -236,10 +243,6 @@ Tests
- `python -m test` can be used to run the test suite as well as
`python -m test.regrtest`.
- Issue #9424: Deprecate the `unittest.TestCase` methods `assertEquals`,
`assertNotEquals`, `assertAlmostEquals`, `assertNotAlmostEquals` and `assert_`
and replace them with the correct methods in the Python test suite.
- Do not fail test_socket when the IP address of the local hostname cannot be
looked up.
...
...
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