se2p / pynguin

The PYthoN General UnIt Test geNerator is a test-generation tool for Python
https://www.pynguin.eu
MIT License
1.22k stars 74 forks source link

How to use pynguin for flask app (with get and post method) #41

Closed shanusharma06 closed 6 months ago

shanusharma06 commented 1 year ago

I want to generate unit test cases for the flask app using pynguin which is containing get and post methods. How can we do this?

stephanlukasczyk commented 1 year ago

Disclaimer: I do not know much about flask and have never used it.

From what I understand from a quick look at the documentation, it should be possible to run Pynguin on the code that implements the app's endpoint (e.g. the methods annotated with @app.route). Pynguin might be able to come up with test cases here. The same holds for code that implements the general application model.

What Pynguin cannot do for you is creating HTTP requests that you could sent to a running flask app. While this might be a nice problem, it is out of the scope of what we want to do with Pynguin.

If you could provide a minimal flask app, we could play around with it a bit.

shanusharma06 commented 1 year ago

Thank you @stephanlukasczyk for the quick response.

I tried to run pynguin for the below module but did not get any test cases (it is restarting continuously). Attaching a sample flask code here which contains get and post methods both :

Requirement : Flask

from flask import Flask, request, jsonify
import json  

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    """
    Base path to check if the service is running
    """
    return (str('Service is running')), 200

@app.route('/test', methods=['POST'])
def get_inference():
    """
    sample test endpoint
    """
    ## input
    input_json = json.loads(request.data)

    ## output
    response = jsonify(input_json)
    response.status_code = 200
    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

It would be beneficial if pynguin could generate unit test cases for the flask app.

stephanlukasczyk commented 1 year ago

Sorry for the late reply. I've just played around and I was able to at least create some output. I would argue that those are maybe not the kind of test cases you'd expect but at least there is some output.

What did I do? I ran Pynguin in its latest released version (0.31.0) with the command line

pynguin --module-name app --project-path . --output-path . --maximum-search-time 60 -v

Please note that the app.py contains your provided example code snippet, I limit the time for the search algorithm to 60 seconds and I increased the log level to get at least some output.

The resulting test file looks like the following:

# Test cases automatically generated by Pynguin (https://github.com/se2p/pynguin).
# Please check them before you use them.
import pytest
import app as module_0

@pytest.mark.xfail(strict=True)
def test_case_0():
    module_0.get_inference()

def test_case_1():
    var_0 = module_0.index()

One of the reasons I noticed is that when Pynguin tried to execute the subject, I ran into the issue that it could not bind to port 5000 because that is bound to something different on my machine here.