Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

python - How to pytest a Flask Endpoint

I'm getting started with Flask and Pytest in order to implemente a rest service with unit test, but i'm having some troouble.

I'll like to make a simple test for my simple endpoint but i keep getting a Working outside of application context. error when running the test.

This is the end point:

from flask import jsonify, request, Blueprint
STATUS_API = Blueprint('status_api', __name__)

def get_blueprint():
    """Return the blueprint for the main app module"""
    return STATUS_API


@STATUS_API.route('/status', methods=['GET'])
def get_status():
    return jsonify({
        'status' : 'alive'
    })

And this is how I'm trying to test it (i know it should fail the test):

import pytest

from routes import status_api

def test_get_status():
    assert status_api.get_status() == ''

I'm guessing I just cant try the method with out building the whole app. But if that's the case i dont really know how to aproach this problem

question from:https://stackoverflow.com/questions/65881106/how-to-pytest-a-flask-endpoint

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you considered trying an API client/development tool? Insomnia and Postman are popular ones. Using one may be able to resolve this for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...