安装¶
在你开始之前¶
本教程假设您已经按照 安装 Pyramid 除外 不要创建虚拟环境或安装Pyramid . 因此,您将满足以下要求。
操作系统上安装了一个python解释器。
你已经满足了 安装包的要求 .
将目录更改为新创建的项目¶
在UNIX上¶
cd tutorial
在Windows上¶
cd tutorial
设置和使用 VENV
环境变量¶
我们将设置 VENV
环境变量到虚拟环境的绝对路径,并使用它进行下一步。
在UNIX上¶
export VENV=~/tutorial
在Windows上¶
set VENV=c:\tutorial
创建虚拟环境¶
在UNIX上¶
python3 -m venv $VENV
在Windows上¶
python -m venv %VENV%
在虚拟环境中升级打包工具¶
在UNIX上¶
$VENV/bin/pip install --upgrade pip setuptools
在Windows上¶
%VENV%\Scripts\pip install --upgrade pip setuptools
以开发模式安装项目¶
为了进行项目工作,您必须在工作区中将项目“注册”为开发鸡蛋。我们将同时安装测试需求。我们用下面的命令来完成。
在UNIX上¶
$VENV/bin/pip install -e ".[testing]"
在Windows上¶
%VENV%\Scripts\pip install -e ".[testing]"
在所有操作系统上¶
控制台将显示 pip
检查包并安装丢失的包。成功执行此命令将显示如下行:
Successfully installed BTrees-4.7.2 Chameleon-3.8.1 Mako-1.1.3 MarkupSafe-1.1.1 PasteDeploy-2.1.1 Pygments-2.7.3 WebTest-2.0.35 ZConfig-3.5.0 ZEO-5.2.2 ZODB-5.6.0 attrs-20.3.0 beautifulsoup4-4.9.3 cffi-1.14.4 coverage-5.3.1 hupper-1.10.2 iniconfig-1.1.1 packaging-20.8 persistent-4.6.4 plaster-1.0 plaster-pastedeploy-0.7 pluggy-0.13.1 py-1.10.0 pycparser-2.20 pyparsing-2.4.7 pyramid-1.10.5 pyramid-chameleon-0.3 pyramid-debugtoolbar-4.9 pyramid-mako-1.1.0 pyramid-retry-2.1.1 pyramid-tm-2.4 pyramid-zodbconn-0.8.1 pytest-6.2.1 pytest-cov-2.10.1 repoze.lru-0.7 six-1.15.0 soupsieve-2.1 toml-0.10.2 transaction-3.0.1 translationstring-1.4 tutorial venusian-3.0.0 waitress-1.4.4 webob-1.8.6 zc.lockfile-2.0 zdaemon-4.3 zodbpickle-2.0.0 zodburi-2.4.0 zope.deprecation-4.4.0 zope.interface-5.2.0
测试要求在我们项目的 setup.py
文件,在 tests_require
和 extras_require
诗节。
24tests_require = [
25 'WebTest',
26 'pytest',
27 'pytest-cov',
28]
48 extras_require={
49 'testing': tests_require,
50 },
运行测试¶
在开发模式和测试需求中安装项目之后,您可以为项目运行测试。以下命令提供选项 pytest
它规定了应为其运行测试的模块,以及 pytest
在安静模式下。
在UNIX上¶
$VENV/bin/pytest -q
在Windows上¶
%VENV%\Scripts\pytest -q
对于成功的测试运行,您应该看到这样结束的输出:
....
4 passed in 0.49 seconds
公开测试覆盖率信息¶
你可以运行 pytest
命令查看测试覆盖率信息。这与运行测试的方式相同 pytest
有,但提供了额外的 coverage 信息,显示测试覆盖了项目的哪些行。
我们已经安装了 pytest-cov
打包到我们的虚拟环境中,这样我们就可以在覆盖范围内运行测试。
在UNIX上¶
$VENV/bin/pytest --cov --cov-report=term-missing
在Windows上¶
%VENV%\Scripts\pytest --cov --cov-report=term-missing
如果成功,您将看到如下输出:
======================== test session starts =========================
platform darwin -- Python 3.9.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /filepath/tutorial, inifile: pytest.ini, testpaths: tutorial
plugins: cov-2.10.1
collected 4 items
tests/test_functional.py .. [ 50%]
tests/test_views.py .. [100%]
---------- coverage: platform darwin, python 3.7.3-final-0 -----------
Name Stmts Miss Cover Missing
-----------------------------------------------------------
tutorial/__init__.py 16 0 100%
tutorial/models/__init__.py 8 0 100%
tutorial/pshell.py 6 4 33% 5-12
tutorial/routes.py 2 0 100%
tutorial/views/__init__.py 0 0 100%
tutorial/views/default.py 4 0 100%
tutorial/views/notfound.py 4 0 100%
-----------------------------------------------------------
TOTAL 40 4 90%
===================== 4 passed in 0.85 seconds =======================
我们的包没有100%的测试覆盖率。
启动应用程序¶
启动应用程序。见 这是什么 pserve 事情 有关更多信息 pserve
.
在UNIX上¶
$VENV/bin/pserve development.ini --reload
在Windows上¶
%VENV%\Scripts\pserve development.ini --reload
备注
您的操作系统防火墙(如果有的话)可能会弹出一个对话框,请求授权以允许python接受传入的网络连接。
如果成功,您将在控制台上看到类似的内容:
Starting monitor for PID 65233.
Starting server in PID 65233.
Serving on http://localhost:6543
Serving on http://localhost:6543
这意味着服务器已准备好接受请求。
在浏览器中访问应用程序¶
在浏览器中,访问http://localhost:6543/。您将看到生成的应用程序的默认页面。
您将注意到的一件事是页面右侧的“调试工具栏”图标。您可以在上阅读有关图标用途的更多信息 调试工具栏 . 它允许您在开发时获取有关应用程序的信息。