Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
15e6db66
Commit
15e6db66
authored
Apr 23, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved some of the test-discovery logic into tests/common.py
parent
4e6e8e69
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
71 deletions
+77
-71
pystache/tests/common.py
+66
-0
pystache/tests/main.py
+11
-71
No files found.
pystache/tests/common.py
View file @
15e6db66
...
...
@@ -24,6 +24,8 @@ SPEC_TEST_DIR = os.path.join(PROJECT_DIR, 'ext', 'spec', 'specs')
# containing doctests. The paths should be relative to the project directory.
TEXT_DOCTEST_PATHS
=
[
'README.rst'
]
UNITTEST_FILE_PREFIX
=
"test_"
def
html_escape
(
u
):
"""
...
...
@@ -45,6 +47,70 @@ def get_data_path(file_name):
return
os
.
path
.
join
(
DATA_DIR
,
file_name
)
# Functions related to get_module_names().
def
_find_files
(
root_dir
,
should_include
):
"""
Return a list of paths to all files in the given directory.
Arguments:
should_include: a function that accepts a file path and returns True or False.
"""
paths
=
[]
# Return value.
# os.walk() is new in Python 2.3
# http://docs.python.org/library/os.html#os.walk
for
dir_path
,
dir_names
,
file_names
in
os
.
walk
(
root_dir
):
new_paths
=
[
os
.
path
.
join
(
dir_path
,
file_name
)
for
file_name
in
file_names
]
new_paths
=
filter
(
should_include
,
new_paths
)
paths
.
extend
(
new_paths
)
return
paths
def
_make_module_names
(
package_dir
,
paths
):
"""
Return a list of fully-qualified module names given a list of module paths.
"""
package_dir
=
os
.
path
.
abspath
(
package_dir
)
package_name
=
os
.
path
.
split
(
package_dir
)[
1
]
prefix_length
=
len
(
package_dir
)
module_names
=
[]
for
path
in
paths
:
path
=
os
.
path
.
abspath
(
path
)
# for example <path_to_package>/subpackage/module.py
rel_path
=
path
[
prefix_length
:]
# for example /subpackage/module.py
rel_path
=
os
.
path
.
splitext
(
rel_path
)[
0
]
# for example /subpackage/module
parts
=
[]
while
True
:
(
rel_path
,
tail
)
=
os
.
path
.
split
(
rel_path
)
if
not
tail
:
break
parts
.
insert
(
0
,
tail
)
# We now have, for example, ['subpackage', 'module'].
parts
.
insert
(
0
,
package_name
)
module
=
"."
.
join
(
parts
)
module_names
.
append
(
module
)
return
module_names
def
get_module_names
(
package_dir
,
should_include
):
"""
Return a list of fully-qualified module names in the given package.
"""
paths
=
_find_files
(
package_dir
,
should_include
)
names
=
_make_module_names
(
package_dir
,
paths
)
return
names
class
AssertStringMixin
:
"""A unittest.TestCase mixin to check string equality."""
...
...
pystache/tests/main.py
View file @
15e6db66
...
...
@@ -11,13 +11,12 @@ import os
import
sys
from
unittest
import
TestProgram
from
pystache.tests.common
import
PACKAGE_DIR
,
PROJECT_DIR
,
SPEC_TEST_DIR
from
pystache.tests.common
import
PACKAGE_DIR
,
PROJECT_DIR
,
SPEC_TEST_DIR
,
UNITTEST_FILE_PREFIX
from
pystache.tests.common
import
get_module_names
from
pystache.tests.doctesting
import
get_doctests
from
pystache.tests.spectesting
import
get_spec_tests
UNITTEST_FILE_PREFIX
=
"test_"
# TODO: enhance this function to create spec-test tests.
def
run_tests
(
sys_argv
):
"""
...
...
@@ -60,83 +59,24 @@ def run_tests(sys_argv):
# No need to return since unitttest.main() exits.
def
_find_unittest_files
(
package_dir
):
"""
Return a list of paths to all unit-test files in the given package directory.
"""
paths
=
[]
# Return value.
def
is_unittest
(
file_name
):
return
file_name
.
startswith
(
UNITTEST_FILE_PREFIX
)
and
file_name
.
endswith
(
'.py'
)
# os.walk() is new in Python 2.3
# http://docs.python.org/library/os.html#os.walk
for
dir_path
,
dir_names
,
file_names
in
os
.
walk
(
package_dir
):
file_names
=
filter
(
is_unittest
,
file_names
)
for
file_name
in
file_names
:
path
=
os
.
path
.
join
(
dir_path
,
file_name
)
paths
.
append
(
path
)
return
paths
def
_get_module_names
(
package_dir
,
paths
):
"""
Return a list of fully-qualified test module names given a list of module paths.
"""
package_dir
=
os
.
path
.
abspath
(
package_dir
)
package_name
=
os
.
path
.
split
(
package_dir
)[
1
]
prefix_length
=
len
(
package_dir
)
module_names
=
[]
for
path
in
paths
:
path
=
os
.
path
.
abspath
(
path
)
# for example <path_to_package>/subpackage/module.py
rel_path
=
path
[
prefix_length
:]
# for example /subpackage/module.py
rel_path
=
os
.
path
.
splitext
(
rel_path
)[
0
]
# for example /subpackage/module
parts
=
[]
while
True
:
(
rel_path
,
tail
)
=
os
.
path
.
split
(
rel_path
)
if
not
tail
:
break
parts
.
insert
(
0
,
tail
)
# We now have, for example, ['subpackage', 'module'].
parts
.
insert
(
0
,
package_name
)
module
=
"."
.
join
(
parts
)
module_names
.
append
(
module
)
return
module_names
def
_get_test_module_names
(
package_dir
):
"""
Return a list of fully-qualified module names given a list of module paths.
"""
paths
=
_find_unittest_files
(
package_dir
)
modules
=
_get_module_names
(
package_dir
,
paths
)
return
modules
def
_discover_test_modules
(
package_dir
):
"""
Discover and return a sorted list of the names of unit-test modules.
"""
modules
=
_get_test_module_names
(
package_dir
)
modules
.
sort
()
def
is_unittest_module
(
path
):
file_name
=
os
.
path
.
basename
(
path
)
return
file_name
.
startswith
(
UNITTEST_FILE_PREFIX
)
and
file_name
.
endswith
(
'.py'
)
names
=
get_module_names
(
package_dir
,
is_unittest_module
)
names
.
sort
()
# This is a sanity check to ensure that the unit-test discovery
# methods are working.
if
len
(
modul
es
)
<
1
:
raise
Exception
(
"No unit-test modules found
."
)
if
len
(
nam
es
)
<
1
:
raise
Exception
(
"No unit-test modules found
--
\n
in
%
s"
%
package_dir
)
return
modul
es
return
nam
es
# The function unittest.main() is an alias for unittest.TestProgram's
...
...
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