我正在使用 pytest 覆盖率,然后我在命令行中有测试脚本,将为我生成覆盖率报告:
"""Manager script to run the commands on the Flask API."""
import os
import click
from api import create_app
from briqy.logHelper import b as logger
import pytest
app = create_app()
@app.cli.command("tests")
@click.argument("option", required=False)
def run_test_with_option(option: str = None):
from subprocess import run
from shlex import split
if option is None:
raise SystemExit(
pytest.main(
[
"--disable-pytest-warnings",
"--cov=lib",
"--cov-config=.coveragerc",
"--cov-report=term",
"--cov-report=xml",
"--cov-report=html",
"--junitxml=./tests/coverage/junit.xml",
"--cov-append",
"--no-cov-on-fail",
]
)
)
elif option == "watch":
run(
split(
'ptw --runner "python3 -m pytest tests --durations=5 '
'--disable-pytest-warnings"'
)
)
elif option == "debug":
run(
split(
'ptw --pdb --runner "python3 -m pytest tests --durations=5 '
'--disable-pytest-warnings"'
)
)
if __name__ == "__main__":
HOST = os.getenv("FLASK_RUN_HOST", default="127.0.0.1")
PORT = os.getenv("FLASK_RUN_PORT", default=5000)
DEBUG = os.getenv("FLASK_DEBUG", default=False)
app.run(
host=HOST,
port=PORT,
debug=DEBUG,
)
if bool(DEBUG):
logger.info(f"Flask server is running in {os.getenv('ENV')}")
但是,在运行测试之后,它会显示测试发现的文件顶部的导入。有谁知道如何摆脱那些未覆盖的线条?