TIL: Pytest has a flat namespace

Published Wed 17 May 2023 in personal

by Bryan Weber

Pytest imports test modules into a flat namespace unless you have __init__.py files in the test folders. This means that your test files and test function names cannot be repeated. This explains several failures I was seeing when loading tests in VS Code in a bigger monorepo project.

The context is that a few weeks ago, Brian Okken had Anthony Sotille on Test and Code and they talked about Pytest features they’d change if they could start Pytest over today. Anthony mentioned he’d change the import system and in passing mentioned my TIL.