title: How to monkeypatch env variable correctly in pytest
body: <p>I have some environment variable set that I need to mock when running my tests but when I try using <code>monkeypatch.setenv</code> it just doesn't work and keep fetching the variables from <code>.env</code> file. I even tried <code>delenv</code> before but still can't figure out how to set the test env variables correctly. I've used the <code>setattr</code> before and it worked just fine but can't figure out how to do env patching correctly.</p>
  <p>The test fixture:</p>
  <pre class="lang-py prettyprint-override"><code>@pytest.fixture
  def engine(monkeypatch):
      monkeypatch.delenv('DATABASE_URL', raising=False)
      monkeypatch.setenv('DATABASE_URL', 'postgres://root:password@localhost:5432/db_name')
      return DatabaseEngine().get()
  </code></pre>
  <p>The .env file contains that <code>DATABASE_URL</code> which is saved to a variable named <code>DB_CONFIG</code>
  in a module named <code>config</code></p>
  <pre class="lang-py prettyprint-override"><code>    load_dotenv()
      DB_CONFIG = os.getenv('DATABASE_URL')
  </code></pre>
  <p>and used in the <code>DatabaseEngine</code>.</p>
  <p>It's not the first time that I tried to monkeypatch the env variables and failed, any clue how to proceed please ?</p>