运行 pytest 和 pytest-cov 时忽略特定文件(不是目录)

我的项目结构为:

myproject
 src
   -routes.py
   -services.py
   -data_access.py
 tests
   -test_routes.py
   -test_services.py

我想在 src 上运行测试,但想排除 src/data_access.py

我想实现类似于: pytest tests --cov=src --exclude=src/data_access.pyexclude 不是正确的论点。

stack overflow Ignore specific files (not directory) while running pytest and pytest-cov
原文答案

答案:

作者头像

You may need to add coveragerc and use the —omit option.

作者头像

@Anu is right, but didn't fully explained.

You need to create .coveragerc file to the root of your project.(If you don't then you need to specify where is it).

In that file add:
[run]
omit = ./*
This will remove all the folders from coverage, considering that you're currently in the root of the project.
If you need to include specific files, use include = ... instead.