• JUPYTER
  • FAQ
  • View as Code
  • Python 3 (ipykernel) Kernel
  • View on GitHub
  • Execute on Binder
  • Download Notebook
  1. learn-python3
  2. notebooks
  3. beginner
  4. exercises
In [ ]:
# Let's make sure pytest and ipytest packages are installed
# ipytest is required for running pytest inside Jupyter notebooks
import sys

!{sys.executable} -m pip install pytest
!{sys.executable} -m pip install ipytest

# These are needed for running pytest inside Jupyter notebooks
import ipytest
import pytest

ipytest.autoconfig()

1. Finalize test cases¶

The testing part of the TodoList implementation is incomplete. Fill ____ parts of the tests.

In [ ]:
class TodoNotFound(Exception):
    pass


class TodoList:
    def __init__(self):
        self._todo = {}
        self._done = {}
        self._task_counter = 1

    @property
    def todo_tasks(self):
        return self._todo

    @property
    def done_tasks(self):
        return self._done

    def add(self, task):
        self._todo[self._task_counter] = task
        self._task_counter += 1

    def complete(self, number):
        if number not in self._todo:
            raise TodoNotFound(f"{number} not in todos")

        task = self._todo.pop(number)
        self._done[number] = task

    def remove(self, number):
        if number not in self._todo:
            raise TodoNotFound(f"{number} not in todos")

        del self._todo[number]

Finalize tests for TodoList.

In [ ]:
%%ipytest


@pytest.____
def todo_list():
    tl = TodoList()
    tl.add('buy milk')
    tl.add('take dog out')
    tl.add('learn pytest fixtures')
    ____ ____


def test_todo_tasks_property(todo_list):
    todo = todo_list.todo_tasks
    assert todo == {
        1: 'buy milk',
        2: 'take dog out',
        3: 'learn pytest fixtures'
    }


def test_add(____):
    todo_list.add('check pytest docs')
    todos = todo_list.todo_tasks
    assert todos[4] == ____


def test_complete(todo_list):
    # Make sure there is not done tasks yet
    assert not todo_list.done_tasks

    todo_list.complete(3)
    done = todo_list.____
    todo = todo_list.____
    assert done[3] == 'learn pytest fixtures'
    assert 3 not in ____


def test_complete_with_unknown_task_number(todo_list):
    # This is how you can test that a certain exception is raised
    with pytest.raises(TodoNotFound):
        todo_list.complete(10)


def test_remove(todo_list):
    todo_list.remove(1)
    done = todo_list.done_tasks
    todo = todo_list.todo_tasks

    assert 1 not in ____
    # Make sure it was not moved to done
    ____ not done


def test_remove_with_unknown_task_number(todo_list):
    with pytest.____(____):
        todo_list.remove(12)

2. Testing the Fibonacci numbers¶

Implement a test for the fibonacci function. Use pytest.mark.parametrize and test at least with numbers: 0, 1, 2, 3, and 10. You can find the expected results and more information about the Fibonacci numbers here.

In [ ]:
def fibonacci(number):
    if number in [0, 1]:
        return number
    return fibonacci(number - 1) + fibonacci(number - 2)
In [ ]:
%%ipytest

# Your implementation here

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbviewer version: 8b013f7

nbconvert version: 7.2.3

Rendered (Tue, 10 Jun 2025 07:09:24 UTC)