Skip to content

Commit 88d1765

Browse files
committed
add pytest --import-mode=importlib
1 parent d278db4 commit 88d1765

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

docs/posts/2021/2021-06-12-python-unittest-cheat-sheet.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ categories:
77
comments: true
88
date:
99
created: 2021-06-12
10-
updated: 2024-12-27
10+
updated: 2025-02-17
1111
description: ''
1212
---
1313

@@ -783,3 +783,20 @@ after_yield_1
783783

784784
[Each atomic operation should be in a separate fixture](https://docs.pytest.org/en/stable/how-to/fixtures.html#safe-fixture-structure) and separating it from other.
785785
But not a big fixutre with many teardown operations after `yield`.
786+
787+
### no **init**.py files needed in test directories
788+
789+
With `--import-mode=importlib`, pytest can discover test files without needing `__init__.py` files in the test directories. This allows for a cleaner directory structure and avoids issues with same-named test files in different directories.
790+
791+
Without `--import-mode=importlib`, `tests/unit/entrypoints/test_health.py` and `tests/unit/services/test_health.py` both has the same module name `test_health`, which causes import conflicts. With `--import-mode=importlib`, pytest treats them as separate modules based on their file paths, allowing both to coexist without issues.
792+
793+
```toml
794+
# pyproject.toml
795+
[tool.pytest.ini_options]
796+
# --import-mode=importlib for same-named test files in different directories
797+
addopts = """
798+
-s
799+
-vv
800+
--import-mode=importlib
801+
"""
802+
asyncio_mode = "auto"

0 commit comments

Comments
 (0)