Evgeny
Evgeny

Reputation: 4561

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

I have a project where I defined a test configration in config.py, but I'm puzzled by the following:

  1. Many times there is a testing configuration for config.py being discussed in tutorials, like this one

  2. Actual testing with database is usually done on application and database defined in testclass (with or without Flask-Testing). This test setup does not make use of testing configuration from config.py

    See for example Flask-SQLAlchemy own test fixtures or some of links listed here

There is some advice about a test database being created specifically for the tests, eg from 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

There are no tutorials that say "you do not need a testing configuration, do your test setup in your base test class". Is this something assumed?

Tst configuration in config.py and explicit test setup in class - are they mutually exclusive? Or sometimes you combine the two?

P.S. Here is a list of project configurations without testing config.

Upvotes: 0

Views: 1539

Answers (1)

Arunmozhi
Arunmozhi

Reputation: 964

tl;dr: It is usually a matter of convenience. Prefer definitions in test module.

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

They are NOT mutually exclusive. You can put the config values

  • in the config.py (or)
  • in the test module (or)
  • in both.

Here is an example using the config.py route for motivation.


The decision to store the test config is mostly project dependent.

The config.py with the class inheritance structure comes with its own set of rules. There are certain values like SECRET_KEY and Database connection strings which has to be changed at the deployment stage in order to ensure application security. So, in order to handle such a situation (especially in public open source projects) there is usually a file like config.py.default which carries all the default values. The developer/admin can copy this to config.py and add the values as per requirement. Using such a default config is recommended in Flask docs.

In situations involving automated testing (eg., Continuous Integration), such a default config setup becomes unusable. So storing the test config in the modules provides a convenient solution.

Upvotes: 2

Related Questions