Python 在gitlab CI/CD管道中使用pytest测试flask应用程序时,如何忽略某些脚本?
我有一个结构如下的Python 在gitlab CI/CD管道中使用pytest测试flask应用程序时,如何忽略某些脚本?,python,python-3.x,pytest,gitlab-ci,test-coverage,Python,Python 3.x,Pytest,Gitlab Ci,Test Coverage,我有一个结构如下的flask restx文件夹 . ├── app │ ├── extensions.py │ ├── __init__.py │ └── pv_dimensioning │ ├── controller.py │ ├── __init__.py │ ├── models │ │ ├── dto.py │ │ ├── __init__.py │ │ ├── input_output_m
flask restx
文件夹
.
├── app
│ ├── extensions.py
│ ├── __init__.py
│ └── pv_dimensioning
│ ├── controller.py
│ ├── __init__.py
│ ├── models
│ │ ├── dto.py
│ │ ├── __init__.py
│ │ ├── input_output_model.py
│ │ └── vendor_models.py
│ ├── services
│ │ ├── calculator.py
│ │ ├── database.py
│ │ ├── db_crud.py
│ │ ├── db_input_output.py
│ │ ├── __init__.py
│ │ └── processor.py
│ └── utils
│ ├── decode_verify_jwt.py
│ ├── decorator.py
│ └── __init__.py
├── config.py
├── main.py
├── package.json
├── package-lock.json
├── Pipfile
├── Pipfile.lock
├── README.md
├── serverless.yml
└── tests
└── test_processor.py
应用程序已连接到数据库,这就是为什么应用程序中有许多脚本需要VPN连接的原因
我正在使用pytest
编写测试,然后在gitlab CI/CD
上运行测试,以获得适当的覆盖率。我希望避免或省略只能在连接VPN时运行的脚本,并且只为不需要VPN的脚本编写测试。(我对需要VPN的脚本进行了测试,但我不想在CI/CD管道中运行它们)
唯一不需要VPN的脚本是processor.py
,在test\u processor.py
中进行测试
我希望避免使用的脚本位于.coveragerc
中:
[run]
omit =
*/site-packages/*
*/distutils/*
tests/*
/usr/*
app/__init__.py
app/extensions.py
app/pv_dimensioning/models/*
app/pv_dimensioning/utils/*
app/pv_dimensioning/controller.py
app/pv_dimensioning/services/calculator.py
app/pv_dimensioning/services/database.py
app/pv_dimensioning/services/db_crud.py
app/pv_dimensioning/services/db_input_output.py
[html]
directory = htmlcov
.gitlab ci.yml的覆盖范围部分
stages:
- coverage
coverage:
image: python:3.7
stage: coverage
artifacts:
paths:
- htmlcov/
before_script:
- apt-get -y update
- apt-get install curl
- pip install pipenv
- pipenv install --dev
script:
- pipenv run python -m coverage run -m pytest
- pipenv run python -m coverage report -m
- pipenv run python -m coverage html
after_script:
- pipenv run bash <(curl -s https://codecov.io/bash)
阶段:
-覆盖范围
新闻报道:
图片:python:3.7
阶段:覆盖率
人工产品:
路径:
-htmlcov/
在脚本之前:
-apt-get-y更新
-易于安装卷曲
-pip安装pipenv
-pipenv安装--dev
脚本:
-pipenv运行python-m覆盖率运行-m pytest
-pipenv运行python-m覆盖率报告-m
-pipenv运行python-m覆盖率html
在脚本之后:
-据我所知,pipenv runbash的覆盖率是关于报告测试了多少代码库,而不是要运行哪些测试。您所做的是从报表中排除内容,而不是停止正在创建的报表的数据
如果您知道测试将失败(由于外部配置),您应该跳过测试。幸运的是,使用了skipif
decorator
我将在tests/conftest.py
中创建一个函数,如果VPN处于活动状态,该函数将跳过测试。比如:
导入套接字
导入pytest
def_需要_vpn():
has_vpn=False
尝试:
socket.gethostbyname(“”)
has_vpn=True
除socket.error外:
通过
返回pytest.mark.skipif(没有vpn,原因=“需要访问vpn”)
requires_vpn=_requires_vpn()#这与链接上的minversion示例类似
然后,您可以忽略整个测试文件,并在顶部添加以下内容:
pytestmark = requires_vpn
你可以用这个装饰跳过特定的测试
@requires_vpn
def test_this_will_be_skipped():
pass
您可以在gitlab ci配置中单独运行test\u processor.py
,例如pipenv运行python-m coverage run-m pytest tests/test\u processor.py
。@hoefling我尝试过,但仍然,当我执行pipenv run python-m coverage run-m pytest app/tests/test_processor.py时,我会遇到相同的错误,这是因为您在模块级别上运行HTTP请求,因此导入失败。顺便说一句,我怀疑接受的答案能否解决这个问题,因为无论是否跳过测试,测试模块中的导入仍在执行。嘿,感谢您提供的详细答案。是否应在test*.py文件中添加pytestmark=requires\u vpn
?是的,那么我应该首先从conftest.py导入它吗?我认为您不需要导入它,我认为您可以自动访问conftest.py
中的内容,但可能这只是装置。如果您不能直接使用它,我会放入一个单独的helpers.py
文件,然后导入它。
@requires_vpn
def test_this_will_be_skipped():
pass