Reputation: 35
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.
Upvotes: 0
Views: 800
Reputation: 35
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)
Upvotes: 1