[Python-Dev] adding a "test" fork to a setup.py package

Bill Janssen janssen at parc.com
Tue Sep 11 22:02:36 CEST 2007


It's actually not bad.  I put the test code and the data files in a
"test" subdirectory of my distribution, then added the following to
the setup.py file:

class Test (Command):

    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run (self):

        """Run the regrtest module appropriately"""

        # figure out where the _ssl2 extension will be put
        b = build(self.distribution)
        b.initialize_options()
        b.finalize_options()
        extdir = os.path.abspath(b.build_platlib)

        # now set up the load path
        topdir = os.path.dirname(os.path.abspath(__file__))
        localtestdir = os.path.join(topdir, "test")
        sys.path.insert(0, topdir)        # for ssl package
        sys.path.insert(0, localtestdir)  # for test module
        sys.path.insert(0, extdir)        # for _ssl2 extension

        # make sure the network is enabled
        import test.test_support
        test.test_support.use_resources = ["network"]

        # and load the test and run it
        os.chdir(localtestdir)
        the_module = __import__("test_ssl", globals(), locals(), [])
        # Most tests run to completion simply as a side-effect of
        # being imported.  For the benefit of tests that can't run
        # that way (like test_threaded_import), explicitly invoke
        # their test_main() function (if it exists).
        indirect_test = getattr(the_module, "test_main", None)
        if indirect_test is not None:
            indirect_test()

and added 

      cmdclass={'test': Test},

to the setup call.  Irritating that you have to manually install the
test files as data_files.  Also irritating that data_files aren't
automatically added to the manifest, and that Test has to have null
initialize_options and finalize_options.  And that there's no easy
way to figure out where the build process left the extension.

Bill



More information about the Python-Dev mailing list