generated from yandex-praktikum/qa_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Дабвление тестов для BooksCollector #5
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
Open
aoiaoki
wants to merge
3
commits into
main
Choose a base branch
from
develop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,21 @@ | ||
| # qa_python | ||
| # qa_python | ||
| # Тестирование класса BooksCollector | ||
|
|
||
| ## Реализованные тесты | ||
|
|
||
| | Метод | Проверка | | ||
| |--------|-----------| | ||
| | `add_new_book` | Добавление книги, ограничение длины названия, уникальность | | ||
| | `set_book_genre` | Присвоение жанра и проверка валидности | | ||
| | `get_book_genre` | Возврат установленного жанра | | ||
| | `get_books_with_specific_genre` | Фильтрация по жанру | | ||
| | `get_books_genre` | Получение полного списка книг с жанрами | | ||
| | `get_books_for_children` | Исключение жанров с возрастным рейтингом | | ||
| | `add_book_in_favorites` | Добавление без повторов | | ||
| | `delete_book_from_favorites` | Удаление из избранного | | ||
| | `get_list_of_favorites_books` | Получение списка избранных книг | | ||
|
|
||
| ## Как запустить тесты | ||
|
|
||
| ```bash | ||
| pytest -v tests.py |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import pytest | ||
| from main import BooksCollector | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def collector(): | ||
| return BooksCollector() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,91 @@ | ||
| from main import BooksCollector | ||
| import pytest | ||
|
|
||
| # класс TestBooksCollector объединяет набор тестов, которыми мы покрываем наше приложение BooksCollector | ||
| # обязательно указывать префикс Test | ||
| class TestBooksCollector: | ||
|
|
||
| # пример теста: | ||
| # обязательно указывать префикс test_ | ||
| # дальше идет название метода, который тестируем add_new_book_ | ||
| # затем, что тестируем add_two_books - добавление двух книг | ||
| def test_add_new_book_add_two_books(self): | ||
| # создаем экземпляр (объект) класса BooksCollector | ||
| collector = BooksCollector() | ||
| # ---------- add_new_book ---------- | ||
|
|
||
| # добавляем две книги | ||
| collector.add_new_book('Гордость и предубеждение и зомби') | ||
| collector.add_new_book('Что делать, если ваш кот хочет вас убить') | ||
| @pytest.mark.parametrize('name', ['', 'A' * 41]) | ||
| def test_add_new_book_invalid_name_not_added(self, collector, name): | ||
| collector.add_new_book(name) | ||
| assert collector.get_books_genre() == {} | ||
|
|
||
| # проверяем, что добавилось именно две | ||
| # словарь books_rating, который нам возвращает метод get_books_rating, имеет длину 2 | ||
| assert len(collector.get_books_rating()) == 2 | ||
| def test_add_new_book_same_book_not_duplicated(self, collector): | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.add_new_book('Гарри Поттер') | ||
| assert collector.get_books_genre() == {'Гарри Поттер': ''} | ||
|
|
||
| # напиши свои тесты ниже | ||
| # чтобы тесты были независимыми в каждом из них создавай отдельный экземпляр класса BooksCollector() | ||
| # ---------- set_book_genre ---------- | ||
|
|
||
| def test_set_book_genre_valid_genre(self, collector): | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.set_book_genre('Гарри Поттер', 'Фантастика') | ||
| assert collector.get_book_genre('Гарри Поттер') == 'Фантастика' | ||
|
|
||
| def test_set_book_genre_invalid_genre_not_set(self, collector): | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.set_book_genre('Гарри Поттер', 'Романтика') | ||
| assert collector.get_book_genre('Гарри Поттер') == '' | ||
|
|
||
| # ---------- get_books_with_specific_genre ---------- | ||
|
|
||
| def test_get_books_with_specific_genre_returns_correct_books(self, collector): | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.add_new_book('Оно') | ||
| collector.set_book_genre('Гарри Поттер', 'Фантастика') | ||
| collector.set_book_genre('Оно', 'Ужасы') | ||
| assert collector.get_books_with_specific_genre('Фантастика') == ['Гарри Поттер'] | ||
|
|
||
| # ---------- get_books_for_children ---------- | ||
|
|
||
| def test_get_books_for_children_excludes_adult_genres(self, collector): | ||
| collector.add_new_book('Оно') | ||
| collector.add_new_book('Шрек') | ||
| collector.set_book_genre('Оно', 'Ужасы') | ||
| collector.set_book_genre('Шрек', 'Мультфильмы') | ||
| result = collector.get_books_for_children() | ||
| assert 'Оно' not in result | ||
| assert 'Шрек' in result | ||
|
|
||
| # ---------- add_book_in_favorites ---------- | ||
|
|
||
| def test_add_book_in_favorites_adds_once(self, collector): | ||
| collector.add_new_book('Шрек') | ||
| collector.add_book_in_favorites('Шрек') | ||
| collector.add_book_in_favorites('Шрек') # повторно не добавляется | ||
| assert collector.get_list_of_favorites_books() == ['Шрек'] | ||
|
|
||
| def test_add_book_in_favorites_not_existing_not_added(self, collector): | ||
| collector.add_book_in_favorites('Несуществующая книга') | ||
| assert collector.get_list_of_favorites_books() == [] | ||
|
|
||
| # ---------- delete_book_from_favorites ---------- | ||
|
|
||
| def test_delete_book_from_favorites_removes_book(self, collector): | ||
|
|
||
| collector.add_new_book('Шрек') | ||
| collector.add_book_in_favorites('Шрек') | ||
| collector.delete_book_from_favorites('Шрек') | ||
| assert 'Шрек' not in collector.get_list_of_favorites_books() | ||
|
|
||
| # ---------- get_list_of_favorites_books ---------- | ||
|
|
||
| def test_get_list_of_favorites_books_returns_correct_list(self, collector): | ||
| collector.add_new_book('Шрек') | ||
| collector.add_new_book('Гарри Поттер') | ||
| collector.add_book_in_favorites('Шрек') | ||
| assert collector.get_list_of_favorites_books() == ['Шрек'] | ||
|
|
||
| # ---------- get_books_genre ---------- | ||
|
|
||
| def test_get_books_genre_returns_all_books(self, collector): | ||
| collector.add_new_book('Шрек') | ||
| collector.add_new_book('Гарри Поттер') | ||
| expected = {'Шрек': '', 'Гарри Поттер': ''} | ||
| assert collector.get_books_genre() == expected | ||
|
|
||
| # ---------- get_book_genre ---------- | ||
|
|
||
| def test_get_book_genre_returns_correct_genre(self, collector): | ||
| collector.add_new_book('Шрек') | ||
| collector.set_book_genre('Шрек', 'Мультфильмы') | ||
| assert collector.get_book_genre('Шрек') == 'Мультфильмы' | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.