From 1c4bea425df8605d9545674e922a580093998b0a Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 5 Dec 2020 20:54:00 -0500 Subject: [PATCH 1/6] Document how to modify environment table after tests finish --- README.rst | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index fcd4fa0a..a9e345cc 100644 --- a/README.rst +++ b/README.rst @@ -102,12 +102,29 @@ Environment The *Environment* section is provided by the `pytest-metadata `_ plugin, and can be accessed -via the :code:`pytest_configure` hook: +via the :code:`pytest_configure` and :code:`pytest_unconfigure` hooks: + +To modify the *Environment* section **before** tests are run, use :code:`pytest_configure`: .. code-block:: python def pytest_configure(config): config._metadata["foo"] = "bar" + +To modify the *Environment* section **after** tests are run, use :code:`pytest_unconfigure`: + +.. code-block:: python + + import pytest + + @pytest.hookimpl(tryfirst=True) + def pytest_sessionfinish(session, exitstatus): + session.config._metadata["Average Response Time"] = calculate_avg_response(session) + +Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ +is important, as this ensures your `pytest_sessionfinish` is run **before** any other plugins ( including `pytest-html` and `pytest-metadata` ) are run. +If this line is omitted, then the *Environment* table will **not** be updated since the `pytest_sessionfinish` of the plugins will execute first, and thus +not pick up your change. The generated table will be sorted alphabetically unless the metadata is a :code:`collections.OrderedDict`. From a4c800b971203525cd96e044657e1cbc97dcea77 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 5 Dec 2020 21:02:16 -0500 Subject: [PATCH 2/6] Fix typos --- README.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index a9e345cc..90a33f0e 100644 --- a/README.rst +++ b/README.rst @@ -102,7 +102,7 @@ Environment The *Environment* section is provided by the `pytest-metadata `_ plugin, and can be accessed -via the :code:`pytest_configure` and :code:`pytest_unconfigure` hooks: +via the :code:`pytest_configure` and :code:`pytest_sessionfinish` hooks: To modify the *Environment* section **before** tests are run, use :code:`pytest_configure`: @@ -111,7 +111,7 @@ To modify the *Environment* section **before** tests are run, use :code:`pytest_ def pytest_configure(config): config._metadata["foo"] = "bar" -To modify the *Environment* section **after** tests are run, use :code:`pytest_unconfigure`: +To modify the *Environment* section **after** tests are run, use :code:`pytest_sessionfinish`: .. code-block:: python @@ -122,9 +122,8 @@ To modify the *Environment* section **after** tests are run, use :code:`pytest_u session.config._metadata["Average Response Time"] = calculate_avg_response(session) Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ -is important, as this ensures your `pytest_sessionfinish` is run **before** any other plugins ( including `pytest-html` and `pytest-metadata` ) are run. -If this line is omitted, then the *Environment* table will **not** be updated since the `pytest_sessionfinish` of the plugins will execute first, and thus -not pick up your change. +is important, as this ensures your :code:`pytest_sessionfinish` is run **before** any other plugins ( including :code:`pytest-html` and :code:`pytest-metadata` ) +are run. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the plugins will execute first, and thus not pick up your change. The generated table will be sorted alphabetically unless the metadata is a :code:`collections.OrderedDict`. From b4bd8b305189d154ed5756c8054bf79ccbf52754 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 5 Dec 2020 21:05:09 -0500 Subject: [PATCH 3/6] better method name in docs --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 90a33f0e..c22fcd0a 100644 --- a/README.rst +++ b/README.rst @@ -119,7 +119,7 @@ To modify the *Environment* section **after** tests are run, use :code:`pytest_s @pytest.hookimpl(tryfirst=True) def pytest_sessionfinish(session, exitstatus): - session.config._metadata["Average Response Time"] = calculate_avg_response(session) + session.config._metadata["Average Response Time"] = calculate_avg_response_time(session) Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ is important, as this ensures your :code:`pytest_sessionfinish` is run **before** any other plugins ( including :code:`pytest-html` and :code:`pytest-metadata` ) From 6974b2a04c231e6f8086a7599aafe7c4242dbf00 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 5 Dec 2020 21:08:48 -0500 Subject: [PATCH 4/6] Update README.rst --- README.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index c22fcd0a..cce12bbd 100644 --- a/README.rst +++ b/README.rst @@ -122,8 +122,9 @@ To modify the *Environment* section **after** tests are run, use :code:`pytest_s session.config._metadata["Average Response Time"] = calculate_avg_response_time(session) Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ -is important, as this ensures your :code:`pytest_sessionfinish` is run **before** any other plugins ( including :code:`pytest-html` and :code:`pytest-metadata` ) -are run. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the plugins will execute first, and thus not pick up your change. +is important, as this ensures your :code:`pytest_sessionfinish` method is run **before** any other plugins ( including :code:`pytest-html` and +:code:`pytest-metadata` ) run theirs. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the +plugins will execute first, and thus not pick up your change. The generated table will be sorted alphabetically unless the metadata is a :code:`collections.OrderedDict`. From 8aaace0e27e2c3a20bc76239e28e92b69a92f3c9 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sat, 5 Dec 2020 21:16:31 -0500 Subject: [PATCH 5/6] apply linting --- README.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index cce12bbd..87167f23 100644 --- a/README.rst +++ b/README.rst @@ -110,20 +110,23 @@ To modify the *Environment* section **before** tests are run, use :code:`pytest_ def pytest_configure(config): config._metadata["foo"] = "bar" - + To modify the *Environment* section **after** tests are run, use :code:`pytest_sessionfinish`: .. code-block:: python import pytest + @pytest.hookimpl(tryfirst=True) def pytest_sessionfinish(session, exitstatus): - session.config._metadata["Average Response Time"] = calculate_avg_response_time(session) + session.config._metadata["Average Response Time"] = calculate_avg_response_time( + session + ) Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ is important, as this ensures your :code:`pytest_sessionfinish` method is run **before** any other plugins ( including :code:`pytest-html` and -:code:`pytest-metadata` ) run theirs. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the +:code:`pytest-metadata` ) run theirs. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the plugins will execute first, and thus not pick up your change. The generated table will be sorted alphabetically unless the metadata is a From 4f394fe31b79d3c0e642152dbb7053141f9e4b4f Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Wed, 9 Dec 2020 09:43:15 -0500 Subject: [PATCH 6/6] move change to read the docs docs and apply feedback --- README.rst | 22 +--------------------- docs/user_guide.rst | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index 87167f23..fcd4fa0a 100644 --- a/README.rst +++ b/README.rst @@ -102,33 +102,13 @@ Environment The *Environment* section is provided by the `pytest-metadata `_ plugin, and can be accessed -via the :code:`pytest_configure` and :code:`pytest_sessionfinish` hooks: - -To modify the *Environment* section **before** tests are run, use :code:`pytest_configure`: +via the :code:`pytest_configure` hook: .. code-block:: python def pytest_configure(config): config._metadata["foo"] = "bar" -To modify the *Environment* section **after** tests are run, use :code:`pytest_sessionfinish`: - -.. code-block:: python - - import pytest - - - @pytest.hookimpl(tryfirst=True) - def pytest_sessionfinish(session, exitstatus): - session.config._metadata["Average Response Time"] = calculate_avg_response_time( - session - ) - -Note that in the above example `@pytest.hookimpl(tryfirst=True) `_ -is important, as this ensures your :code:`pytest_sessionfinish` method is run **before** any other plugins ( including :code:`pytest-html` and -:code:`pytest-metadata` ) run theirs. If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the -plugins will execute first, and thus not pick up your change. - The generated table will be sorted alphabetically unless the metadata is a :code:`collections.OrderedDict`. diff --git a/docs/user_guide.rst b/docs/user_guide.rst index 39145fe4..072c714c 100644 --- a/docs/user_guide.rst +++ b/docs/user_guide.rst @@ -55,13 +55,31 @@ Environment ~~~~~~~~~~~ The *Environment* section is provided by the `pytest-metadata`_ plugin, and can be accessed -via the :code:`pytest_configure` hook: +via the :code:`pytest_configure` and :code:`pytest_sessionfinish` hooks: + +To modify the *Environment* section **before** tests are run, use :code:`pytest_configure`: .. code-block:: python def pytest_configure(config): config._metadata["foo"] = "bar" +To modify the *Environment* section **after** tests are run, use :code:`pytest_sessionfinish`: + +.. code-block:: python + + import pytest + + + @pytest.hookimpl(tryfirst=True) + def pytest_sessionfinish(session, exitstatus): + session.config._metadata["foo"] = "bar" + +Note that in the above example `@pytest.hookimpl(tryfirst=True)`_ is important, as this ensures that a best effort attempt is made to run your +:code:`pytest_sessionfinish` **before** any other plugins ( including :code:`pytest-html` and :code:`pytest-metadata` ) run theirs. +If this line is omitted, then the *Environment* table will **not** be updated since the :code:`pytest_sessionfinish` of the plugins will execute first, +and thus not pick up your change. + The generated table will be sorted alphabetically unless the metadata is a :code:`collections.OrderedDict`. @@ -248,6 +266,7 @@ Below is an example of a :code:`conftest.py` file setting :code:`duration_format **NOTE**: Milliseconds are always displayed with a precision of 2 +.. _@pytest.hookimpl(tryfirst=True): https://docs.pytest.org/en/stable/writing_plugins.html#hook-function-ordering-call-example .. _ansi2html: https://pypi.python.org/pypi/ansi2html/ .. _Content Security Policy (CSP): https://developer.mozilla.org/docs/Web/Security/CSP/ .. _JSON: https://json.org/