Skip to content
Open
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
Binary file added __pycache__/main.cpython-313.pyc
Binary file not shown.
Binary file added __pycache__/tests.cpython-313-pytest-8.4.2.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def delete_book_from_favorites(self, name):

# получаем список Избранных книг
def get_list_of_favorites_books(self):
return self.favorites
return self.favorites
73 changes: 56 additions & 17 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
# -*- coding: utf-8 -*-
from main import BooksCollector
import pytest


@pytest.fixture
def books():
return BooksCollector()


# класс TestBooksCollector объединяет набор тестов, которыми мы покрываем наше приложение BooksCollector
# обязательно указывать префикс Test
class TestBooksCollector:

# пример теста:
# обязательно указывать префикс test_
# дальше идет название метода, который тестируем add_new_book_
# затем, что тестируем add_two_books - добавление двух книг
def test_add_new_book_add_two_books(self):
# создаем экземпляр (объект) класса BooksCollector
collector = BooksCollector()
@pytest.mark.parametrize('name', ['The Witcher', 'The Witcher 2', 'Sherlock'])
def test_add_new_book(self, books, name):
check = len(books.books_genre)
books.add_new_book(name)
assert check + 1 == len(books.books_genre)

@pytest.mark.parametrize('name,genre', [['The Witcher', 'Фантастика'], ['The Witcher 2', 'Фантастика'],
['Sherlock', 'Детективы']])
def test_set_book_genre(self, books, name, genre):
if name not in books.books_genre:
books.add_new_book(name)
books.set_book_genre(name, genre)
print(books.books_genre)
assert books.books_genre[name] == genre

def test_get_book_genre(self, books):
books.add_new_book('The Witcher')
books.set_book_genre('The Witcher', 'Фантастика')
assert books.get_book_genre('The Witcher') == 'Фантастика'

def test_get_books_with_specific_genre(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
result = len(books.get_books_with_specific_genre('Фантастика'))
assert result == 2

def test_get_books_genre(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
assert books.get_books_genre() == books.books_genre

def test_get_books_for_children(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
children_books = {'The Witcher', 'The Witcher 2'}
result = books.get_books_for_children()
assert set(result) == children_books

# добавляем две книги
collector.add_new_book('Гордость и предубеждение и зомби')
collector.add_new_book('Что делать, если ваш кот хочет вас убить')
def test_add_book_in_favorites(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
books.add_book_in_favorites('The Witcher 2')
assert 'The Witcher 2' in books.favorites

# проверяем, что добавилось именно две
# словарь books_rating, который нам возвращает метод get_books_rating, имеет длину 2
assert len(collector.get_books_rating()) == 2
def test_delete_book_from_favorites(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
books.add_book_in_favorites('The Witcher 2')
books.delete_book_from_favorites('The Witcher 2')
assert 'The Witcher 2' not in books.favorites

# напиши свои тесты ниже
# чтобы тесты были независимыми в каждом из них создавай отдельный экземпляр класса BooksCollector()
def test_get_list_of_favorites_books(self, books):
books.books_genre = {'Sherlock': 'Детективы', 'The Witcher 2': 'Фантастика', 'The Witcher': 'Фантастика'}
books.add_book_in_favorites('The Witcher 2')
books.add_book_in_favorites('The Witcher')
assert books.get_list_of_favorites_books() == ['The Witcher 2', 'The Witcher']