New submission from Chris Jerdonek:

I have observed that when running unit tests using unittest's test discovery, 
unittest can simultaneously (1) modify sys.path unnecessarily (by adding a path 
that is already in sys.path with a different case), and (2) modify sys.path by 
adding a path of the "wrong" case.

This occurs for me on Mac OS X with the default case-insensitive file system.

If the path--


is already in sys.path, running unittest's test discovery will prepend sys.path 
with the following:


Aside from causing unnecessary modifications of sys.path, this also causes an 
issue in coverage, for example:

The relevant code is here in unittest/ (with `top_level_dir` starting 
out as os.curdir):

    top_level_dir = os.path.abspath(top_level_dir)

    if not top_level_dir in sys.path:
        # all test modules must be importable from the top level directory
        # should we *unconditionally* put the start directory in first
        # in sys.path to minimise likelihood of conflicts between installed
        # modules and development versions?
        sys.path.insert(0, top_level_dir)
    self._top_level_dir = top_level_dir

(from )

The issue occurs when os.path.abspath(top_level_dir) is already in sys.path but 
with a different case.  (Note that if os.path.abspath() returned a path with 
the "right" case, then the unittest code would be okay.)

See also these two threads regarding obtaining the correct case for a path:


components: Library (Lib)
messages: 232993
nosy: chris.jerdonek
priority: normal
severity: normal
status: open
title: unittest can unnecessarily modify sys.path (and with the wrong case)
type: behavior
versions: Python 3.4, Python 3.5

Python tracker <>
Python-bugs-list mailing list

Reply via email to