title: Test Pydantic settings in FastAPI
body: <p>Suppose my <code>main.py</code> is like this (this is a simplified example, in my app I use an actual database and I have two different database URIs for development and testing):</p>
  
  <pre class="lang-py prettyprint-override"><code>from fastapi import FastAPI
  from pydantic import BaseSettings
  
  app = FastAPI()
  
  class Settings(BaseSettings):
      ENVIRONMENT: str
  
      class Config:
          env_file = ".env"
          case_sensitive = True
  
  settings = Settings()
  
  databases = {
      "dev": "Development",
      "test": "Testing"
  }
  database = databases[settings.ENVIRONMENT]
  
  @app.get("/")
  def read_root():
      return {"Environment": database}
  </code></pre>
  
  <p>while the <code>.env</code> is</p>
  
  <pre><code>ENVIRONMENT=dev
  </code></pre>
  
  <p>Suppose I want to test my code and I want to set <code>ENVIRONMENT=test</code> to use a testing database. What should I do? In FastAPI documentation (<a href="https://fastapi.tiangolo.com/advanced/settings/#settings-and-testing" rel="noreferrer">https://fastapi.tiangolo.com/advanced/settings/#settings-and-testing</a>) there is a good example but it is about dependencies, so it is a different case as far as I know.</p>
  
  <p>My idea was the following (<code>test.py</code>):</p>
  
  <pre class="lang-py prettyprint-override"><code>import pytest
  
  from fastapi.testclient import TestClient
  
  from main import app
  
  @pytest.fixture(scope="session", autouse=True)
  def test_config(monkeypatch):
      monkeypatch.setenv("ENVIRONMENT", "test")
  
  @pytest.fixture(scope="session")
  def client():
      return TestClient(app)
  
  def test_root(client):
      response = client.get("/")
      assert response.status_code == 200
      assert response.json() == {"Environment": "Testing"}
  </code></pre>
  
  <p>but it doesn't work.</p>
  
  <p>Furthermore I get this error:</p>
  
  <pre><code>ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'session' scoped request object, involved factories
  test.py:7:  def test_config(monkeypatch)
  env\lib\site-packages\_pytest\monkeypatch.py:16:  def monkeypatch()
  </code></pre>
  
  <p>while from <code>pytest</code> official documentation it should work (<a href="https://docs.pytest.org/en/3.0.1/monkeypatch.html#example-setting-an-environment-variable-for-the-test-session" rel="noreferrer">https://docs.pytest.org/en/3.0.1/monkeypatch.html#example-setting-an-environment-variable-for-the-test-session</a>). I have the latest version of <code>pytest</code> installed.</p>
  
  <p>I tried to use specific test environment variables because of this: <a href="https://pydantic-docs.helpmanual.io/usage/settings/#field-value-priority" rel="noreferrer">https://pydantic-docs.helpmanual.io/usage/settings/#field-value-priority</a>.</p>
  
  <p>To be honest I'm lost, my only real aim is to have a different test configuration (in the same way Flask works: <a href="https://flask.palletsprojects.com/en/1.1.x/tutorial/tests/#setup-and-fixtures" rel="noreferrer">https://flask.palletsprojects.com/en/1.1.x/tutorial/tests/#setup-and-fixtures</a>). Am I approaching the problem the wrong way?</p>