From 77a3adb5465b968942d7e8b5fa4f93c2f5ced782 Mon Sep 17 00:00:00 2001 From: Lan Hui <1348141770@qq.com> Date: Tue, 9 Apr 2024 20:06:30 +0800 Subject: [PATCH] Define fixture 'restore_sqlite_database' that will be automatically used to restore the database before starting each test --- app/test/conftest.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/test/conftest.py b/app/test/conftest.py index 460480d..fe64e5c 100644 --- a/app/test/conftest.py +++ b/app/test/conftest.py @@ -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()