当我拥有所有参数时,为什么在SQLite数据库上进行Python单元测试失败



我是python的新手,正在尝试单元测试。现在我正试图测试插入到我的SQLite数据库中,但当我在插入语句中输入时,它一直说我缺少必需的参数"author"、"maincharacter"、"place"one_answers"year"。我显然错过了什么,但就是看不见。

以下是单元测试用例:

import unittest
import backend

class MyTestCase(unittest.TestCase):
def test_db_write(self):
self.assertEqual(backend.insert("""INSERT INTO `book`", (title, author, year, maincharacter, place) 
('5', 'Harry Potter', 'JK Rowling', '2000','Harry','Closet' )"""), True)
self.assertEqual(backend.insert("""INSERT INTO `book`", (title, author, year, maincharacter, place)
('6', 'Harry Potter', 'JK Rowling', '2000','Harry','Closet')"""), True)
self.assertEqual(backend.delete("""DELETE FROM `book` WHERE id='5' """), True)
self.assertEqual(backend.delete("""DELETE FROM `book` WHERE id='6' """), True)

if __name__ == '__main__':
unittest.main()

这是错误消息:

错误追踪(最近一次通话(:文件";C: \Users\google\PycharmProjects\BookManagementSystem\BookManagerTest.py";,第8行,在test_db_write中self.assertEqual(backend.insert("插入书本值(NULL;,(标题、作者、年份、主要人物、地点(TypeError:insert((缺少4个必需的位置参数:"author"、"year"、"maincharacter"one_answers"place">

您在语句中只传递了一个字符串。该代码缺少四个单独的参数。我相信它认为你的整个字符串就是"title"参数。

看起来您的代码是为ORM设置的。这是以编程方式执行sql语句的时候。您正在将字符串编写为SQL脚本。

尝试将第一条语句更改为:

self.assertEqual(backend.insert('Harry Potter', 'JK Rowling', '2000','Harry','Closet' ), True)

最新更新