Skip to content

Commit

Permalink
add pytest-env
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Jun 29, 2024
1 parent 2d59f6b commit 602ff67
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions book/testing/Chapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,57 @@
"source": [
"!pytest -vv --diff-width=60"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using Environment Variables with `pytest-env`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How to use environment variables when testing your code?\n",
"\n",
"It's important to not mix up your test environment and local environment, especially when you want to define environment variables specifically for your tests.\n",
"\n",
"For this case, use `pytest-env`.\n",
"\n",
"`pytest-env` is a pytest plugin to define your environment variables in a pytest.ini file.\n",
"\n",
"Those variables will be isolated from the local environment, perfect for writing and running tests."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install pytest-env"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# pytest.ini\n",
"[pytest]\n",
"env =\n",
" API_KEY=example-key\n",
" API_ENDPOINT=https://example.endpoint.net\n",
" \n",
"# test_example.py\n",
"import os\n",
"\n",
"def test_load_env_vars():\n",
" assert os.environ[\"API_KEY\"] == \"example-key\"\n",
" assert os.environ[\"API_ENDPOINT\"] == https://example.endpoint.net\""
]
}
],
"metadata": {
Expand Down

0 comments on commit 602ff67

Please sign in to comment.