How to use assertGoodView method in Django Test Plus

Best Python code snippet using django-test-plus_python

test_views.py

Source: test_views.py Github

copy

Full Screen

...22 self.assertTemplateUsed(response, 'blog/​index.html')23 def test_good_view(self):24 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs25 """26 self.assertGoodView('blog:index')27class PopularPostListViewTestCase(TestCase):28 def test_template_used(self):29 self.get('blog:popular')30 response = self.response_200()31 self.assertTemplateUsed(response, 'blog/​index.html')32 def test_good_view(self):33 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs34 """35 self.assertGoodView('blog:index')36class PopularPostListViewCBVTestCase(CBVTestCase):37 def test_get_queryset(self):38 PostFactory.create_batch(10)39 view = self.get_instance(views.BasePostListView)40 posts = view.get_queryset()41 # https:/​/​stackoverflow.com/​questions/​16058571/​comparing-querysets-in-django-testcase42 self.assertQuerysetEqual(Post.objects.all().reverse(), map(repr, posts))43class CategoryPostListViewTestCase(TestCase):44 def setUp(self):45 self.category = CategoryFactory()46 def test_template_used(self):47 self.get('blog:category_slug', slug=self.category.slug)48 self.response_200()49 self.assertTemplateUsed(self.last_response, 'blog/​category_post_list.html')50 def test_tutorial_category_template_used(self):51 tutorial_category = CategoryFactory(genre=Category.GENRE_CHOICES.tutorial)52 self.get('blog:category_slug', slug=tutorial_category.slug)53 self.assertTemplateUsed(self.last_response, 'blog/​tutorial_detail.html')54 self.assertContext('is_paginated', False)55 def test_good_views(self):56 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs57 """58 self.assertGoodView('blog:category_slug', slug=self.category.slug)59 def test_redirect_pk_url_to_slug_url(self):60 self.get('blog:category', pk=self.category.pk)61 self.response_301()62 self.get('blog:category', pk='does not exist pk')63 self.response_404()64 def test_404(self):65 self.get('blog:category_slug', slug='dose not exist slug')66 self.response_404()67class CategoryPostListViewCBVTestCase(CBVTestCase):68 def setUp(self):69 self.category = CategoryFactory(genre=Category.GENRE_CHOICES.tutorial)70 self.req = RequestFactory().get(71 self.reverse('blog:category_slug', slug=self.category.slug)72 )73 self.req.user = self.make_user()74 def test_dispatch(self):75 view = self.get_instance(views.CategoryPostListView, slug=self.category.slug, request=self.req)76 view.dispatch(self.req, )77 self.assertEqual(view.category, self.category)78 self.assertEqual(view.template_name, 'blog/​tutorial_detail.html')79 self.assertIsNone(view.paginate_by)80 def test_get_queryset(self):81 PostFactory()82 post = PostFactory(category=self.category)83 view = self.get_instance(views.CategoryPostListView, slug=self.category.slug, request=self.req)84 view.dispatch(self.req)85 self.assertQuerysetEqual(view.get_queryset(), [repr(post)])86 def test_context_data(self):87 post_list = [PostFactory(category=self.category), PostFactory(category=self.category)]88 view = self.get_instance(views.CategoryPostListView,89 initkwargs={'object_list': post_list},90 request=self.req,91 slug=self.category.slug92 )93 view.dispatch(self.req)94 context = view.get_context_data()95 self.assertEqual(context['category'], self.category)96class PostDetailViewTestCase(TestCase):97 def setUp(self):98 self.first_post = PostFactory(body='**body**')99 def test_template_used(self):100 self.get('blog:detail', pk=self.first_post.pk)101 self.response_200()102 self.assertTemplateUsed(self.last_response, 'blog/​detail.html')103 def test_good_views(self):104 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs105 """106 self.assertGoodView('blog:detail', pk=self.first_post.pk)107 def test_404(self):108 self.get('blog:detail', pk=self.first_post.pk)109 self.response_200()110 self.get('blog:detail', pk=2)111 self.response_404()112 self.get('blog:detail', pk='a')113 self.response_404()114 def test_increase_post_views(self):115 self.get('blog:detail', pk=self.first_post.pk)116 self.first_post.refresh_from_db()117 self.assertEqual(self.first_post.views, 1)118 self.get('blog:detail', pk=self.first_post.pk)119 self.first_post.refresh_from_db()120 self.assertEqual(self.first_post.views, 2)121 def test_previous_post(self):122 self.get('blog:detail', pk=self.first_post.pk)123 self.assertContext('previous_post', None)124 second_post = PostFactory()125 self.get('blog:detail', pk=second_post.pk)126 self.assertContext('previous_post', self.first_post)127 def test_next_post(self):128 self.get('blog:detail', pk=self.first_post.pk)129 self.assertContext('next_post', None)130 second_post = PostFactory()131 self.get('blog:detail', pk=self.first_post.pk)132 self.assertContext('next_post', second_post)133 def test_mark_post(self):134 self.get('blog:detail', pk=self.first_post.pk)135 self.assertHTMLEqual(self.context['post'].body, "<p><strong>body</​strong></​p>")136 def test_context(self):137 self.get('blog:detail', pk=self.first_post.pk)138 self.assertContext('post', self.first_post)139 self.assertInContext('previous_post')140 self.assertInContext('next_post')141class TutorialListViewTestCase(TestCase):142 def test_template_used(self):143 self.get('blog:tutorials')144 self.response_200()145 self.assertTemplateUsed(self.last_response, 'blog/​tutorial_list.html')146 def test_good_views(self):147 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs148 """149 self.assertGoodView('blog:tutorials')150class CategorylListViewTestCase(TestCase):151 def test_template_used(self):152 self.get('blog:categories')153 self.response_200()154 self.assertTemplateUsed(self.last_response, 'blog/​category_list.html')155 def test_good_views(self):156 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs157 """158 self.assertGoodView('blog:categories')159class PostArchivesViewTestCase(TestCase):160 def test_template_used(self):161 self.get('blog:archives')162 self.response_200()163 self.assertTemplateUsed(self.last_response, 'blog/​archives.html')164 def test_good_views(self):165 """http:/​/​django-test-plus.readthedocs.io/​en/​latest/​low_query_counts.html#assertgoodview-url-name-args-kwargs166 """...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Test Optimization for Continuous Integration

“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.

Why Agile Teams Have to Understand How to Analyze and Make adjustments

How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.

24 Testing Scenarios you should not automate with Selenium

While there is a huge demand and need to run Selenium Test Automation, the experts always suggest not to automate every possible test. Exhaustive Testing is not possible, and Automating everything is not sustainable.

The Art of Testing the Untestable

It’s strange to hear someone declare, “This can’t be tested.” In reply, I contend that everything can be tested. However, one must be pleased with the outcome of testing, which might include failure, financial loss, or personal injury. Could anything be tested when a claim is made with this understanding?

Why Agile Is Great for Your Business

Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.

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 Django Test Plus 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