Django tests are not outputting Messages in template rendering

I can't seem to get a unit test to check if Messages are rendering properly in my template. As per my template, I'm not getting any output where the messages should be listed.

I'm using pytest 6.2.5 and Django 3.1.13 if that's helpful. Here is my test code:

import pytest
from django.contrib import messages
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import RequestFactory
from django.views.generic import TemplateView

pytestmark = pytest.mark.django_db

class TestBaseTemplate:
    def test_messages_middleware(self):
        request = RequestFactory().get("/")
        view = TemplateView.as_view(template_name="base.html")

        middleware1 = SessionMiddleware()

        middleware2 = MessageMiddleware()

        foobar_msg = "Hello, world!", foobar_msg)
        response = view(request)

        content = response.content.decode("utf-8")

        # This assert fails
        assert foobar_msg in content 

My message block in the template (base.html) is straight-forward (and does work in views that send messages):

  {% if messages %}
    {% for message in messages %}
    <p>{{ message }}</p>
    {% endfor %}
  {% endif %}

I can see through debugging that before you get to rendering the response that the "Hello, world!" message is being noted properly in the request via the Session/Message middleware, it's just not being rendered in the response.

Any ideas on what I'm missing? Do I need to do something with a context preprocessor before rendering the content?

Thanks in advance for any advice!

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum