2

How can I publish my tests along with my package? I want to keep my tests separate from src, as below.

mypackage/
    src/
        mypackage/
            __init__.py
            hello.py
    tests/
        test_hello.py

And the content of my setup.cfg is:

[metadata]
name = mypackage
version = 1.5.0
author = John Henckel
url = https://test.com
[options]
package_dir =
    = src
packages = find:
[options.packages.find]
where = src

However when I do build + twine the tests are not included. Is there a way to include the tests in the tar and wheel?

John Henckel
  • 10,274
  • 3
  • 79
  • 79

2 Answers2

1

It appears people put the tests/ folder inside the module directory when they want to distribute them along with the code.

Related SO question.

You can see the project structure they use here.

Edit: This link suggests that you just have to list the directories in your setup.py

    packages=['an_example_pypi_project', 'tests'],

I cannot find anything about setup.cfg though I would imagine it should be the same.

Alex
  • 509
  • 4
  • 8
  • Thanks indeed that works. If I make the tests to be a subdirectory under src/mypackage, then it gets published. However, I don't want to do that. I was hoping there is some magic I can put into the `setup.cfg` that will act *as if* the tests is under src/mypackage without actually moving it there. – John Henckel Oct 05 '21 at 18:26
  • Both of those links are from 2009. I tried adding tests to the list of packages, and it works badly. After the user does `pip install mypackage` the `tests` directory is directly under `site-packages`!! so if two packages have tests, the second one will clobber the first! BTW, I think it is astonishing that `pip` does not prevent this from happening, or even show a warning. It would be easy for a rogue package to use the same directory name as another package and clobber it. It is stupid that 'pip' does not do a better job to isolate the packages from each other. – John Henckel Oct 06 '21 at 14:33
0

This isn't really a solution, but as a hack I have decided to just copy the tests directory into the src/mypackage (temporarily, just before the build) as part of the continuous integration pipeline. My build pipeline script looks like this:

git clone https://.../mypackage
cd mypackage
pip install pytest -r requirements.txt
pytest
pip install build twine
cp -R tests src/mypackage
python -m build --wheel
twine upload ... dist/*.whl

and this works just fine. After this I can pip install mypackage and run the tests by navigating to the package root (under site-packages) and running pytest.

The pytest documentation says I should also be able to do pytest --pyargs mypackage. But that does not work. I don't know why. Bug in pytest?

John Henckel
  • 10,274
  • 3
  • 79
  • 79