Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] add tests for the ToDoList class #18

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/ci_for_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
push:
branches: [ "main" ]
pull_request:
types: [synchronize, opened, reopened, ready_for_review]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build-and-run"
build-and-run:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/[email protected]
with:
python-version: '3.12'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest

# Runs a set of commands using the runners shell
- name: Run ToDoList tests
shell: bash
run: |
pytest tests
38 changes: 38 additions & 0 deletions tests/test_todo_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
from todo.todo_list import ToDoList

def test_add_task():
todo = ToDoList()
task = "Write unit tests"
index = todo.add_task(task)
assert index == 0
assert todo.tasks == [task]

def test_add_task_invalid():
todo = ToDoList()
with pytest.raises(ValueError, match="Task must be a non-empty string."):
todo.add_task("") # Testing with an empty string

def test_get_task():
todo = ToDoList()
task = "Write unit tests"
todo.add_task(task)
assert todo.get_task(0) == task

def test_get_task_invalid_index():
todo = ToDoList()
with pytest.raises(IndexError, match="Task index out of range."):
todo.get_task(1) # No task at index 1

def test_remove_task():
todo = ToDoList()
task = "Write documentation"
todo.add_task(task)
removed_task = todo.remove_task(0)
assert removed_task == task
assert len(todo.tasks) == 0

def test_remove_task_invalid_index():
todo = ToDoList()
with pytest.raises(IndexError, match="Task index out of range."):
todo.remove_task(0) # No task to remove