Testing is a fundamental part of software development. In Python, itβs essential to write testable code and accompany it with test cases to ensure reliability and robustness. This document provides a working code sample for a realistic Python project and demonstrates how to write corresponding test cases using Python's built-in unittest framework.
We will build a small project consisting of a library management system. The project will include classes for Book and Library, along with methods to add, remove, and search for books. Each functionality will be accompanied by test cases that follow best practices.
class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
def __str__(self):
return f"{self.title} by {self.author} ({self.year})"
def __eq__(self, other):
return (
self.title == other.title and
self.author == other.author and
self.year == other.year
)
from book import Book
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
if book in self.books:
return False
self.books.append(book)
return True
def remove_book(self, book):
if book in self.books:
self.books.remove(book)
return True
return False
def find_books_by_author(self, author):
return [book for book in self.books if book.author == author]
def find_books_by_title(self, title):
return [book for book in self.books if book.title == title]
def list_books(self):
return sorted(self.books, key=lambda x: (x.author, x.title))
import unittest
from library import Library
from book import Book
class TestLibrary(unittest.TestCase):
def setUp(self):
self.library = Library()
self.book1 = Book("1984", "George Orwell", 1949)
self.book2 = Book("Animal Farm", "George Orwell", 1945)
self.book3 = Book("Brave New World", "Aldous Huxley", 1932)
def test_add_book(self):
result = self.library.add_book(self.book1)
self.assertTrue(result)
self.assertIn(self.book1, self.library.books)
def test_add_duplicate_book(self):
self.library.add_book(self.book1)
result = self.library.add_book(self.book1)
self.assertFalse(result)
def test_remove_book(self):
self.library.add_book(self.book1)
result = self.library.remove_book(self.book1)
self.assertTrue(result)
self.assertNotIn(self.book1, self.library.books)
def test_remove_nonexistent_book(self):
result = self.library.remove_book(self.book1)
self.assertFalse(result)
def test_find_books_by_author(self):
self.library.add_book(self.book1)
self.library.add_book(self.book2)
self.library.add_book(self.book3)
results = self.library.find_books_by_author("George Orwell")
self.assertIn(self.book1, results)
self.assertIn(self.book2, results)
self.assertNotIn(self.book3, results)
def test_find_books_by_title(self):
self.library.add_book(self.book1)
results = self.library.find_books_by_title("1984")
self.assertEqual(results, [self.book1])
def test_list_books(self):
self.library.add_book(self.book1)
self.library.add_book(self.book2)
self.library.add_book(self.book3)
books = self.library.list_books()
expected_order = sorted([self.book1, self.book2, self.book3], key=lambda x: (x.author, x.title))
self.assertEqual(books, expected_order)
if __name__ == "__main__":
unittest.main()
When running test_library.py with python -m unittest test_library.py, you should see output like:
.......
----------------------------------------------------------------------
Ran 7 tests in 0.001s
OK
This working code sample demonstrates a complete, real-world example of how to structure Python code and write effective tests. It shows how to:
With these techniques, developers can confidently write, test, and refactor code while ensuring correctness and stability in their Python projects.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved