我具有以下build_settings_message函数,我有点困惑应该如何为其编写单元测试。我需要检查哪些方面?

def build_settings_message(team_id):
    team = SlackTeam.objects.find_by_id(team_id)
    domain = Site.objects.get_current().domain

    attachments = [
        _build_manage_admins(),
        _build_checks_available(team, domain)
    ]

    return {
        'text': "Here is my settings page",
        'attachments': attachments
    }


def _build_manage_admins():
    return {
        "fallback": "Manage admins",
        "color": "#85cdff",
        "callback_id": "admins_controls",
        "title": "Admins",
        "footer": "Users that could remove and edit any checks ",
        "actions": [
            {
                "name": "manage",
                "text": ":key: Manage Admins",
                "type": "button",
                "value": "manage"
            }
        ]
    }


def _build_checks_available(team, domain):
    return {
        "title": "Items available",
        "footer": ("You have got *{} of {}* items for "
                   "check *available*.").format(
                       team.checks_used, team.checks_available),
        "actions": [
            {
                "text": "Open Dashboard",
                "type": "button",
                "url": 'https://' + domain + reverse('dashboard')
            }
        ]
    }

最佳答案

您必须mock SlackTeamSite并为teamdomain返回一些假的但真实的值,然后验证build_settings_message返回的值是正确的(基于teamdomain)。

请务必检查边缘情况,例如没有团队,重复的域等。

关于python - 我需要测试功能的哪些方面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54039157/

10-13 08:33