我正在尝试为我正在创建的电影网站编写一个测试用例。我想测试add_user
方法。每次我从测试方法调用add_user()
时,它都会在SQLite数据库中创建一个用户,然后作为失败返回该用户已经存在。
def add_user(self, name):
param = {'name': name}
query = text('Insert into users (name) values (:name)')
check_usr_query = text('Select name from users '
'Where name = :name')
check_usr = self._execute_query(check_usr_query, param).fetchone()
if check_usr is not None:
return 'User Already Exists in Database'
else:
with self.engine.connect() as conn:
conn.execute(query, param)
conn.commit()
return 'Successfully added user'
import pytest
from DataManager.sql_data_manager import SQLiteDataManager
dataManager = SQLiteDataManager('DataManager/movie.db')
def test_add_user():
name = 'Esth'
test_data = dataManager.add_user(name)
check_usr = dataManager.get_user_by_name(name)
if test_data == 'Successfully added user':
assert test_data == 'Successfully added user'
print(name + ' added to db')
elif test_data == 'User Already Exists in Database':
assert check_usr is not None
print(name + ' Already exists in the db')
1条答案
按热度按时间xbp102n01#
这里有几个选项:
1.使用pytest teardown fixture在测试完成时清除用户
1.在测试启动时删除用户
Esth
(如果存在)1.使用random name创建用户
1.在每次测试运行时创建新数据库