I'm relatively new to FastApi and unit testing in Python.
I am testing an api that has a conditional check on whether an environment variable FOO
is present.
I am trying to clear that environment variable so that when the api gets invoked by the test the environment variable is not found:
def test_invoice(self, requests_mock):
with mock.patch.dict(os.environ, {}, clear=True):
url = "/api/invoices/check"
requests_mock.post(url, real_http=True)
response = self.test_client.post(url, json.dumps([invoice_id]))
@router.post("/api/invoices/check")
def invoices_api():
if os.environ.get("FOO"):
When the test client gets invoked and the env var is checked the env variable is not None
?
How I can make the code I am trying to test use the mocked os environment?
Note: I've also tried clearing the env variable with the following syntax:
@mock.patch.dict(os.environ, {}, clear=True)
def test_invoice(self, requests_mock):