@@ -270,38 +270,53 @@ code is restored and everything that was changed by the patch is undone.
270270
271271The ``monkeypatch `` fixture offers the following functions:
272272
273- +-------------------------------------------------------+-----------------------+
274- | Function | Description |
275- +=======================================================+=======================+
276- | :samp: `setattr(TARGET, NAME, VALUE, raising=True) ` | sets an attribute |
277- | [1 ]_ | |
278- +-------------------------------------------------------+-----------------------+
279- | :samp: `delattr(TARGET, NAME, raising=True) ` [1 ]_ | deletes an attribute |
280- +-------------------------------------------------------+-----------------------+
281- | :samp: `setitem(DICT, NAME, VALUE) ` | sets a dict entry |
282- | | |
283- +-------------------------------------------------------+-----------------------+
284- | :samp: `delitem(DICT, NAME, raising=True) ` [1 ]_ | deletes a dict entry |
285- +-------------------------------------------------------+-----------------------+
286- | :samp: `setenv(NAME, VALUE, prepend=None) ` [2 ]_ | sets an environment |
287- | | variable |
288- +-------------------------------------------------------+-----------------------+
289- | :samp: `delenv(NAME, raising=True) ` [1 ]_ | deletes an environment|
290- | | variable |
291- +-------------------------------------------------------+-----------------------+
292- | :samp: `syspath_prepend(PATH) ` | expands the path |
293- | | ``sys.path `` |
294- +-------------------------------------------------------+-----------------------+
295- | :samp: `chdir(PATH) ` | changes the current |
296- | | working directory |
297- +-------------------------------------------------------+-----------------------+
273+ +-----------------------------------------------+-----------------------+
274+ | Function | Description |
275+ +===============================================+=======================+
276+ | :meth: `monkeypatch.setattr(obj, name, value, | sets an attribute |
277+ | raising=True) | |
278+ | <pytest.MonkeyPatch.setattr>` | |
279+ | [1 ]_ | |
280+ +-----------------------------------------------+-----------------------+
281+ | :meth: `monkeypatch.delattr(obj, name, | deletes an attribute |
282+ | raising=True) | |
283+ | <pytest.MonkeyPatch.delattr>` | |
284+ | [1 ]_ | |
285+ +-----------------------------------------------+-----------------------+
286+ | :meth: `monkeypatch.setitem(mapping, name, | sets a dict entry |
287+ | value) <pytest.MonkeyPatch.setitem>` | |
288+ +-----------------------------------------------+-----------------------+
289+ | :meth: `monkeypatch.delitem(obj, name, | deletes a dict entry |
290+ | raising=True) <pytest.MonkeyPatch.delitem>` | |
291+ | [1 ]_ | |
292+ +-----------------------------------------------+-----------------------+
293+ | :meth: `monkeypatch.setenv(name, value, | sets an environment |
294+ | prepend=None) <pytest.MonkeyPatch.setenv>` | variable |
295+ | [2 ]_ | |
296+ +-----------------------------------------------+-----------------------+
297+ | :meth: `monkeypatch.delenv(name, raising=True) | deletes an environment|
298+ | <pytest.MonkeyPatch.delenv>` | variable |
299+ | [1 ]_ | |
300+ +-----------------------------------------------+-----------------------+
301+ | :meth: `monkeypatch.syspath_prepend(path) | expands the path |
302+ | <pytest.MonkeyPatch.syspath_prepend>` | :py:data: `sys.path ` |
303+ +-----------------------------------------------+-----------------------+
304+ | :meth: `monkeypatch.chdir(path) | changes the current |
305+ | <pytest.MonkeyPatch.chdir>` | working directory |
306+ +-----------------------------------------------+-----------------------+
307+ | :meth: `monkeypatch.context() | changes the current |
308+ | <pytest.MonkeyPatch.context>` | context |
309+ +-----------------------------------------------+-----------------------+
298310
299311.. [1 ] The ``raising `` :term: `parameter ` tells pytest whether an exception
300312 should be thrown if the element is not (yet) present.
301313 .. [2 ] The ``prepend `` :term: `parameter ` of ``setenv() `` can be a character. If
302314 it is set, the value of the environment variable is changed to
303315 :samp: `{ VALUE } + prepend + { OLD_VALUE } `
304316
317+ RMonkey patching of environment variables
318+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319+
305320We can use ``monkeypatch `` to redirect the :abbr: `CLI ( Command Line Interface ) `
306321to a temporary directory for the database in two ways. Both methods require
307322knowledge of the application code. Let’s take a look at the method
@@ -392,6 +407,44 @@ environment variable :envvar:`ITEMS_DB_DIR` that can be easily patched:
392407 monkeypatch.setenv(" ITEMS_DB_DIR" , str (tmp_path))
393408 assert run_items_cli(" config" ) == str (tmp_path)
394409
410+ Monkey patching dictionaries
411+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
412+
413+ The path could also have been specified in a dictionary, for example:
414+
415+ .. code-block :: python
416+ :caption: conf.py
417+
418+ DEFAULT_CONFIG = {" database" : " items_db" }
419+
420+
421+ def create_connection (config = None ):
422+ """ Create a connection string from input or defaults."""
423+ config = config or DEFAULT_CONFIG
424+ return f " Location= { config[' database' ]} ; "
425+
426+ For testing purposes, we can change the values in the ``DEFAULT_CONFIG ``
427+ dictionary:
428+
429+ .. code-block :: python
430+ :caption: tests/ test_conf.py
431+
432+ from items import conf
433+
434+
435+ def test_connection (monkeypatch ):
436+ monkeypatch.setitem(conf.DEFAULT_CONFIG , " database" , " test_db" )
437+
438+ Alternatively, you could have defined a fixture with:
439+
440+ .. code-block :: python
441+ :caption: tests/ conftest.py
442+
443+ @pytest.fixture
444+ def mock_test_database (monkeypatch ):
445+ """ Set the DEFAULT_CONFIG database to test_db."""
446+ monkeypatch.setitem(app.DEFAULT_CONFIG , " database" , " test_db" )
447+
395448 Remaining built-in fixtures
396449---------------------------
397450
0 commit comments