This will ensure that you will either get a Reservation back, or your test will fail (not error out) because there were no reservations created. This also handles the case where multiple reservations were created for some reason.Avoid hard-coding dates in your testsYou managed to pick dates that were far into the future, but usually people forget to do that. In my experience, we had a bunch of tests which were hard-coded to fail in 2 years and 2 months, and we never touched those tests to update the dates until everything started failing on May 1st, 2015.You can avoid hard-coding dates in your tests by generating relative dates in the future using the timedelta module
I'm learning TDD with Django and currently I'm testing my CreateView class. I feel like I'm not using best practices but am not sure on how to improve my code.
test_reservation_create.py
from django.test import TestCase
from django.urls import reverse
from reservations.models import Reservation
from src.factories import UserFactory, GroupFactory, ProfileFactory, TagFactory
class TestCreateReservation(TestCase): def setUp(self): persona UserFactory(groups(GroupFactory.create(),)) ProfileFactory(userpersona, ) persona.profile.tags.add(TagFactory()) def test_create_view_denies_anonymous(self): response self.client.get(reverse('reservation-create'), followTrue) self.assertRedirects(response, '/login?next/r/create/') def test_loads_template_for_user(self): self.client.login(username'john', password'defaultpassword') response self.client.get(reverse('reservation-create')) self.assertEquals(response.status_code, 200) def test_create_blank_for_user(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', response self.client.post(reverse('reservation-create'), datadata) self.assertFormError(response,'form','passenger_name','This field is required.') self.assertFormError(response,'form','passenger_lastname','This field is required.') self.assertFormError(response,'form','service_date','This field is required.') self.assertFormError(response,'form','author_alias','This field is required.') def test_formset_validation(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertContains(response,'You must have specify both place1 and place2') def test_create_minimal_for_user(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertRedirects(response, reverse('dashboard')) reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_create_minimal_for_user_and_new(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'save_and_new':'Submit', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertRedirects(response, reverse('reservation-create')) reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_create_minimal_for_user_and_clone(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'save_and_clone':'Submit', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertEquals(response.status_code, 200) self.assertContains(response, 'John') reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_invalid_date_in_past(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date': '02/07/2010', response self.client.post(reverse('reservation-create'), datadata) self.assertFormError(response, 'form', 'service_date', 'Date can't be in the past!')
·OTHER ANSWER:
I'm learning TDD with Django and currently I'm testing my CreateView class. I feel like I'm not using best practices but am not sure on how to improve my code.
test_reservation_create.py
from django.test import TestCase
from django.urls import reverse
from reservations.models import Reservation
from src.factories import UserFactory, GroupFactory, ProfileFactory, TagFactory
class TestCreateReservation(TestCase): def setUp(self): persona UserFactory(groups(GroupFactory.create(),)) ProfileFactory(userpersona, ) persona.profile.tags.add(TagFactory()) def test_create_view_denies_anonymous(self): response self.client.get(reverse('reservation-create'), followTrue) self.assertRedirects(response, '/login?next/r/create/') def test_loads_template_for_user(self): self.client.login(username'john', password'defaultpassword') response self.client.get(reverse('reservation-create')) self.assertEquals(response.status_code, 200) def test_create_blank_for_user(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', response self.client.post(reverse('reservation-create'), datadata) self.assertFormError(response,'form','passenger_name','This field is required.') self.assertFormError(response,'form','passenger_lastname','This field is required.') self.assertFormError(response,'form','service_date','This field is required.') self.assertFormError(response,'form','author_alias','This field is required.') def test_formset_validation(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertContains(response,'You must have specify both place1 and place2') def test_create_minimal_for_user(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertRedirects(response, reverse('dashboard')) reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_create_minimal_for_user_and_new(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'save_and_new':'Submit', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertRedirects(response, reverse('reservation-create')) reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_create_minimal_for_user_and_clone(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date':'02/07/2027', 'passenger_name':'John', 'passenger_lastname':'Doe', 'stops-0-time': '11:00', 'stops-0-place':'Place1', 'stops-1-place': 'Place2', 'service_type':'BUSINESS', 'status':0, 'payment_options':'CASH', 'save_and_clone':'Submit', 'amount':'0.00', 'author_alias':'Faker', 'vehicle':'CAR', 'pax_number':1 response self.client.post(reverse('reservation-create'), datadata, followTrue) self.assertEquals(response.status_code, 200) self.assertContains(response, 'John') reservation Reservation.objects.first() self.assertEquals(reservation.service_date.strftime('%Y-%m-%d'),'2027-02-07') self.assertEquals(reservation.passenger_name,'John') self.assertEquals(reservation.passenger_lastname,'Doe') self.assertEquals(reservation.service_type,'BUSINESS') self.assertEquals(reservation.status,0) self.assertEquals(reservation.author_alias,'Faker') self.assertEquals(reservation.payment_options,'CASH') self.assertEquals(reservation.amount,0.00) self.assertEquals(reservation.vehicle,'CAR') self.assertEquals(reservation.pax_number,1) self.assertEquals(reservation.stops.first().place, 'Place1') self.assertEquals(reservation.stops.last().place, 'Place2') self.assertEquals(reservation.stops.first().time.strftime('%H:%M'), '11:00') def test_invalid_date_in_past(self): self.client.login(username'john', password'defaultpassword') data 'stops-TOTAL_FORMS': u'2', 'stops-INITIAL_FORMS': u'0', 'stops-MIN_NUM_FORMS': u'0', 'stops-MAX_NUM_FORMS': u'1000', 'stops-0-id': u'', 'service_date': '02/07/2010', response self.client.post(reverse('reservation-create'), datadata) self.assertFormError(response, 'form', 'service_date', 'Date can't be in the past!')