mardi 16 juillet 2019

Does it make sense to write unit tests that are just testing if functions are called?

I have the following code:

def task_completed(task):
    _update_status(task, TaskStatus.COMPLETED)
    successful_task(task)


def task_pending(task):
    _update_status(task, TaskStatus.PENDING)
    successful_task(task)


def task_canceled(task):
    _update_status(task, TaskStatus.CANCELED)
    process_task(task)


def successful_task(task):
    process_task(task)
    send_notification(task)


def process_task(task):
    assign_user(task)
    notify_user(task)
    cleanup(task)


def _update_status(task, status):
    task.status = status
    task.save(update_fields=['status'])

I have written the following tests:

def test_task_completed(mocker, task):
    mock_successful_task = mocker.patch('services.successful_task')
    task_completed(task)

    assert task.status == TaskStatus.COMPLETED
    mock_successful_task.called_once_with(task)


def test_task_pending(mocker, task):
    mock_successful_task = mocker.patch('services.successful_task')
    task_pending(task)

    assert task.status == TaskStatus.PENDING
    mock_successful_task.called_once_with(task)


def test_task_canceled(mocker, task):
    mock_process_task = mocker.patch('services.process_task')
    task_pending(task)

    assert task.status == TaskStatus.CANCELED
    mock_process_task.called_once_with(task)


def test_successful_task(mocker, task):
    mock_process_task = mocker.patch('services.process_task')
    mock_send_notification = mocker.patch('notifications.send_notification')

    mock_process_task.called_once_with(task)
    mock_send_notification.called_once_with(task)


def test_process_task(mocker, task):
    mock_assign_user = mocker.patch('users.assign_user')
    mock_notify_user = mocker.patch('notifications.notify_user')
    mock_cleanup = mocker.patch('utils.cleanup')

    mock_assign_user.called_once_with(task)
    mock_notify_user.called_once_with(task)
    mock_cleanup.called_once_with(task)

As you can see some tests like test_successful_task and test_process_task are just testing if specific functions are called.

But does it make sense to write a test for this or do I understand something wrong and my unit tests are just bad? I don't know another solution how I should test these functions.

Aucun commentaire:

Enregistrer un commentaire