Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest in Flask test通过传递id获取单个对象_Python_Python 3.x_Unit Testing_Flask_Pytest - Fatal编程技术网

Python Pytest in Flask test通过传递id获取单个对象

Python Pytest in Flask test通过传递id获取单个对象,python,python-3.x,unit-testing,flask,pytest,Python,Python 3.x,Unit Testing,Flask,Pytest,我正在为我的FlaskAPI编写测试,并使用Python(3.7)。我有一个视图函数,通过在路由中传递其id来获取单个对象,现在我想为这个视图编写单元测试 以下是我迄今为止所做的工作: 从查看功能: @api.route("/book/<int:id>", methods=["GET", "PUT", "DELETE"]) def single_book(id): conn = db_connec

我正在为我的FlaskAPI编写测试,并使用Python(3.7)。我有一个视图函数,通过在路由中传递其id来获取单个对象,现在我想为这个视图编写单元测试

以下是我迄今为止所做的工作:

查看功能:

@api.route("/book/<int:id>", methods=["GET", "PUT", "DELETE"])
def single_book(id):
    conn = db_connection()
    cursor = conn.cursor()
    book = {}
    if request.method == "GET":
        try:
            cursor.execute("SELECT * FROM BOOK WHERE id=?", (id,))
            rows = cursor.fetchall()
            for r in rows:
                book = r
            book = {
                "id": book[0],
                "author": book[1],
                "title": book[2],
                "shortDescription": book[3],
                "thumbnailUrl": book[4],
                "status": book[5],
                "pageCount": book[6],

            }
            conn.close()
        except Error as e:
            return 'SQL Error'
        print(book)
        if book is not None:
            return jsonify(book), 200
        else:
            return "Something wrong", 404
现在,如果我运行
pytest
命令,所有其他测试都会通过,只有这个测试失败,出现以下错误:

E键错误:0

它指向这条线:


“id”:book[0],如果查询返回
None
,会发生什么?为什么你要在循环中重新定义
book
?它将只包含循环中的最后一个。我认为您希望重新定义查询或循环,但我将使用您拥有的

试试这个:

for r in rows:
    print(r) # to see what you have
    book = {
        "id": r[0],
        "author": r[1],
        "title": r[2],
        "shortDescription": r[3],
        "thumbnailUrl": r[4],
        "status": r[5],
        "pageCount": r[6],
    }
for r in rows:
    print(r) # to see what you have
    book = {
        "id": r[0],
        "author": r[1],
        "title": r[2],
        "shortDescription": r[3],
        "thumbnailUrl": r[4],
        "status": r[5],
        "pageCount": r[6],
    }