简体   繁体   中英

How can I fix my unittest 405 != 200 error when testing my flask application?

First time using unittests and trying to run and test my flask app. I have the basic things in my Flask app.py:

from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
port = 5000
@app.route('/',methods=['POST'])
def main():
    data = request.get_json()

And in my test.py I have:

from app import app
class testing(unittest.TestCase):
    def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DEBUG'] = False
        self.app = app.test_client()
        self.assertEqual(app.debug, False)

    def tearDown(self):
        pass

    def test_main_page(self):
        response = self.app.get('/', follow_redirects=True)
        self.assertEqual(response.status_code, 200)

Keep getting the assertation error 405 != 200 I ran python -m unittest test.py Didn't run. So I tried running python -m flask run and then running the unittest in a split terminal but still didnt work. Still new to this so not sure if I'm just missing pieces of code. I have other unit tests in the code that run and pass but they just check functions and not the actual flask app.

The fix for a 405 error is that the route is expecting a POST or GET. Depending on your flask app make sure you are using the correct self.app.post('/',follow_redirects=True) or self.app.get('/',follow_redirects=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM