classification
Title: Load tests from path (patch included)
Type: enhancement Stage: test needed
Components: Tests Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: purcell Nosy List: bluebird, jafo, michael.foord, purcell
Priority: normal Keywords: patch

Created on 2007-09-26 19:25 by bluebird, last changed 2009-06-02 18:25 by michael.foord. This issue is now closed.

Messages (5)
msg56148 - (view) Author: Bluebird (bluebird) * Date: 2007-09-26 19:25
Something very nice about unittest is that it can find automatically the
TestCase that you declare, and the test methods of every test case. This
makes the operation of adding or removing tests very simple.

For test modules however, there is nothing to automatically load all the
modules of a given directory. I think that would be very helpful.

Here is my proposal, to add to the set of TestLoader methods:

============================
def loadTestsFromPath( path='', filePattern='test*.py' ):
    '''Load all the TestCase in all the module of the given path.

    path: directory containing test files
    filePattern: glob pattern to find test modules inside path. Default
is test*.py

    The path will be converted into an import statement so anything that
can not be imported will
    not work.  The path must be relative to the current directory, and
can not include '.' and '..'
    directories.

    To simply load all the test files of the current directories, pass
an empty path (the default).

    Return a test suite containing all the tests.
    '''

    if len(path) == 0:
        pathPattern = filePattern
    else:
        pathPattern = path + '/' + filePattern
    pathPattern = os.path.normpath( pathPattern )
    fileList = glob.glob( pathPattern )

    mainSuite = TestSuite()
    for f in fileList:
        importName = f[:-3]
        importName = importName.replace( '\\', '.' )
        importName = importName.replace( '/', '.' )

        suite = defaultTestLoader.loadTestsFromName(importName)
        mainSuite._tests.extend( suite._tests )

    return mainSuite
===================

I use it like this: on my project, I have the following directory
organisation:

vy
  + run_all_tests.py
  + tests
     - run_tests.py
     - test_xxx.py
     - test_yyy.py
  + libvy
     + tests
        - run_tests.py
        - test_xxx.py
        - test_yyy.py
  + qvy
    + tests
        - run_tests.py
        - test_xxx.py
        - test_yyy.py

 
I can do either:
- cd libvy/tests && python run_tests.py
- cd qvy/tests && python run_tests.py
- cd tests && python run_tests.py
- run_all_tests.py

Each time I add a new test module, it is automatically picked up by the
test runners thank to the loadFromPath() feature. It makes it easy to
maintain the global test suite that runs all the tests. That's the most
important one because that test suite is responsible for non regression.

run_tests.py:
=============
if __name__ == '__main__':
    mainSuite = TestSuite()
    mainSuite._tests.extend( loadTestsFromPath('.')._tests )
    ttr = TextTestRunner(verbosity=2)
    ttr.run( mainSuite )

run_all_tests.py:
=================
if __name__ == '__main__':
    mainSuite = TestSuite()
    mainSuite._tests.extend( loadTestsFromPath( 'libvy/tests' )._tests )
    mainSuite._tests.extend( loadTestsFromPath( 'qvy/tests' )._tests )
    mainSuite._tests.extend( loadTestsFromPath( 'tests' )._tests )
    ttr = TextTestRunner(verbosity=2)
    ttr.run( mainSuite )
msg63673 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2008-03-17 16:48
Patch is inline.

A few notes for submitter:

This also needs to include a documentation patch before it can be accepted.

Please format the docstring for a line length of 78 characters or less.

Ideally, please submit a "diff -c" style patch as an attachment.

Please discuss this approach on python-dev and/or with Steve Purcell
(copied).
msg86603 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-04-26 20:31
I'm intending to implement loadTestsFromPackage which will do a
*similar* job. As well as allowing test autodiscovery it will allow for
customizing test loading from modules / packages using a protocol
thrashed out on the Testing in Python mailing list.
msg87618 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-05-12 12:29
See issue 6001 for a patch implementing test discovery for unittest.

It would allow you to do:

    python -m unittest discover
msg88761 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-06-02 18:25
Similar functionality is now in TestLoader.discover.
History
Date User Action Args
2009-06-02 18:25:33michael.foordsetstatus: open -> closed
resolution: rejected
messages: + msg88761
2009-05-12 12:29:25michael.foordsetmessages: + msg87618
2009-04-26 20:31:20michael.foordsetmessages: + msg86603
2009-04-26 00:46:10ajaksu2setnosy: + michael.foord
versions: + Python 3.1, Python 2.7, - Python 2.5

stage: test needed
2008-03-17 16:48:49jafosetpriority: normal
assignee: purcell
messages: + msg63673
keywords: + patch
nosy: + purcell, jafo
2007-09-26 19:25:04bluebirdcreate