From a54baebe6e244f9ab86994997640d00cbbedd7ff Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sun, 9 Jun 2024 18:40:00 +0900 Subject: [PATCH] revise test runner conf --- .env.test | 1 + Makefile | 36 +++++++++++++++++++++++------- app/config.py | 2 ++ deps-top.txt | 3 +++ pytest | 6 ----- pytest.ini | 4 ++-- requirements.txt | 58 ++++++++++++++++++++++++++++++------------------ 7 files changed, 72 insertions(+), 38 deletions(-) delete mode 100755 pytest diff --git a/.env.test b/.env.test index 2a799a0..7c214bc 100644 --- a/.env.test +++ b/.env.test @@ -1,4 +1,5 @@ # Env variables for tests +TESTING=YES LOG_LEVEL=DEBUG SITE_NAME=萌翻TEST SECRET_KEY=SECRET diff --git a/Makefile b/Makefile index f32f6ab..1b561b1 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,41 @@ -venv: - python3 -mvenv venv +PYTEST_COV_ARGS = --cov=app --cov-report=term --cov-report=xml:cov.xml + +FORCE: ; + +create-venv: + python3.10 -mvenv venv + +deps: venv/bin/pip install -r requirements.txt +remove-venv: FORCE + rm -rf venv + +recreate-venv: remove-venv create-venv + format: venv/bin/ruff format -test: - @./pytest --html=report.html --self-contained-html +requirements.txt: deps-top.txt recreate-venv + venv/bin/pip install -r deps-top.txt + echo '# GENERATED: run make requirements.txt to recreate lock file' > requirements.txt + venv/bin/pipdeptree --freeze >> requirements.txt + +test: test_all + +test_all_cov: + venv/bin/pytest --html=report.html --self-contained-html ${PYTEST_COV_ARGS[@]} + +test_all: + venv/bin/pytest --html=report.html --self-contained-html -test_p: - @./pytest -n 8 +test_all_parallel: + venv/bin/pytest -n 8 test_single: - @bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_not_exist_error.py" + venv/bin/pytest tests/base/test_not_exist_error.py test_logging: #--capture=no - # WTF: setting level to above WARNING not working? @bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_it_runs.py" # @bash -c "set -uexo allexport && source .env.test && exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG tests/base/test_it_runs.py" diff --git a/app/config.py b/app/config.py index 6a8e961..674825b 100644 --- a/app/config.py +++ b/app/config.py @@ -15,6 +15,8 @@ LOG_PATH = env.get("LOG_PATH") MAX_CONTENT_LENGTH = int(env.get("MAX_CONTENT_LENGTH_MB", 1024)) * 1024 * 1024 ADMIN_EMAIL = env["ADMIN_EMAIL"] +# TODO remove code relying on this +TESTING = env.get('TESTING') == 'YES' # ----------- # Mongo 数据库 # ----------- diff --git a/deps-top.txt b/deps-top.txt index c1a89c5..c71ce3d 100644 --- a/deps-top.txt +++ b/deps-top.txt @@ -15,6 +15,9 @@ gunicorn==20.0.4 # 生产环境服务器 pytest==8.2.1 # 测试框架 pytest-cov==5.0.0 # 测试覆盖率 pytest-xdist==3.6.1 # 并发测试支持 +pytest-dotenv==0.5.2 +pytest-html==4.1.1 + flask-babel==1.0.0 # i18n mongoengine==0.20.0 # Mongo数据库 mongomock==4.1.2 diff --git a/pytest b/pytest deleted file mode 100755 index 7aef141..0000000 --- a/pytest +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash -set -ue -cd $(dirname "$0") -set -xo allexport -source .env.test -exec venv/bin/pytest --capture=sys --log-cli-level=DEBUG "$@" diff --git a/pytest.ini b/pytest.ini index f4dcc45..a9ead01 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,9 +1,9 @@ [pytest] generate_report_on_test = True ; 覆盖率配置,需要时开启(可配合VSCode的Coverage Gutters使用) -addopts = -s -; addopts = -s --cov=app --cov-report=term --cov-report=xml:cov.xml +addopts = --log-level=INFO ; 忽略第三方库Warining filterwarnings = ignore:count is deprecated. Use Collection.count_documents instead.:DeprecationWarning ignore:Using or importing the ABCs .* is deprecated.*:DeprecationWarning +env_files = .env.test diff --git a/requirements.txt b/requirements.txt index 4089b2e..7fa1292 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,26 +1,25 @@ +# GENERATED: run make requirements.txt to recreate lock file asgiref==3.7.2 - typing_extensions==4.11.0 -attrs==23.2.0 -blinker==1.7.0 + typing_extensions==4.12.2 Flask-APIKit==0.0.7 Flask==2.2.5 click==8.1.3 itsdangerous==2.0.1 - Jinja2==3.1.3 + Jinja2==3.1.4 MarkupSafe==2.1.5 - Werkzeug==3.0.2 + Werkzeug==3.0.3 MarkupSafe==2.1.5 marshmallow==3.0.0b20 Flask-Babel==1.0.0 - Babel==2.14.0 + Babel==2.15.0 Flask==2.2.5 click==8.1.3 itsdangerous==2.0.1 - Jinja2==3.1.3 + Jinja2==3.1.4 MarkupSafe==2.1.5 - Werkzeug==3.0.2 + Werkzeug==3.0.3 MarkupSafe==2.1.5 - Jinja2==3.1.3 + Jinja2==3.1.4 MarkupSafe==2.1.5 pytz==2024.1 flower==0.9.5 @@ -33,7 +32,7 @@ flower==0.9.5 click==8.1.3 click-repl==0.3.0 click==8.1.3 - prompt-toolkit==3.0.43 + prompt_toolkit==3.0.46 wcwidth==0.2.13 kombu==5.3.7 amqp==5.2.0 @@ -46,7 +45,7 @@ flower==0.9.5 humanize==4.9.0 prometheus-client==0.8.0 pytz==2024.1 - tornado==6.4 + tornado==6.4.1 google-cloud-storage==1.33.0 google-auth==1.35.0 cachetools==4.2.4 @@ -66,11 +65,11 @@ google-cloud-storage==1.33.0 pyasn1==0.6.0 setuptools==65.5.0 six==1.16.0 - googleapis-common-protos==1.63.0 + googleapis-common-protos==1.63.1 protobuf==4.25.3 protobuf==4.25.3 requests==2.22.0 - certifi==2024.2.2 + certifi==2024.6.2 chardet==3.0.4 idna==2.8 urllib3==1.25.11 @@ -87,7 +86,7 @@ google-cloud-storage==1.33.0 google-crc32c==1.5.0 six==1.16.0 requests==2.22.0 - certifi==2024.2.2 + certifi==2024.6.2 chardet==3.0.4 idna==2.8 urllib3==1.25.11 @@ -102,16 +101,16 @@ oss2==2.7.0 aliyun-python-sdk-core-v3==2.13.3 jmespath==0.10.0 pycryptodome==3.20.0 - aliyun-python-sdk-kms==2.16.2 + aliyun-python-sdk-kms==2.16.3 aliyun-python-sdk-core==2.15.1 - cryptography==42.0.5 + cryptography==42.0.8 cffi==1.16.0 pycparser==2.22 jmespath==0.10.0 crcmod==1.7 pycryptodome==3.20.0 requests==2.22.0 - certifi==2024.2.2 + certifi==2024.6.2 chardet==3.0.4 idna==2.8 urllib3==1.25.11 @@ -119,21 +118,37 @@ Pillow==8.0.1 pip==23.0.1 pipdeptree==2.13.2 pytest-cov==5.0.0 - coverage==7.4.4 + coverage==7.5.3 pytest==8.2.1 exceptiongroup==1.2.1 iniconfig==2.0.0 packaging==24.0 pluggy==1.5.0 tomli==2.0.1 -pytest-forked==1.6.0 - py==1.11.0 +pytest-dotenv==0.5.2 pytest==8.2.1 exceptiongroup==1.2.1 iniconfig==2.0.0 packaging==24.0 pluggy==1.5.0 tomli==2.0.1 + python-dotenv==1.0.1 +pytest-html==4.1.1 + Jinja2==3.1.4 + MarkupSafe==2.1.5 + pytest==8.2.1 + exceptiongroup==1.2.1 + iniconfig==2.0.0 + packaging==24.0 + pluggy==1.5.0 + tomli==2.0.1 + pytest-metadata==3.1.1 + pytest==8.2.1 + exceptiongroup==1.2.1 + iniconfig==2.0.0 + packaging==24.0 + pluggy==1.5.0 + tomli==2.0.1 pytest-xdist==3.6.1 execnet==2.1.1 pytest==8.2.1 @@ -144,5 +159,4 @@ pytest-xdist==3.6.1 tomli==2.0.1 redis==5.0.3 async-timeout==4.0.3 -ruff==0.4.1 -toml==0.10.2 +ruff==0.4.8