Question: I'm working in Django 1.8. I'd like to test that there are four `<li>` elements in my page. 

This is my existing `test_views.py`:

```
def test_call_view_bnf_all(self):
    response = self.client.get('/bnf/')
    self.assertEqual(response.status_code, 200)
    context_sections = response.context['sections']
    self.assertEqual(len(context_sections), 4)

    self.assertTemplateUsed(response, 'all_bnf.html')
    self.assertContains(response, '<h1>All BNF sections</h1>')

```

How can I test that there are four `<li>` elements in the page, short of providing all the raw HTML and doing an `assertContains`?

Answer: 
[`assertContains()`](https://docs.djangoproject.com/en/1.8/topics/testing/tools/#django.test.SimpleTestCase.assertContains) has a `count` option built in:

> 
> 
> ```
> assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False)
> 
> ```
> 
> If **count** is provided, **text** must occur exactly **count** times in the response.
> 
> 
> 

Therefore, you can use:

```
self.assertContains(response, '</li>', 4)

```
I'd use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) to achieve this. Something along the lines of:

```
def test_call_view_bnf_all(self):
    response = self.client.get('/bnf/')
    response_soup = BeautifulSoup(response.content)

    li_elements = response_soup.find_all('li')
    self.assertEqual(len(li_elements), 4)

```