Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

flask sqlalchemy - Pytest hangs on testing database

I'm using Pytest to test connection to database which hangs when I run it:

def test_db():
    db.create_all()
    new_comment = Comments(comment='python rocks')
    db.session.add(new_comment)
    db.session.commit()

    entry = Comments.query.all()
    assert len(entry) == 1
    db.drop_all()

The table is created successfully but I can't run select * from Comments; as it hangs too. I have to kill both windows.

How can I fix this ?

question from:https://stackoverflow.com/questions/65858775/pytest-hangs-on-testing-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This code did the trick:

from sqlalchemy.engine.reflection import Inspector
from sqlalchemy import create_engine
   
engine = create_engine('mysql://user:password@localhost/table')
inspector = Inspector.from_engine(engine)

def test_db():
   db.create_all()
   assert inspector.get_table_names()[0] == 'comments'
   db.drop_all()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...