As our application and code base increased it became necessary to write tests for each functionality. Earlier we had tests only for basic functionalities like creating an event, editing an event, but then it is very important and also beneficial if we have tests for each and every small functionality. Hence we started writing proper tests. We divivded the tests into three folder
All the API related tests were in the above one whereas the basic functionalities were in the second one. The last folder was further divided into three parts
- Admin Tests
- Super-Admin Tests
- Guest Pages
We had to test each and every functionality. For example let us look at the test file for the events. It looks like this:
class TestEvents(OpenEventViewTestCase):
def test_events_list(self):
with app.test_request_context():
url = url_for('events.index_view')
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Manage Events" in rv.data, msg=rv.data)
def test_events_create(self):
with app.test_request_context():
url = url_for('events.create_view')
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Create Event" in rv.data, msg=rv.data)
def test_events_create_post(self):
with app.test_request_context():
custom_forms = ObjectMother.get_custom_form()
url = url_for('events.create_view')
data = POST_EVENT_DATA.copy()
del data['copyright']
data['start_date'] = '07/04/2016'
data['start_time'] = '19:00'
data['end_date'] = '07/04/2016'
data['end_time'] = '22:00'
data['custom_form[name]'] = ['session_form', 'speaker_form']
data['custom_form[value]'] = [custom_forms.session_form, custom_forms.speaker_form]
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data',
data=data)
self.assertTrue(POST_EVENT_DATA['name'] in rv.data, msg=rv.data)
def test_events_create_post_publish(self):
with app.test_request_context():
url = url_for('events.create_view')
data = POST_EVENT_DATA.copy()
del data['copyright']
data['start_date'] = '07/04/2016'
data['start_time'] = '19:00'
data['end_date'] = '07/04/2016'
data['end_time'] = '22:00'
data['state'] = 'Published'
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data',
data=data)
self.assertTrue('unpublish' in rv.data, msg=rv.data)
def test_events_create_post_publish_without_location_attempt(self):
with app.test_request_context():
custom_forms = ObjectMother.get_custom_form()
url = url_for('events.create_view')
data = POST_EVENT_DATA.copy()
del data['copyright']
data['start_date'] = '07/04/2016'
data['start_time'] = '19:00'
data['end_date'] = '07/04/2016'
data['end_time'] = '22:00'
data['location_name'] = ''
data['state'] = u'Published'
data['custom_form[name]'] = ['session_form', 'speaker_form']
data['custom_form[value]'] = [custom_forms.session_form, custom_forms.speaker_form]
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data',
data=data)
self.assertTrue('To publish your event please review the highlighted fields below' in rv.data, msg=rv.data)
def test_events_edit(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
custom_forms = ObjectMother.get_custom_form(event.id)
save_to_db(custom_forms, "Custom forms saved")
url = url_for('events.edit_view', event_id=event.id)
data = POST_EVENT_DATA.copy()
del data['copyright']
data['name'] = 'EditTestName'
data['start_date'] = '07/04/2016'
data['start_time'] = '19:00'
data['end_date'] = '07/04/2016'
data['end_time'] = '22:00'
data['custom_form[name]'] = ['session_form', 'speaker_form']
data['custom_form[value]'] = [custom_forms.session_form, custom_forms.speaker_form]
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data',
data=data)
self.assertTrue('EditTestName' in rv.data, msg=rv.data)
def test_event_view(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
url = url_for('events.details_view', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("event1" in rv.data, msg=rv.data)
microlocation = ObjectMother.get_microlocation(event_id=event.id)
track = ObjectMother.get_track(event_id=event.id)
cfs = ObjectMother.get_cfs(event_id=event.id)
save_to_db(track, "Track saved")
save_to_db(microlocation, "Microlocation saved")
save_to_db(cfs, "Call for speakers saved")
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("event1" in rv.data, msg=rv.data)
def test_event_publish(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
url = url_for('events.publish_event', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
event = DataGetter.get_event(event.id)
self.assertEqual("Published", event.state, msg=event.state)
def test_event_unpublish(self):
with app.test_request_context():
event = ObjectMother.get_event()
event.state = "Published"
save_to_db(event, "Event saved")
url = url_for('events.unpublish_event', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
event = DataGetter.get_event(event.id)
self.assertEqual("Draft", event.state, msg=event.state)
def test_event_delete(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
url = url_for('events.trash_view', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Your event has been deleted" in rv.data, msg=rv.data)
def test_event_copy(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
url = url_for('events.copy_event', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Copy of event1" in rv.data, msg=rv.data)
if __name__ == '__main__':
unittest.main()
So this is the test file for the event part. As you can see we have tests for each and every small functionality
- test_events_list : Tests the list of events
- test_events_create: Tests whether the event creation page is displayed
- test_events_create_post: Tests whether the event is created on doing a POST
- test_events_create_post_publish : Tests whether the event is published on doing a POST through Publish button
- test_events_copy: Tests whether the event is copied properly or not
Thus each functionality related to an event is tested properly. Similarly not just for events but also for the other services like sessions:
import unittest
from tests.api.utils_post_data import POST_SESSION_DATA, POST_SPEAKER_DATA
from tests.object_mother import ObjectMother
from open_event import current_app as app
from open_event.helpers.data import save_to_db
from flask import url_for
from tests.views.view_test_case import OpenEventViewTestCase
class TestSessionApi(OpenEventViewTestCase):
def test_sessions_list(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
session = ObjectMother.get_session(event.id)
save_to_db(session, "Session Saved")
url = url_for('event_sessions.index_view', event_id=event.id, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Sessions" in rv.data, msg=rv.data)
self.assertTrue("test" in rv.data, msg=rv.data)
def test_session_create(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
custom_form = ObjectMother.get_custom_form(event.id)
save_to_db(custom_form, "Custom form saved")
url = url_for('event_sessions.create_view', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Create Session" in rv.data, msg=rv.data)
def test_session_create_post(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
custom_form = ObjectMother.get_custom_form(event.id)
save_to_db(custom_form, "Custom form saved")
data = POST_SESSION_DATA
data.update(POST_SPEAKER_DATA)
url = url_for('event_sessions.create_view', event_id=event.id)
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data', data=data)
self.assertTrue(data['title'] in rv.data, msg=rv.data)
def test_session_edit(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
custom_form = ObjectMother.get_custom_form(event.id)
save_to_db(custom_form, "Custom form saved")
session = ObjectMother.get_session(event.id)
save_to_db(session, "Session saved")
url = url_for('event_sessions.edit_view', event_id=event.id, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Edit Session" in rv.data, msg=rv.data)
def test_session_edit_post(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
custom_form = ObjectMother.get_custom_form(event.id)
save_to_db(custom_form, "Custom form saved")
session = ObjectMother.get_session(event.id)
save_to_db(session, "Session saved")
data = POST_SESSION_DATA
data['title'] = 'TestSession2'
url = url_for('event_sessions.edit_view', event_id=event.id, session_id=session.id)
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data', data=data)
self.assertTrue("TestSession2" in rv.data, msg=rv.data)
def test_session_accept(self):
with app.test_request_context():
session = ObjectMother.get_session()
save_to_db(session, "Session Saved")
url = url_for('event_sessions.accept_session', event_id=1, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("The session has been accepted" in rv.data, msg=rv.data)
def test_session_reject(self):
with app.test_request_context():
session = ObjectMother.get_session()
save_to_db(session, "Session Saved")
url = url_for('event_sessions.reject_session', event_id=1, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("The session has been rejected" in rv.data, msg=rv.data)
def test_session_delete(self):
with app.test_request_context():
session = ObjectMother.get_session()
save_to_db(session, "Session Saved")
url = url_for('event_sessions.delete_session', event_id=1, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("deleted" in rv.data, msg=rv.data)
def test_session_view(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event)
session = ObjectMother.get_session()
session.event_id = event.id
save_to_db(session, "Session Saved")
url = url_for('event_sessions.session_display_view', event_id=event.id, session_id=session.id)
rv = self.app.get(url, follow_redirects=True)
self.assertTrue("Short abstract" in rv.data, msg=rv.data)
def test_wrong_form_config(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event saved")
url = url_for('event_sessions.create_view', event_id=event.id)
rv = self.app.get(url, follow_redirects=True)
self.assertFalse("incorrectly configured" in rv.data, msg=rv.data)
if __name__ == '__main__':
unittest.main()
We see that there are tests for each functionality of the sessions. However these tests were simple to write. However there was problem in one aspect of writing tests. In the Event creation wizard there are steps where the sponsors, tracks, rooms are dynamically added to the event. How then should we test them. I wrote the test for the creation of sponsors in step -2
def test_events_create_post(self):
with app.test_request_context():
custom_forms = ObjectMother.get_custom_form()
url = url_for('events.create_view')
data = POST_EVENT_DATA.copy()
del data['copyright']
data['sponsors[name]'] = ['Sponsor 1', 'Sponsor 2']
data['sponsors[type]'] = ['Gold', 'Silver']
data['sponsors[url]'] = ["", ""]
data['sponsors[description]'] = ["", ""]
data['sponsors[level]'] = ["", ""]
data['start_date'] = '07/04/2016'
data['start_time'] = '19:00'
data['end_date'] = '07/04/2016'
data['end_time'] = '22:00'
data['custom_form[name]'] = ['session_form', 'speaker_form']
data['custom_form[value]'] = [custom_forms.session_form, custom_forms.speaker_form]
data = ImmutableMultiDict(data)
rv = self.app.post(url, follow_redirects=True, buffered=True, content_type='multipart/form-data',
data=data)
self.assertTrue(POST_EVENT_DATA['name'] in rv.data, msg=rv.data)
rv2 = self.app.get(url_for('events.details_view', event_id=1))
self.assertTrue(data['sponsors[name]'] in rv2.data, msg=rv2.data)
Here on importing the data dict I dynamically add two sponsors to the dict. After that I convert the dict to an Immutablemulti-dict so that the multiple sponsors can be displayed. Then I pass this dict to the event creation view via a POST request and check whether the two sponsors are present in the details page or not.
Thus our test system is developed and improving. Still as we develop more functionalities we will write more tests 🙂
You must be logged in to post a comment.