[TIP] Extension module discovery questions...

Fernando Perez fperez.net at gmail.com
Wed Jul 2 00:05:36 PDT 2008

Hi Jason,

On Wed, Jun 25, 2008 at 7:27 AM, jason pellerin <jpellerin at gmail.com> wrote:

> Here's what I would do:
> * save a reference to the core test loader in prepareTestLoader()
> * implement wantFile() to return True for files that are extension modules
> * implement loadTestsFromFile() so that when it sees a file that is an
> extension module, it imports the module and then does:
>  yield self.loader.loadTestsFromModule(extension_module)

Sorry for not replying before, I was at a conference for a few days.
But many thanks for your advice.  I'm trying to implement it and I'm
close, but it's proving surprisingly tricky to navigate the combined
layers of nose, plugins, unittest and doctest when it comes to test
loading.  In the meantime, I have  a suggestion to make.  In nose's
loader, the following code is at work:

    def loadTestsFromFile(self, filename):
        """Load tests from a non-module file. Default is to raise a
        ValueError; plugins may implement `loadTestsFromFile` to
        provide a list of tests loaded from the file.
        log.debug("Load from non-module file %s", filename)
            tests = [test for test in
            if tests:
                # Plugins can yield False to indicate that they were
                # unable to load tests from a file, but it was not an
                # error -- the file just had no tests to load.
                tests = filter(None, tests)
                return self.suiteClass(tests)
                # Nothing was able to even try to load from this file
                open(filename, 'r').close() # trigger os error
                raise ValueError("Unable to load tests from file %s"
                                 % filename)
        except (KeyboardInterrupt, SystemExit):
            exc = sys.exc_info()
            return self.suiteClass([Failure(*exc)])

That last trapping of exceptions is IMHO not a very good idea, as it
makes debugging problems nigh near impossible, since the traceback one
sees has been swallowed away.  Here's an example:

ERROR: Failure: <type 'exceptions.ValueError'>(Unable to load tests
from file /home/fperez/code/scipy_berkeley/cython/primes/sprimes/primes.so)
Traceback (most recent call last):
  File "/var/lib/python-support/python2.5/nose/loader.py", line 201,
in loadTestsFromFile
    % filename)
ValueError: Unable to load tests from file


I understand your reasons for trapping exceptions, but in that case I
think it would make life vastly easier if you generated a full version
of the original traceback and stored that in the message. I'm being
forced to monkeypatch nose itself just so I can see what my original
errors actually are in trying to debug this.

Thanks for all the help though, I really appreciate it. I'll continue
with the fun over here :)



More information about the testing-in-python mailing list