Home > front end >  What does the empty list passed in the assert function mean?
What does the empty list passed in the assert function mean?

Time:02-25

sorry for the weird title.

I am currently learning Django, and going through their tutorial. In Part 5, it discusses test cases which I am understanding well.

However, I am unclear what the last line of the snippet means.

    def test_future_question(self):
        #questions with a pub_date in the future aren't displayed on the index page.
        create_question(question_text="Future question.", days=30)
        response = self.client.get(reverse('polls:index'))
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

Why must we pass an empty list? It doesn't make any sense to me, and I am just wanting to learn why.

Thank you!

CodePudding user response:

assertQuerysetEqual takes two arguments: a queryset, and something to compare it against.

In this case, the person who wrote this test wants to ensure that when a question is created in the future, it doesn't show up in the latest poll list. This can be tested by making sure that the queryset in the poll index is empty, i.e. testing if it's equal to an empty list.

Most assertEqual test cases work this way. Say you want to check if some value is true, you'd use self.assertEqual(some_value, True)

  • Related