loading

Test Driven Development with Django

I have a few suggestions for how you can improve your tests to better guard against possible issues as well as make them more maintainable.Always call super().setUp() if you override setUp in your tests.This may not seem like something which is required, but sometimes you'll use a custom test class which has some custom logic in the setUp method (usually involving the database). It's caught me a few times and I've ended up having to remove a bunch of test objects from my database.If you aren't going to hard-code your test urls, don't hard code where you expect them to redirect to.test_create_view_denies_anonymous tests that unauthenticated users will get redirected, but you're hard-coding the url that it should be redirected to. You can safely rely on Django's reverse not messing up, which will allow you to build out the url it's redirected to while still handling you renaming the url in the future.Don't hard-code your username and password in your test factory.This was mentioned already, but you should instead be setting the password for the user within the setUp for your test class, or at the start of the test itself. This allows you to create multiple users with different names, all within the same test.Always assert your assumptionsIn test_create_minimal_for_user you are asserting that the user is redirected to the dashboard, which I'm assuming should mean the reservation was created. And then you call Reservation.objects.first(), which will either give you a Reservation or will give you None without raising an error. You then continue on, assuming that the reservation was returned, to test the properties of the reservation.You should instead be asserting that a Reservation exists before you try to retrieve it. Also, you can use .get() on any manager as long as there is a single object, which you appear to be assuming.

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!')

Test Driven Development with Django 1

GET IN TOUCH WITH Us
recommended articles
Related Blogs Info Center
This addresses the third question, which was about the recall of any drugs that had been misrepresented by company-sponsored research. Paroxetine has not been recall...
So $fracmnfracnmfracm^2n^2mn$ an integer. This means $$m^2-n^2 mod mn $$ $$m^2n^2kmn$$ for some integer $k$.If $k1$, then $(mn)(m-n)0$ so $mn$ or $m-n$.With some cal...
James Foster of Exis Web did an interesting A/B test on the hamburger icon:Tests on mobile showed a difference, though not all that significant, when the icon was us...
Insights network launched the install blockchain, which is one of the most unique blockchain technology use cases seen in the world. In this article, we will discuss...
Reasons why we should impose tariffs on goods from China...?I really see no good so I will give you things to prepare for. 1.) Trade is good for everyone 2.) This wo...
On March 8, the new surface Pro X was officially launched in China. As Microsoft's lightest, strongest and most connected surface product so far, surface Pro x adopt...
Well there is a lot of empty space in an atom.They actually say that if you take all the space out of atoms then the Empire State building would shrink to the size o...
Beijing, August 4, 2011 - Texas Instruments (TI) recently announced the launch of a 50 Ma, 60 V synchronous swift â„¢ The step-down regulator can pr...
This is the wiki text version of lesson 1 and lesson 2 of arm bare metal phase 1 enhanced version.Why do you want to learn SCM without a future?Because it's a good e...
Since the outbreak of the epidemic, health codes, online diagnosis and treatment, infrared thermometers, etc. have been gradually known by the public. They share a "...
no data
Contact Us
Contact Person: AI customer service
Tel: +86 0757-23368757
Address: No.4 Of Xingye Road, Shafu Industrial Park, Longjiang Town, Shunde District, Foshan 
WHATSAPP: +86-15919090839
WECHAT: w87735492
Better Touch Better Business
Contact Sales at JuJiao.
Call Us
+86 0757-23368757
  
Copyright © 2024 FOSHAN SAN DUN Furniture CO., LTD. | All Rights Reserved |Sitemap
Customer service
detect