3

I wrote a Python3 package which I am soon going to ship/share via PyPI and Github.

Now I am looking for the optimal solution for the end user to have a small test which can be run to see if after setup.py build and setup.py install the package/analysis works on the end users' system. I am NOT talking about a test on the development side.

I would ship some toy data set in order to perform the test since my package is used for data analysis.

My thoughts on this:

  1. setup.py test would be a nice option but it seems to be deprecated soon
  2. I do not want a solution where the end user has to install an additional package like pytest or tox first

Any recommendations on how to integrate a test into my package considering the two points above?

Michael
  • 706
  • 9
  • 29
  • 1
    You might be interested in this question about [post-install script with Python setuptools](https://stackoverflow.com/q/20288711/3767239) as well. – a_guest Jul 29 '20 at 11:39
  • 1
    There is a software quality and assurance SE https://sqa.stackexchange.com/ you may find this type of question is a better place for. – possum Jul 29 '20 at 12:15
  • @possum Thanks for pointing this out. Was not aware. – Michael Jul 29 '20 at 12:56

1 Answers1

1

You can provide a __main__.py file which contains the test logic. Then the user can invoke the test via python -m your-package-name test if they wish to. For example:

# __main__.py

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('mode', choices=('test',))

args = parser.parse_args()

if args.mode == 'test':
    # perform test logic here
    ...

Alternatively you can specify a command line script as an entry point in setup.py which allows your users to invoke your-pkg-name-test from the command line. This tutorial contains extensive information on how to realize this. Basically you need to add the following to your setup.py:

setup(
    ...
    entry_points = {
        'console_scripts': ['my-pkg-test=mypkg.user_test:main'],
    }
    ...
)

and then ship an additional file user_test.py with the following function:

def main():
    # test logic goes here
    ...

So the overall directory structure is as follows:

my-pkg/
├── mypkg
│   ├── __init__.py
│   ├── __main__.py  # You can use either this ...
│   └── user_test.py  # ... or this together with 'console_scripts'.
└── setup.py
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Thanks! In the end I wrote a testing script which calls main() with some parameters. It goes along the lines of your solution but did not integrate it into setup.py. Might do this later though... – Michael Jul 29 '20 at 13:36
  • 1
    @Michael It's important to note that `setup.py` is not invoked when *installing* from a wheel (`.whl`). It is however invoked when *building* that wheel. So if you intend to distribute your package as a wheel (which is useful) then including the tests in `setup.py` will not invoke them on the user site upon installation. This needs to be done separately by the user. – a_guest Jul 29 '20 at 14:02