Flask session--测试用例失败

Flask session-- Test case failure

正在处理在会话中存储登录对象。解决方案在本地机器上运行良好。然而,测试用例在黑客等级环境中失败。 遵循的步骤:

user.html

<!DOCTYPE html>
<html lang="en">
<body>
{% block content %}
<!-- write a condition to check if the user in session or not with appropriate response and redirect routes-->
{% if login %}
    Logged in as {{ session['username'] }} 
    <a href="/logout">logout</a>
    {%else%}
    You are not logged in
    <a href="/login">login</a>.
    {%endif %}

{% endblock %}
</body>
</html>

test.py

 def test_login_and_user_page(self):
        soup1 = self.get_soup1()
        msg = soup1.find('p', {'id': 'a'})
        link = soup1.find('a')
        assert msg.text == 'Logged in as admin'
        assert link['href'] == '/logout'

        soup3 = self.get_soup3()
        assert soup3.text == "You've been logged out successfully!"

        soup4 = self.get_soup4()
        msg = soup4.find('p', {'id': 'a'})
        assert msg.text == 'Logged in as user'

    def test_user_page(self):
        soup2 = self.get_soup2()
        msg = soup2.find('p', {'id': 'b'})
        link = soup2.find('a')
        assert msg.text == 'You are not logged in'
        assert link['href'] == '/login'

测试失败:

============================================== FAILURES ==============================================
_______________________________ Test_Webpage.test_login_and_user_page ________________________________

self = <tests.Test_Webpage object at 0x7f8baae8c198>

    def test_login_and_user_page(self):
        soup1 = self.get_soup1()
        msg = soup1.find('p', {'id': 'a'})
        link = soup1.find('a')
>       assert msg.text == 'Logged in as admin'
E       AttributeError: 'NoneType' object has no attribute 'text'

正如我在评论中所写,测试用例尝试过滤 <p> 标签以获取网页上的输出:

msg = soup2.find('p', {'id': 'a'}) # first testcase
msg = soup2.find('p', {'id': 'b'}) # second testcase

但是在您的 html 模板上,您只是写下了文本:

Logged in as {{ session['username'] }} 
You are not logged in

如果您将这些消息放入 <p> 标签并给它们正确的 id 它应该可以工作。

<p id="a">Logged in as {{ session['username'] }}</p>
<p id="b">You are not logged in</p>