config.py 与基础测试 class 中的烧瓶测试配置

flask testing configuration in config.py vs. base test class

我有一个项目,我在 config.py 中定义了一个测试配置,但我对以下内容感到困惑:

  1. 多次有config.py的测试配置 在教程中讨论,例如 this one

  2. 数据库的实际测试通常在应用程序和数据库上完成 在 testclass 中定义(有或没有 Flask-Testing)。这个测试设置确实 不使用 config.py

    中的测试配置

    例如 Flask-SQLAlchemy own test fixtures 或列出的一些链接 here

有一些关于专门为测试创建测试数据库的建议,例如来自 testing-with-sqlalchemy:

First, ensure you set the database URI to something other than your production database ! Second, it’s usually a good idea to create and drop your tables with each test run, to ensure clean tests

没有说 "you do not need a testing configuration, do your test setup in your base test class" 的教程。这是假设的吗?

测试配置 config.py 中的显式测试设置和 class 中的显式测试设置 - 它们相互排斥吗? 或者有时你会把两者结合起来?

P.S。 Here 是没有测试配置的项目配置列表。

tl;dr: 通常是为了方便。首选测试模块中的定义。

Are a test configuration in config.py and explicit test setup in class mutually exclusive? Or sometimes you combine the two?

它们并不相互排斥。您可以将配置值

  • config.py(或)
  • 在测试模块中(或)
  • 两者都有。

Here 是一个使用 config.py 路线作为动力的例子。


存储测试配置的决定主要取决于项目。

具有class继承结构的config.py有自己的一套规则。为了确保应用程序安全,必须在部署阶段更改某些值,如 SECRET_KEY 和数据库连接字符串。因此,为了处理这种情况(尤其是在 public 开源项目中),通常会有一个像 config.py.default 这样的文件,其中包含所有默认值。 developer/admin 可以将其复制到 config.py 并根据要求添加值。在 Flask docs.

中推荐使用这样的默认配置

在涉及自动化测试(例如,持续集成)的情况下,这样的默认配置设置变得不可用。因此将测试配置存储在模块中提供了一个方便的解决方案。