简体   繁体   中英

how to distribute python packages with unittests as executable entry points?

I am writing a python package that has executable scripts registered into 'setup.py' as entry points. when the package is installed these are placed into the "bin" directory of the python package installation without the ".py" extension. I want to be able to run the unit tests of the package as an executable too once the package is installed. the unittests are in "test_package.py" which is in the "entry_points" of setup.py: run the python packages

setup(
  entry_points={
      'console_scripts': [
          'my_exec = mypackage.my_exec:main'
          'test_package = mypackage.test_package.main'],
  }
)

the test_package.py script reads some files from "test-files":

mypackage/
  setup.py
  mypackage/
    my_exec.py
    test_package.py
    # files needed to run tests
    test-files/
    # directory containing files written by 'test_package'
    test-results/

when I run "test_package.py" inside the package it works fine. however when I install it and "test_package" is created as executable in "bin", then running:

$ test_package

from command line cannot find the tests. it says "ran 0 tests". what is wrong with this directory structure? what is the right way to distribute unit tests in a way that they can be run from the command line even if they create files? if the package is installed for someone using sudo privileges then i want the user which is sudo-less to be able to run these without requiring write access to where package is installed.

the structure of test_package.py is:

class TestPackage(unittest.Test):
  def setUp():
     ...
  def test_foo():
     # read stuff from 'test-files/'
     # ...
     # output stuff to 'test-results/'
     # ...

def main():
    unittest.main()


if __name__ == '__main__':
    main()

By default unittest.main try to find tests in __main__ module. Because you try to run the tests from entry point then the module is not __main__ anymore.

Probably if you try to pass module argument to unittest.main function which is mypackage.test_package it may work.

For more details you can see the documentation for unittest.main :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM