0

I'm making a web application using flask framework. To display a web page using flask, I use a render_template() function. For example:

@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
    try:
        # pdb.set_trace()
        session = get_session()
        restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
        if restaurant is not None:
            menu_items = session.query(MenuItem).filter(
                  MenuItem.restaurant_id == restaurant.id
            )
        session.close()
        if menu_items is not None:
            return render_template('menu.html', restaurant=restaurant, items=menu_items)
    except NoResultFound:
        return render_template('error.html', error_message='No such restaurant id.')

And

@app.route('/')
def welcomePage():
    return render_template('index.html')

How do I write test cases for functions like these? I'm new to testing so I wanted to write test cases for my code.

lord63. j
  • 4,500
  • 2
  • 22
  • 30
user2430771
  • 1,326
  • 4
  • 17
  • 33

1 Answers1

0

You can create a test app in your tests and call it very simply, like so:

app = create_app().test_client()
result = app.get('/')
assert('something' in result.data)

That gives you the general idea - it's pretty easy to work with. More information can be found in the Flask Testing Docs: http://flask.pocoo.org/docs/0.10/testing/

jimjkelly
  • 1,631
  • 14
  • 15