Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of src project structure #14

Closed
stefaneidelloth opened this issue Oct 27, 2022 · 1 comment
Closed

Support of src project structure #14

stefaneidelloth opened this issue Oct 27, 2022 · 1 comment

Comments

@stefaneidelloth
Copy link

stefaneidelloth commented Oct 27, 2022

My project has following structure using a src folder

+ root
  + src
    + foo
      baa.py
    main.py
  + test
    + foo
        test_baa.py
    test_main.py

=> What are the right parameters for pytest_func_cov and what --import-mode should I use for pytest?

I tried pytest --func_cov=src --func_cov_report=term-missing test but get a No module named 'foo' error.

"Development tools should ensure that they support the src layout cleanly - they can obviously also support the non-src layout, but we should be clear that in order to be recommended in the PyPA guidelines, a tool must be capable of supporting the src layout."

pypa/packaging.python.org#320 (comment)

As kind of a workaround, some interim solution might be to add some extra folders to the PYTHON_PATH.

a) Maybe pytest_func_cov could support some init-hook like pylint.

# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
init-hook=''print('Hello from pylint init hook')"

b) Or support a test_setup.py file under src, initialiting the path:

import sys
import os

file_path = os.path.abspath(__file__)
src_path = os.path.dirname(file_path)

print('Including python path for testing:')
print(src_path)

sys.path.append(src_path)
@stefaneidelloth
Copy link
Author

stefaneidelloth commented Oct 27, 2022

Got it running. The reported coverage is zero; however, that might be another issue, see #12.

I used the command

pytest --func_cov=src --func_cov_report=term-missing

in combination with following pyproject.toml settings for pytest:

[tool.pytest.ini_options]
# Also see
# https://docs.pytest.org/en/7.1.x/customize.html#pyproject-toml
# https://pytest-cov.readthedocs.io/en/latest/config.html
# If you want to see console output from tests, include -s flag
addopts = [
    # !! do not include --cov option here because it would destroy PyCharm debug feature !!
    # Also see https://stackoverflow.com/questions/40718760/unable-to-debug-in-pycharm-with-pytest
    '--junitxml=report.xml',
    '--import-mode=importlib',
]
pythonpath = 'src'
testpaths = 'test'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant