Stew
Stew

Reputation: 4525

How to pass environment-specific application configuration to Pytest

I am using Pytest for unit testing, and some of my tests need to use e.g. database connection information that varies depending on the environment (development vs staging vs production).

This is stuff that, during normal execution, gets read from application configuration files. In an ideal world, I'd just provide a command-line option to Pytest specifying e.g. my development.ini application config file, and my test code could provide that configuration info to the application code under test without me needing to duplicate that information in a test config file. Is this possible?

Two possibilities that strike me from the Pytest documentation are:

  1. put it in the pytest.ini as addopts
  2. pass it in via custom command-line options

(If I understand the documentation correctly, both of the above approaches should use a custom test fixture. Please correct me if that is wrong.)

What is the conventional approach to providing environment-specific configuration information for use by my test code? Or, what are the pros and cons of the different approaches?

Upvotes: 2

Views: 2251

Answers (1)

Frank T
Frank T

Reputation: 9046

The two possibilities you list -- using pytest.ini to add options, and allowing -- are sensible approaches, and in fact would share an implementation. I agree that implementing it using a custom test fixture makes sense.

Here is an example of a pytest plugin that allows the user to specify what Django settings to use when running pytest (in addition to the other ways that Django lets you specify settings). As you can see, the settings file can be specified on the command line or in pytest.ini. This example is evidence that your approach has merit.

Upvotes: 1

Related Questions