Define fixture 'restore_sqlite_database' that will be automatically used to restore the database before starting each test

Bug540-XiongJiaming
Lan Hui 2024-04-09 20:06:30 +08:00
parent 4f91659713
commit 77a3adb546
1 changed files with 15 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import pytest
import sqlite3
from selenium import webdriver
@pytest.fixture
@ -9,3 +10,17 @@ def URL():
@pytest.fixture
def driver():
return webdriver.Edge() # uncomment this line if you wish to run the test on your laptop
@pytest.fixture(autouse=True)
def restore_sqlite_database():
'''
Automatically restore SQLite database file app/db/wordfreqapp.db
using SQL statements from app/static/wordfreqapp.sql
'''
con = sqlite3.connect('../db/wordfreqapp.db')
with con:
con.executescript('DROP TABLE IF EXISTS user;')
con.executescript('DROP TABLE IF EXISTS article;')
con.executescript(open('../static/wordfreqapp.sql', encoding='utf8').read())
con.close()