多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 测试级别 * 单元: 开发者的角度,测试单个代码单元测功能。 * 功能性(集成测试):最终用户的角度, 测试软件产品的多个组件,确保这些组件能够正常工作。 * 端到端 测试应与CI (持续集成)流程相结合,确保不断执行测试。 ## 测试的内容 在Flask 中, 单元测试集中单独测试小代码单元: 1. 数据库模型 ( 通常在models.py中定义) 2. 视图函数调用的函数(例如:服务器端验证检查) 功能测试关注视图功能的运行方式,比如: 1. 视图函数的触发(GET,POST) 2. 视图函数正确处理无效的HTTP方法 3. 无效参数传递给视图函数 ## Python 内置的测试框架,unittest 包含 1. 单元测试的工具, 包括一整套assert 语法用来执行检查 2. 单元测试和单元测试套件的结果 3. 执行测试的测试运行器 pytest 和unittest 的区别 | 对比项 | pytest | unittest | | --- | --- | --- | | 安装 | 第三方库 | 核心标准库的一部分 | | 测试设置和拆卸 | fixtures | setup() 和tearDown() 方法 | | 断言格式 | 内置断言 | | | 结构 | 功能性 | 面向对象 | pytest 的优点 1. 需要更少的样板代码,测试代码更可读 2. 支持简单的assert 语句,这比assertEquals, assertTrue, 和assertContains 更可读 3. 因为不是标准库的一部分, 所以更新更频繁 4. 简化测试状态的建立和拆除 5. 支持 Fixture ## 测试 项目结构 比较建议的方式 1. 测试放在 test 目录中 2. 单元测试和功能测试分目录存放 ( unit, functional) ``` -app.py -project -__init__.py - models.py - blueprint folder -requirements.txt -tests - contest.py - functional - __init__.py -test_users.py -unit __init__.py test_models.py venv ``` ### 功能测试 ``` from project import create_app def test_home_page(): """ GIVEN a Flask application configured for testing WHEN the '/' page is requested (GET) THEN check that the response is valid """ # Set the Testing configuration prior to creating the Flask application os.environ['CONFIG_TYPE'] = 'config.TestingConfig' flask_app = create_app() # Create a test client using the Flask application configured for testing with flask_app.test_client() as test_client: response = test_client.get('/') assert response.status_code == 200 assert b"Welcome to the" in response.data assert b"Flask User Management Example!" in response.data assert b"Need an account?" in response.data assert b"Existing user?" in response.data ``` ## Fixture ### xUnit 经典方式 ``` setUp() 运行测试 TearDown() ``` ### Fixture的优点 1. Fixture 被定义成函数 2. 多个Fixture 可以被用来设置测试函数的初始状态,Fixture 还可以呼叫 Fixture ,所以可以组合使用设置状态 3. Fixture 可以在不同的范围运行 * function - 每个测试函数运行一次 * class - 每个测试类运行一次 * module - 每个模块运行一次 * session - 每个会话运行一次 Fixture 在conftest.py 中创建。 ### 单元测试 Fixture ``` ~~~ user = User('patkennedy79@gmail.com', 'FlaskIsAwesome') assert user.email == 'patkennedy79@gmail.com' assert user.hashed_password != 'FlaskIsAwesome' assert user.role == 'user' ~~~ ``` Fixture ``` @pytest.fixture(scope='module') def new_user(): user = User('patkennedy79@gmail.com', 'FlaskIsAwesome') return user ``` * 装饰@pytest.fixture器指定此函数是具有module-level 范围的固定装置 ### 功能测试 Fixture ``` @pytest.fixture(scope='module') def test_client(): # Set the Testing configuration prior to creating the Flask application os.environ['CONFIG_TYPE'] = 'config.TestingConfig' flask_app = create_app() # Create a test client using the Flask application configured for testing with flask_app.test_client() as testing_client: # Establish an application context with flask_app.app_context(): yield testing_client # this is where the testing happens! ``` ## * pytest 将递归搜索项目结构以查找以 开头的 Python 文件test_*.py,然后运行这些文件中以 开头的函数test_。无需配置即可识别测试文件的位置 * 运行特定 ` python -m pytest tests/functional/` pytest 可以提供装置的调用结构并使用参数进行测试--setup-show ## --setup-show ` pytest --setup-show`命令用于运行单元测试时,展示setup和teardown方法的执行情况。 打开Pytest的setup和teardown试图功能(`--setup-show`)会在每次调用setup和teardown时显示一行详细信息。这可以帮助测试统计test suite,检测性能问题以及了解哪些fixtures甚至可以在模块或session级别使用。这特别对于新的test suite或使用了新的fixture规则的旧的test suite非常有用。 正常情况下,在运行pytest时,pytest会在背后静默地处理fixture setup和teardown。然而,在分析测试时,这些信息可能会对了解测试运行方式有所帮助。 例如: ~~~ shCopy codepytest --setup-show test_mod.py ~~~ 这将运行`test_mod.py`的测试,并显示关于该测试的setup和teardown执行情况的详细信息。 请注意,这只是Pytest的一种测试调试工具,且这不会改变测试的执行方式。他只是提供了更多的透明度,帮助我们理解和诊断测试运行过程中可能出现的问题。 注意:执行`pytest --setup-show`需要已安装pytest库。如果未安装,可以使用`pip install pytest`进行安装。 ## 代码覆盖率 代码覆盖率的包: * coverage.py * pytest-cov : 与pytest 无缝集成 ~~~ python -m pytest --cov=project ~~~ ## pytest-cov `pytest-cov`是一个插件,提供了 pytest 进行代码覆盖率测量的功能。它是使用`coverage.py`的一种便利方式,`coverage.py`是广泛使用的 Python 代码覆盖率工具。 在测试过程中,你可能需要知道哪些代码被执行,哪些未被执行,以此来了解测试的完整性。这就是代码覆盖率(code coverage)的概念,`pytest-cov`就是这样的工具。 例如,你可以如下使用: ~~~ shCopy codepytest --cov=myproj tests/ ~~~ 这将运行 tests 目录下的所有测试,并测量 myproj 的代码覆盖率。结果会显示每个文件的覆盖率,以及总的覆盖率。 你也可以指定一个报告类型,例如使用 HTML 报告: ~~~ shCopy codepytest --cov=myproj --cov-report html tests/ ~~~ 这会生成一个名为`htmlcov`的目录,其中包含与覆盖率测量相关的 HTML 文件。你可以打开`htmlcov/index.html`来查看完整的报告。 如果你还没有安装`pytest-cov`,你可以使用 pip 来安装: ~~~ shCopy codepip install pytest-cov ~~~ ## 参考 https://testdriven.io/blog/flask-pytest/