Robbert Bos
Robbert Bos

Reputation: 13

Adding a parameter to the flask unittest

I'm trying to unittest my app in flask, but I'm getting a little bit stuck witch this one. I want to add a parameter to my app route but I don't know how to do this. Can anybody help me?

I set the vacancy id hardcoded in the test for now, but I will add a connection and a query to the database. But I first wanted to get this to work

This is the route:

@app.route('/add_application/<vacancy_id>')
def add_application(vacancy_id):
   return render_template(
        'addapplication.html')

This is my test so far:

VACANCY_ID = '5f67bc3643f71774b981ebfc'

def test_addapplicationFromVacancy(self):
    tester = app.test_client(self)
    tester.post(
        '/login',
        data=dict(username=USERNAME_USER, password=SPW_TWO),
        follow_redirects=True
    )
    response = tester.get(
        '/add_application/',
        data={'vacancy_id': VACANCY_ID},
        content_type='html/text'
    )
    print()
    self.assertEqual(response.status_code, 200)

Upvotes: 1

Views: 1116

Answers (1)

Grzegorz Redlicki
Grzegorz Redlicki

Reputation: 269

I don't know your whole application however I see at least two things which could help you here:

  1. You shouldn't use self during creating the client because this is the object of the unittest.TestCase class, not the Flask application's one. And it will be passed automatically.
    tester = app.test_client() 
    
  2. Your endpoint use the URL parameter, so the get request should look more or less like this:
    tester.get(f"/add_application/{VACANCY_ID}", ...)
    
    *I have used here f-string, feel free to change it if you are not using >=3.6 version of Python

Upvotes: 3

Related Questions