在Google App Engine (GAE)中对来自webapp WSGI应用程序的响应进行单元测试,可以通过使用测试工具如PyTest来编写单元测试。以下是在GAE上创建和执行单元测试的基本步骤:
- 安装测试库 PyTestpip install pytest
- 创建项目与app engine应用程序cd your-project-path
gcloud init
gcloud app create --version=1
- 创建测试文件touch tests.py
- 在测试文件('tests.py')中编写测试代码from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, GAE!'
def test_app():
response = request.get('/')
assert response.status_code == 200
assert 'Hello, GAE!' == response.data.decode('utf-8')
print('All tests passed!')
- 配置 PyTest 使用
pytest.ini
文件touch pytest.ini
将以下选项添加到 pytest.ini
文件中:
[pytest]
python_files = tests.py
junit_files = junit_results.xml
- 执行测试:python -m pytest
测试运行后生成的 junit_results.xml
文件会被保存在同一目录下。根据需求进行分析,以确保代码按预期工作。
- 在Google App Engine上集成Jenkins进行持续集成和持续部署(CI/CD):pip install -U jenkins将Jenkins与Google App Engine集成:https://cloud.google.com/solutions/jenkins-on-app-engine。
执行代码的 CI/CD 流:
./gradlew clean build install check -Papp_engine_gae --stacktrace
这样就可以在Google App Engine上对来自webapp WSGI应用程序的响应进行单元测试了。后续可以在Google App Engine持续集成和部署中扩展这些测试,确保代码正确性,并提高产品质量。
推荐腾讯云的云计算产品:
- 腾讯云CVM服务器:基于云优化的Serverless容器技术和操作系统,提供高性能、可伸缩的容器运行时服务。
- 腾讯云Redis:提供高性能的键值存储服务,具备高可靠、高可用、高压缩等特点。
- 腾讯云COS:提供稳定可靠的存储服务功能,支持多种数据冗余策略和分片策略。
- 腾讯云数据库DBS:提供可弹性扩展、高性能的数据库服务,支持MySQL、PostgreSQL和腾讯云PolarDB等数据库类型。
- 腾讯云CloudFlareCDN:提供全球范围内的边缘CDN节点,通过全球负载均衡系统,帮助网站提高跨地域的访问速度和响应速度。
腾讯云官网链接:https://cloud.tencent.com/