Unit testing 404使用pytest测试烧瓶应用时未找到

Unit testing 404使用pytest测试烧瓶应用时未找到,unit-testing,testing,flask,automated-tests,http-status-code-404,Unit Testing,Testing,Flask,Automated Tests,Http Status Code 404,如果我手动运行这个简单的web服务,它就会工作,但是在我的单元测试中,我会得到一个404NotFound页面作为我的响应,这会妨碍我正确地测试应用程序 正常行为: 文件夹结构: /SRC --web_application.py /UNIT_TESTS --test_wab_application.py web_application.py from flask import Flask, request, jsonify, send_from_directory from python

如果我手动运行这个简单的web服务,它就会工作,但是在我的单元测试中,我会得到一个404NotFound页面作为我的响应,这会妨碍我正确地测试应用程序

正常行为:

文件夹结构:

/SRC
--web_application.py
/UNIT_TESTS
--test_wab_application.py
web_application.py


from flask import Flask, request, jsonify, send_from_directory

from python.Greeting import Greeting

application = Flask(__name__)


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='mega_developer',
        DATABASE=os.path.join(app.instance_path, 'web_application.sqlite'),
    )
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    return app


@application.route('/greetings', methods=['GET', 'POST'])
def hello():

    # GET: for url manipulation #
    if request.method == 'GET':
        return jsonify(hello = request.args.get('name', 'world', str))
import tempfile
import pytest    
import web_application


class TestWebApplication:

    app = web_application.create_app()  # container object for test applications #

    @pytest.fixture
    def initialize_app(self):
        app = web_application.create_app()
        app.config['TESTING'] = True
        app.config['DEBUG'] = False
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DATABASE'] = tempfile.mkstemp()
        app.testing = True
        self.app = app

    def test_hello_get(self, initialize_app):
        with self.app.test_client() as client:
            response = client.get('/greetings?name=Rick Sanchez')

        assert response.status_code == 200
test_web_application.py


from flask import Flask, request, jsonify, send_from_directory

from python.Greeting import Greeting

application = Flask(__name__)


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='mega_developer',
        DATABASE=os.path.join(app.instance_path, 'web_application.sqlite'),
    )
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    return app


@application.route('/greetings', methods=['GET', 'POST'])
def hello():

    # GET: for url manipulation #
    if request.method == 'GET':
        return jsonify(hello = request.args.get('name', 'world', str))
import tempfile
import pytest    
import web_application


class TestWebApplication:

    app = web_application.create_app()  # container object for test applications #

    @pytest.fixture
    def initialize_app(self):
        app = web_application.create_app()
        app.config['TESTING'] = True
        app.config['DEBUG'] = False
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DATABASE'] = tempfile.mkstemp()
        app.testing = True
        self.app = app

    def test_hello_get(self, initialize_app):
        with self.app.test_client() as client:
            response = client.get('/greetings?name=Rick Sanchez')

        assert response.status_code == 200
测试结果(仅限最相关部分):

到目前为止,我已经测试了
test-web_application.py
中的
client.get()
方法的各种可选路由路径,包括像“/SRC/greetings?name=Rick Sanchez”或“../SRC/greetings?name=Rick Sanchez”这样的组合,但都没有不同的效果


您是否知道我可能做错了什么,或者如何从单元测试中访问web服务的功能?

我认为问题在于您正在创建两个
Flask
实例。其中一个名为
application
,您可以将其添加到
hello
路由,第二个名为
create\u app
功能。您需要使用
应用程序
实例(您将
hello
路由添加到的实例)创建一个测试客户机

是否可以导入
应用程序
,然后使用
应用程序获取
客户端
。test_client()

样品溶液:

导入pytest
从web_应用程序导入应用程序
@pytest.fixture
def client():
使用application.test_client()作为客户端:
收益客户
类TestSomething:
def测试_此(自身、客户端):
res=client.get('/greetings?name=Rick Sanchez')
assert res.status_code==200
结帐