How to use assertTemplateUsed method in pytest-django

Best Python code snippet using pytest-django_python

tests_views.py

Source:tests_views.py Github

copy

Full Screen

...78 2. Check if suitables templates are used.79 """80 response = self.client.get(reverse("contact"))81 self.assertEqual(response.status_code, 200)82 with self.assertTemplateUsed("webapp/contact.html"):83 render_to_string("webapp/contact.html")84 with self.assertTemplateUsed("webapp/base.html"):85 render_to_string("webapp/contact.html")86 def test_if_view_delete_return_302(self):87 """Check if "response.status_code" is "302"."""88 response = self.client.post(reverse("delete", args=[self.product.id]))89 self.assertEqual(response.status_code, 302)90 # no templates used (redirect to "myfavorites.html")91 def test_if_view_delete_return_200_with_user_logged(self):92 """Check if "response.status_code" is "200" with user logged."""93 self.client.force_login(CustomUser.objects.get_or_create("user1")[0])94 response = self.client.post(reverse("delete", args=[self.product.id]))95 self.assertEqual(response.url, "/webapp/myfavorites/")96 # no templates used (redirect to "myfavorites.html")97 def test_if_view_favorites_return_302(self):98 """Check if "response.status_code" is "302"."""99 response = self.client.post(100 reverse("favorites", args=[self.product.id])101 )102 self.assertEqual(response.status_code, 302)103 # no templates used (redirect to "myfavorites")104 def test_if_view_favorites_return_200_when_user_is_logged(self):105 """Check if "response.status_code" is "200"."""106 self.client.force_login(CustomUser.objects.get_or_create("user1")[0])107 response = self.client.post(108 reverse("favorites", args=[self.product.id])109 )110 self.assertEqual(response.url, "/webapp/myfavorites/")111 # no templates used (redirect to "myfavorites")112 def test_if_view_home_return_200_and_use_suitables_templates(self):113 """1. Check if "response.status_code" is "200".114 2. Check if suitables templates are used.115 """116 response = self.client.get(reverse("home"))117 self.assertEqual(response.status_code, 200)118 with self.assertTemplateUsed("webapp/home.html"):119 render_to_string("webapp/home.html")120 with self.assertTemplateUsed("webapp/base.html"):121 render_to_string("webapp/home.html")122 def test_if_view_legal_return_200_and_use_suitables_templates(self):123 """1. Check if "response.status_code" is "200".124 2. Check if suitables templates are used.125 """126 response = self.client.get(reverse("legal"))127 self.assertEqual(response.status_code, 200)128 with self.assertTemplateUsed("webapp/legal.html"):129 render_to_string("webapp/legal.html")130 with self.assertTemplateUsed("webapp/base.html"):131 render_to_string("webapp/legal.html")132 def test_if_view_myfavorites_return_302_and_use_suitables_templates(self):133 """1. Check if "response.status_code" is "302".134 2. Check if suitables templates are used.135 """136 response = self.client.get(reverse("myfavorites"))137 self.assertEqual(response.status_code, 302)138 with self.assertTemplateUsed("webapp/myfavorites.html"):139 render_to_string("webapp/myfavorites.html")140 with self.assertTemplateUsed("webapp/base.html"):141 render_to_string("webapp/myfavorites.html")142 with self.assertTemplateUsed("webapp/portfoliobox.html"):143 render_to_string("webapp/myfavorites.html")144 def test_if_view_myfavorites_return_200_with_user_logged(self):145 """Check if myfavorites "response.status_code" is "200" with user logged."""146 self.client.force_login(CustomUser.objects.get_or_create("user1")[0])147 response = self.client.get(reverse("myfavorites"))148 self.assertEqual(response.status_code, 200)149 def test_if_view_product_return_200_and_use_suitables_templates(self):150 """1. Check if "response.status_code" is "200".151 2. Check if suitables templates are used.152 """153 response = self.client.get(reverse("product", args=[self.product.id]))154 self.assertEqual(response.status_code, 200)155 with self.assertTemplateUsed("webapp/product.html"):156 render_to_string("webapp/product.html", {"product": self.product,})157 with self.assertTemplateUsed("webapp/base.html"):158 render_to_string("webapp/product.html", {"product": self.product,})159 def test_if_view_results_return_200_and_use_suitables_templates(self):160 """1. Check if "response.status_code" is "200".161 2. Check if suitables templates are used.162 """163 response = self.client.get(164 reverse("results"), {"query": self.product.id}165 )166 self.assertEqual(response.status_code, 200)167 with self.assertTemplateUsed("webapp/results.html"):168 render_to_string("webapp/results.html", {"product": self.product,})169 with self.assertTemplateUsed("webapp/base.html"):170 render_to_string("webapp/results.html", {"product": self.product,})171 with self.assertTemplateUsed("webapp/portfoliobox.html"):172 render_to_string("webapp/results.html", {"product": self.product,})173 self.assertTrue(len(response.context["products"]) == 1)174 self.assertEqual(len(response.context), 3)175 def test_if_view_search_return_200_and_use_suitables_templates(self):176 """1. Check if "response.status_code" is "200".177 178 2. Check if suitables templates are used.179 """180 response = self.client.get(reverse("search"), {"query": self.query})181 self.assertEqual(response.status_code, 200)182 with self.assertTemplateUsed("webapp/search.html"):183 render_to_string("webapp/search.html")184 with self.assertTemplateUsed("webapp/base.html"):185 render_to_string("webapp/search.html")186 with self.assertTemplateUsed("webapp/portfoliobox.html"):187 render_to_string("webapp/search.html")...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...87 create_user() 88 def test_tos_template(self):89 print("testing tos template used")90 response = self.client.get('/prisonbreak/terms/')91 self.assertTemplateUsed(response, 'prison_break_app/tos.html')92 def test_privacy_template(self):93 print("testing privacy template used")94 response = self.client.get('/prisonbreak/privacy/')95 self.assertTemplateUsed(response, 'prison_break_app/privacypol.html')96 def test_about_template(self):97 print("testing about template used")98 response = self.client.get('/prisonbreak/about/')99 self.assertTemplateUsed(response, 'prison_break_app/about.html')100 def test_index_template(self):101 print("testing index template used")102 response = self.client.get('/prisonbreak/')103 self.assertTemplateUsed(response, 'prison_break_app/index.html')104 def test_leaderboard_template(self):105 print("testing leaderboard template used")106 response = self.client.get('/prisonbreak/leaderboard/')107 self.assertTemplateUsed(response, 'prison_break_app/leaderboard.html')108 def test_login_template(self):109 print("testing login template used")110 response = self.client.get('/prisonbreak/login/')111 self.assertTemplateUsed(response, 'prison_break_app/login.html')112 def test_signup_template(self):113 print("testing signup template used")114 response = self.client.get('/prisonbreak/signup/')115 self.assertTemplateUsed(response, 'prison_break_app/Signup.html')116 def test_profile_accesible(self):117 print("Testing profile page is reachable")118 login(self)119 response = self.client.get('/prisonbreak/profile/')120 self.assertTemplateUsed(response, 'prison_break_app/profile.html')121class test_base_templatee_used(TestCase):122 """123 These make sure every visible page inherits from the base.html file, ensuring uniformity across site124 """125 def setUp(self):126 create_user() 127 def test_about_template(self):128 print("testing about template used")129 response = self.client.get('/prisonbreak/about/')130 self.assertTemplateUsed(response, 'prison_break_app/base.html')131 def test_index_template(self):132 print("testing index template used")133 response = self.client.get('/prisonbreak/')134 self.assertTemplateUsed(response, 'prison_break_app/base.html')135 def test_leaderboard_template(self):136 print("testing leaderboard template used")137 response = self.client.get('/prisonbreak/leaderboard/')138 self.assertTemplateUsed(response, 'prison_break_app/base.html')139 def test_login_template(self):140 print("testing login template used")141 response = self.client.get('/prisonbreak/login/')142 self.assertTemplateUsed(response, 'prison_break_app/base.html')143 def test_signup_template(self):144 print("testing signup template used")145 response = self.client.get('/prisonbreak/signup/')146 self.assertTemplateUsed(response, 'prison_break_app/base.html')147 def test_profile_accesible(self):148 print("Testing profile page is reachable")149 login(self)150 response = self.client.get('/prisonbreak/profile/')151 self.assertTemplateUsed(response, 'prison_break_app/base.html')152class test_models(TestCase):153 """154 make sure the models in database are correctly represented155 """156 def setUp(self):157 create_user()158 def test_user(self):159 user = User.objects.get(username=test_username)160 self.assertEqual(user.username, test_username)161 self.assertEqual(user.email, test_email)162 def test_leaderboard(self):163 #generate leaderboard object by visiting leaderboard page164 login(self)165 response = self.client.get('/prisonbreak/leaderboard/')...

Full Screen

Full Screen

tests_admin.py

Source:tests_admin.py Github

copy

Full Screen

...12 def test_admin_message(self):13 self.client.login(username="testadmin", password=self.password)14 resp = self.client.get("/admin/core/message/")15 self.assertEqual(resp.status_code, 200)16 self.assertTemplateUsed(resp, "admin/base.html")17 resp = self.client.get("/admin/core/message/add/")18 self.assertEqual(resp.status_code, 200)19 self.assertTemplateUsed(resp, "admin/change_form.html")20 def test_admin_profile(self):21 self.client.login(username="testadmin", password=self.password)22 resp = self.client.get("/admin/core/profile/")23 self.assertEqual(resp.status_code, 200)24 self.assertTemplateUsed(resp, "admin/base.html")25 resp = self.client.get("/admin/core/profile/add/")26 self.assertEqual(resp.status_code, 200)27 self.assertTemplateUsed(resp, "admin/change_form.html")28 resp = self.client.get("/admin/core/profile/1/change/")29 self.assertEqual(resp.status_code, 200)30 self.assertTemplateUsed(resp, "admin/change_form.html")31 def test_admin_thread(self):32 self.client.login(username="testadmin", password=self.password)33 resp = self.client.get("/admin/core/thread/")34 self.assertEqual(resp.status_code, 200)35 self.assertTemplateUsed(resp, "admin/base.html")36 resp = self.client.get("/admin/core/thread/add/")37 self.assertEqual(resp.status_code, 200)38 self.assertTemplateUsed(resp, "admin/change_form.html")39 def test_admin_unreadthread(self):40 self.client.login(username="testadmin", password=self.password)41 resp = self.client.get("/admin/core/unreadthread/")42 self.assertEqual(resp.status_code, 200)43 self.assertTemplateUsed(resp, "admin/base.html")44 resp = self.client.get("/admin/core/unreadthread/add/")45 self.assertEqual(resp.status_code, 200)46 self.assertTemplateUsed(resp, "admin/change_form.html")47 def test_admin_friendshiprequest(self):48 self.client.login(username="testadmin", password=self.password)49 resp = self.client.get("/admin/core/friendshiprequest/")50 self.assertEqual(resp.status_code, 200)51 self.assertTemplateUsed(resp, "admin/base.html")52 resp = self.client.get("/admin/core/friendshiprequest/add/")53 self.assertEqual(resp.status_code, 200)54 self.assertTemplateUsed(resp, "admin/change_form.html")55 def test_admin_friend(self):56 self.client.login(username="testadmin", password=self.password)57 resp = self.client.get("/admin/core/friend/")58 self.assertEqual(resp.status_code, 200)59 self.assertTemplateUsed(resp, "admin/base.html")60 resp = self.client.get("/admin/core/friend/add/")61 self.assertEqual(resp.status_code, 200)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-django automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful