How to use pytest.fixture in pytest.hookspec.pytest_configure #9580
-
ENV: CentOS Linux release 7.8.2003 (Core)
Python 3.6.8
pytest 6.2.5
attrs 20.3.0
iniconfig 1.1.1
packaging 21.3
pluggy 1.0.0
py 1.11.0
tomli 1.2.3
colorama 0.4.4
importlib-metadata4.8.3 Hi, I want to store some global variables shared in difference test case. So I found this question which mention this hook Actually I want to create some data to database and then bind to pytest, I try to use another fixture in Here is my snippet in import pytest
from some_models import User
@pytest.fixture(scope="function")
def db_session(db, request):
"""Creates a new database session for a test."""
# It's a bussiness code written by colleague, I have to hide it without permission.
# `db` is a :class:`flask_sqlalchemy.SQLAlchemy` instance.
# This fixture yield a db.session, so in test function, we call it like that: `db_session.add`.
def pytest_configure(config):
user = User(name='admin')
db_session.add(user)
print('config>>>>>>>>>', config)
pytest.aio_user = user When a I run a test function, it raise: (envaio) [airflow@aioserver-30151 aio]$ pytest -s /home/airflow/aio/tests/api/v2/event/test_create_event.py
[2022-01-30 17:34:50,883] INFO [root.<module>:36] flask env:production
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/_pytest/main.py", line 265, in wrap_session
INTERNALERROR> config._do_configure()
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/_pytest/config/__init__.py", line 982, in _do_configure
INTERNALERROR> self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/pluggy/_hooks.py", line 277, in call_historic
INTERNALERROR> res = self._hookexec(self.name, self.get_hookimpls(), kwargs, False)
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "/home/airflow/envaio/lib64/python3.6/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "/home/airflow/aio/tests/conftest.py", line 149, in pytest_configure
INTERNALERROR> db_session.add(user)
INTERNALERROR> AttributeError: 'function' object has no attribute 'add' Q1: What is the best practice to share global variables between tests? Q2: If my way is right, how to implement it? Q3: What is Thank you for watching this question. Any help is appreciative. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
If you want to share data between tests, fixtures are usually used, not hooks. When databases are involved usually users define a session-scoped fixture which setups the database, and function-scoped fixtures which setup the database for some tests (usually with a rollback mechanism so tests using that fixture leave the database pristine). Some articles describing the pattern:
Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
What about a use case where I would like to allow specification of custom log directory path based on a fixture value that in turns gets populated based on CLI option input? Fixture is used as one fixture for all test suite to write test artifacts to (Selenium screenshots, container logs, pickle dumps, packet capture), but also want to dump pytest's logs to it too but with with increased verbosity, so stdout gets INFO, but file logs are DEBUG there is an example of that in one of github discussions about having better documentation for
would be nice to have access to artifact_dir fixture |
Beta Was this translation helpful? Give feedback.
If you want to share data between tests, fixtures are usually used, not hooks.
When databases are involved usually users define a session-scoped fixture which setups the database, and function-scoped fixtures which setup the database for some tests (usually with a rollback mechanism so tests using that fixture leave the database pristine).
Some articles describing the pattern:
Hope this helps!