From df38deeaa8149fff228e460fbb77d643421a032b Mon Sep 17 00:00:00 2001 From: Jay Miller Date: Sun, 9 Jul 2023 16:44:52 -0700 Subject: [PATCH 1/3] Update cookies_session docstring --- src/pytest_cookies/plugin.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/pytest_cookies/plugin.py b/src/pytest_cookies/plugin.py index 375ff4c..9d7cb00 100644 --- a/src/pytest_cookies/plugin.py +++ b/src/pytest_cookies/plugin.py @@ -157,14 +157,26 @@ def cookies(request, tmpdir, _cookiecutter_config_file): @pytest.fixture(scope="session") def cookies_session(request, tmpdir_factory, _cookiecutter_config_file): - """Yield an instance of the Cookies helper class that can be used to - generate a project from a template. + """Like `cookies` this yields an instance of the `Cookies` helper class. + This instance is scoped at the _session_ level to persist the environment + throughout the testing sessioninstead of rebuilding with each referenced test. Run cookiecutter: - result = cookies.bake(extra_context={ - 'variable1': 'value1', - 'variable2': 'value2', - }) + @pytest.fixture(scope="module") + def bakery(request, context, cookies_session): + """create a session-wide cookiecutter instance""" + result = cookies_session.bake(extra_context={ + "value_1": "value_1", + "value_2": "value_2", + }) + yield result + + Call the session in multiple tests: + def test_session_project_path(bakery): + assert bakery.project_path == "SAME/PATH/" + + def test_session_same_project_path(bakery): + assert bakery.project_path == "SAME/PATH/" """ template_dir = request.config.option.template From 51e61af8b8b331d69de7bbf9a07dc3d58b45c2a3 Mon Sep 17 00:00:00 2001 From: Jay Miller Date: Sun, 9 Jul 2023 16:53:42 -0700 Subject: [PATCH 2/3] Adds cookies_session to features.md --- docs/features.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/features.md b/docs/features.md index 26df6ff..0e750f2 100644 --- a/docs/features.md +++ b/docs/features.md @@ -1,4 +1,6 @@ -# Bake Result +# Features + +## Bake Results ``cookies.bake()`` returns a result instance with a bunch of fields that hold useful information: @@ -22,4 +24,29 @@ def test_readme(cookies): assert readme_lines == ["helloworld", "=========="] ``` -[path]: https://docs.python.org/3/library/pathlib.html#pathlib.Path \ No newline at end of file +## Cookies Session +``cookies_session`` is the same as `cookies` but the instance uses the ["session" scope]. + +This is useful when you want to run multiple tests on the same cookiecutter instance without having to setup and teardown with each test. + +```python + @pytest.fixture(scope="module") + def bakery(cookies_session): + """create a session-wide cookiecutter instance""" + result = cookies_session.bake(extra_context={ + "value_1": "value_1", + "value_2": "value_2", + }) + yield result + + def test_session_project_path(bakery): + """The first test checks for value 1""" + assert bakery.context["value_1"] == "value_1" + + def test_session_same_project_path(bakery): + """The second test checks for value 2""" + assert bakery.context["value_2"] == "value_2" +``` + +[path]: https://docs.python.org/3/library/pathlib.html#pathlib.Path +["session" scope]: https://docs.pytest.org/en/7.1.x/how-to/fixtures.html?highlight=scope#scope-sharing-fixtures-across-classes-modules-packages-or-session From f8efed10c48f4e60c492fe2bfb38f6d09caf0aa3 Mon Sep 17 00:00:00 2001 From: Jay Miller Date: Sun, 9 Jul 2023 16:54:33 -0700 Subject: [PATCH 3/3] remove other attrs from cookies_session docstring --- src/pytest_cookies/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_cookies/plugin.py b/src/pytest_cookies/plugin.py index 9d7cb00..33c2b3e 100644 --- a/src/pytest_cookies/plugin.py +++ b/src/pytest_cookies/plugin.py @@ -163,7 +163,7 @@ def cookies_session(request, tmpdir_factory, _cookiecutter_config_file): Run cookiecutter: @pytest.fixture(scope="module") - def bakery(request, context, cookies_session): + def bakery(cookies_session): """create a session-wide cookiecutter instance""" result = cookies_session.bake(extra_context={ "value_1": "value_1",