Pytest Flask应用程序AttributeError:模块“ src.api”没有属性“ test_client”

时间:2019-11-23 21:19:58

标签: python-3.x flask pytest

我正在尝试针对“ hello world”烧瓶应用程序进行基本的Pytest测试 请在下面查看src文件中的内容

api.py:

Draggable.create(".draggable", {
  onDragEnd: function() {
    if (this.hitTest("#left_drop_area")) {
      console.log("A coin was dropped in left drop area.");
    } else if (this.hitTest("#right_drop_area")) {
      console.log("A coin was dropped in right drop area.");
    } else {
      TweenLite.to(this.target, 0.5, {x: 0, y: 0, ease: Bounce.easeOut});
    }
  }
});

$("#weighleft").click(function() {
  weighLeft();
});

$("#weighright").click(function() {
  weighRight();
});

$("#equal").click(function() {
  equal();
});

function weighLeft(){

  gsap.set('#lever', {transformOrigin: "center"});

  gsap.to('#lever', {duration: 2, rotation: -10});

  gsap.to('#left_bowl', {duration: 2, y: 30});

  gsap.to('#right_bowl', {duration: 2, y: -30});

}


function weighRight(){

  gsap.set('#lever', {transformOrigin: "center"});

  gsap.to('#lever', {duration: 2, rotation: 10});

  gsap.to('#right_bowl', {duration: 2, y: 30});

  gsap.to('#left_bowl', {duration: 2, y: -30});

}

function equal(){

  gsap.set('#lever', {transformOrigin: "center"});

  gsap.to('#lever', {duration: 2, rotation: 0});

  gsap.to('#right_bowl', {duration: 2, y: 0});

  gsap.to('#left_bowl', {duration: 2, y: 0});

}

这就是我为考试编写的

test_api.py

from flask import Flask, jsonify


api = Flask(__name__)


@api.route('/')
def hello_world():
    return jsonify(message="hello world")


if __name__ == '__main__':
    api.run(debug=True)

结构

import pytest
from src import api


api.testing = True
client = api.test_client()


def test_route(client):
    response = api.get('/')
    assert response.status_code == 200

我使用my_project __init__.py src __init__.py api.py test __init__.py test_api.py 从根目录运行测试 我收到的错误消息是

python -m pytest

我真的不确定如何进行这项工作。

1 个答案:

答案 0 :(得分:2)

from src import api导入src/api.py

的模块

但是,您对api模块范围内的全局src.api对象感兴趣

from src.api import api将导入烧瓶应用程序对象,该对象应具有您正在调用的test_client方法。