pytest-dev / pytest-dev/pytest

pytest collection overwrites testcases in monorepo like structure

Open
#13,997 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection type: question
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Hi

I have a monorepo, where I have following directory structure

Project/
      repo/
             pkg1/
                     src/
                     tests/
                            …
                            __init__.py
                           test_main.py
             pkg2/
                     src/
                     tests/
                            …
                            __init__.py
                           test_main.py

I also have other mini packages which don’t follow src layout, I have added —import-mode=importlib in root pyproject for addopts
But what I am seeing is test_main.py for pkg2 gets overwritten for pkg1 I.e. in test_main.py for pkg1 I have 3 test cases and in test_main.py for pkg2 I have 8 tests cases, but when I see in logs of pytest I see

…
pkg2/tests/test_main . . . # should have eight

From my understand and my guess is this is happening because when pytest resolves unique path for each it might be picking tests.test_main for both.

I tried verbose run and I can see the test name from pkg1 in path of pkg2

I needed some help guidance / help to point me in right direction to properly setup this for my usecase

Thanks !

Originally posted by @indiVar0508 in https://github.com/pytest-dev/pytest/discussions/13749

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the root pyproject and the two similarly named files, pkg1/tests/test_main.py and pkg2/tests/test_main.py. Run pytest in verbose mode with the existing importlib setting and compare collection and test names for both packages. Done means the cause of the overwritten collection is isolated and the expected three and eight tests remain distinct, with a focused regression test or clear reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.