r/Python • • 14h ago

Discussion More Python projects should test what they actually ship, not just what's in the repository

A surprisingly embarrassing category of Python bugs is the stuff that works perfectly in development but breaks after installing the actual wheel.

Missing template files, undeclared dependencies, paths that assume you're running from the project root, etc.

I've become quite fond of having a separate CI job that builds the wheel, installs it into a fresh environment and runs a few basic functionality checks from an unrelated directory.

Nothing elaborate. Just enough to experience the package the way an actual user would.

It's interesting how much attention we give to test coverage while sometimes neglecting to test the very artifact we're distributing.

Editable installs are great for development, but I think they can make us a little too comfortable.

60 Upvotes

16 comments sorted by

36

u/luthienpogo 13h ago

This is also one of the strongest arguments for using a src layout. Accidentally importing your local source tree instead of the installed package is a surprisingly easy way to give yourself false confidence.

2

u/marr75 8h ago

This is the way. Simultaneously, treat your tests as scripts (don't have them import each other).

13

u/SheriffRoscoe Pythonista 13h ago

This is true of all software.

3

u/SFJulie 12h ago

As a former perl dev, my python modules do tests before install and abort before install.

This (like stricture from perl) are annoyingly not standard.

Perl also automatizes the report of failures by various parameters (as version of the module, language version, OS) that it makes troubleshooting easier for maintainer.

I would freaking love that python embraces good practices from 30 years ago /troll

2

u/Exotic-Draft8802 13h ago

I was about to downvote - thanks for the explanation. I agree 👍 

1

u/caatbox288 13h ago

Yeah, a few smoke tests like that should be standard for python packages that are pushed to pypi.

•

u/ogrinfo 8m ago

People actually do this? I guess I'm lucky I work somewhere with very strong quality practices. It's easy to add a CI job that just installs the wheel into a clean environment and runs app -h to check it doesn't fall over.

1

u/9BQRgdAH 13h ago

Is this not why containers are used?

2

u/marr75 8h ago

No. This isn't just "works on my machine". It's x works when not installed". Most container setups mount the source.

3

u/fiddle_n 13h ago

It’s one aspect of why containers are used, especially if you use dev containers for development. That said one can run a project locally without a container and then build the container in CI - in that case you are still liable to what OP described.

•

u/Important_Ninja_3909 20m ago

I think this is an important point, especially for Python projects that are meant to be installed and used by others.

A project can pass all its tests locally but still fail after installation because of missing dependencies, incorrect file paths or packaging configuration.

I haven't worked extensively with Python package distribution yet, but this is something I want to pay more attention to as I build more backend projects with FastAPI.

Testing the actual installed package in a clean environment seems like a simple step that can prevent a lot of headaches later.

-2

u/AtreidesPlum 13h ago

A passing test suite and a broken wheel aren't contradictory. You've just thoroughly tested something you're not shipping.