-
-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problems with pytest supporting in 0.19.0 #1110
Comments
Did you try the code that's in the documentation? I adapted it a bit, and this works for me:
|
We went with this workaround till the issue is fixed @pytest.fixture(scope="session", autouse=True)
def db(request: SubRequest) -> None:
config = getTestDBConfig(app_label="models", modules=DB_MODELS)
async def _init_db() -> None:
await Tortoise.init(config)
try:
await Tortoise._drop_databases()
except (DBConnectionError, OperationalError): # pragma: nocoverage
pass
await Tortoise.init(config, _create_db=True)
await Tortoise.generate_schemas(safe=False)
loop = asyncio.get_event_loop()
loop.run_until_complete(_init_db())
request.addfinalizer(lambda: loop.run_until_complete(Tortoise._drop_databases())) |
same issue here, I guess there's something to do with this PR #1001? Downgrading to 0.18.1 fixes this issue. |
Same here |
I have the same issue |
Any updates on this? |
I see 0.19.3 has partial solution in it, so for me workaround is as follows:
|
I spent the last two days on a tiny app with one table to reproducing this, and I just cannot work out how tortoise orm, pytest and fastapi I supposed to work together. The default suggestion of creating an All the solutions I've seen don't seem to work - it just seems like such a foot gun, this seems way too complicated and the async nature means working out the order of things is very painful. I wonder if seperating the place where the There are a lot of people hitting this issue if you search for the similar search terms - maybe there is a design issue making this hard, when it really should be something you can't get wrong easily. |
@stuaxo I found fastAPI and tortoise example (https://tortoise.github.io/examples/fastapi.html) that uses pytest to run the test. It seems to work fine for me. |
Thanks for posting that - I was many many hours into a "3 hour" exercise I had taken on for an interview when I hit the wall on this; hopefully someone will find that link helpful (or me in the future). |
For 0.20.0 I struggled to find a workaround that actually works. @sodrian's came closest, but I still got an 'Event loop is closed' error. In my env this does the trick: import asyncio
from tortoise import Tortoise
from tortoise.contrib.test import _init_db, getDBConfig
# https://stackoverflow.com/questions/61022713/pytest-asyncio-has-a-closed-event-loop-but-only-when-running-all-tests
@pytest.fixture(scope="session")
def event_loop():
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
def in_memory_db(request, event_loop):
config = getDBConfig(app_label="models", modules=["models.models"])
event_loop.run_until_complete(_init_db(config))
request.addfinalizer(lambda: event_loop.run_until_complete(Tortoise._drop_databases()))
# actual test, using pytest-asyncio plugin
@pytest.mark.asyncio
async def test_create(in_memory_db):
await MyModel(....).save()
mm = await MyModel.first()
assert mm is not None |
Thanks to @kleynajn, here is what mine became:
|
I want to use pytest via fixture like this https://tortoise-orm.readthedocs.io/en/latest/contrib/unittest.html#py-test, but i get
ConfigurationError
in
conftest.py
:simple test:
But after running test i get:
I'm getting it only in
v0.19.0
, for examplev0.18.1
works correctlyThe text was updated successfully, but these errors were encountered: