2

My python project is structured in the so called src-layout.

hyperorg
├── README.md
├── setup.cfg
├── setup.py
├── src
│   └── hyperorg
│       ├── content.py
│       ├── exporter.py
│       ├── __init__.py
│       ├── __main__.py
│       └── reader.py
└── tests
    ├── helper.py
    ├── __init__.py
    ├── test_content.py
    ├── test_exporter.py
    ├── test_hyperorg.py
    └── test_reader.py

I am not able to get coverage running for it. How can I do this?

What I tried so far

Run coverage in the projects root folder just gives me Nothing to run.

Or coverage run ./src/hyperorg gives me

Traceback (most recent call last):
File "/home/user/tab-cloud/hyperorg/src/hyperorg/__main__.py", line 6, in <module>
from .content import Content
ImportError: attempted relative import with no known parent package
4
  • 1
    How do you normally run your test suite? Take that command, and replace "python" with "coverage run". Commented Apr 29, 2022 at 20:45
  • 2
    @NedBatchelder it's not that simple. I just migrated to src-layout from the adhoc one and couldn't find recommended way to run coverage, hope you can help. In src-layout ideally the test & coverage needs to run on the installed package, not the one in src directory. If you set coverage source to installed package coverage report generates with absolute path of files. Combining those reports from a build matrix is another challenge. Commented Oct 2, 2022 at 16:08
  • 1
    Seems to be a serious Issue. Shouldn't we open a report at upstream? Maybe this #963 is about it? Commented Oct 3, 2022 at 11:20
  • 1
    This resolves all my concerns stated above. Commented Oct 3, 2022 at 12:30

1 Answer 1

2

One of the ways of doing that is entering the src folder by cd src and then running coverage from there as following:

coverage run -m unittest discover -s ../tests

You can also leverage rcfile (default .coveragerc) to specify configurations and change HTML report path to be created outside the src folder as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.