forked from pytest-dev/pytest-bdd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from elchupanebrej/doc-e2e
Adjust tutorial to be executed during e2e test launch
- Loading branch information
Showing
15 changed files
with
208 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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,10 @@ | ||
Feature: Tutorial examples could be executed successfully | ||
|
||
Scenario: Catalog example with simplest steps | ||
Given Copy path from "docs\tutorial" to "tutorial" | ||
When run pytest | ||
|cli_args| --rootdir=tutorial| tutorial/tests | | ||
|
||
Then pytest outcome must contain tests with statuses: | ||
|passed| | ||
| 1| |
This file contains 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
Empty file.
2 changes: 2 additions & 0 deletions
2
docs/tutorial/tests/features/books.feature → docs/tutorial/features/books.feature
This file contains 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
This file contains 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
This file contains 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,3 @@ | ||
[pytest] | ||
; Defines default rootpath and a lot of pytest configs | ||
; |
This file contains 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,27 @@ | ||
"""This files represents simple `Application under test`""" | ||
from dataclasses import dataclass, field | ||
from typing import Iterable, List | ||
|
||
|
||
@dataclass # Easy way to not write redundant __init__ https://docs.python.org/3/library/dataclasses.html | ||
class Book: | ||
author: str | ||
title: str | ||
|
||
|
||
@dataclass | ||
class Catalog: | ||
storage: List[Book] = field(default_factory=list) | ||
|
||
def add_books_to_catalog(self, books: Iterable[Book]): | ||
self.storage.extend(books) | ||
|
||
def search_by_author(self, term: str): | ||
for book in self.storage: | ||
if term in book.author: | ||
yield book | ||
|
||
def search_by_title(self, term: str): | ||
for book in self.storage: | ||
if term in book.title: | ||
yield book |
Empty file.
This file contains 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,6 @@ | ||
[Desktop Entry] | ||
Type=Link | ||
URL=features/books.feature | ||
; Feature files are gathered as usual test modules, but also could be linked into | ||
; directory hierarchy by symlinks. | ||
; Some operation systems do not provide symlinks, so such files could replace them. |
This file contains 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,5 +1,21 @@ | ||
from steps.library_steps import * | ||
""" | ||
conftest.py is local per-directory plugin of pytest. | ||
Its mission is to define fixtures, steps, and hooks which will be used | ||
by tests gathered by pytest from directory structure below | ||
## or maybe: | ||
# import steps | ||
# ? | ||
https://docs.pytest.org/en/latest/how-to/writing_plugins.html#conftest-py-local-per-directory-plugins | ||
https://docs.pytest.org/en/latest/explanation/goodpractices.html#test-discovery | ||
""" | ||
|
||
from pytest import fixture | ||
|
||
from .steps.library_steps import ( | ||
a_search_type_is_performed_for_search_term, | ||
only_these_books_will_be_returned, | ||
these_books_in_the_catalog, | ||
) | ||
|
||
|
||
@fixture | ||
def search_results() -> list: | ||
return [] |
This file contains 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,34 +1,85 @@ | ||
from helper_methods.library_catalog import Catalog | ||
from helper_methods.verification_helper_methods import verify_returned_books | ||
from pytest import fixture | ||
from pytest_bdd_ng import given, parsers, step, then, when | ||
import re | ||
from typing import List, Literal | ||
|
||
from src.catalog import Book, Catalog | ||
|
||
@fixture | ||
def context() | ||
""" Create a placeholder object to use in place of Cucumber's context object. The context object allows us to pass state between steps. """ | ||
class dummy(): | ||
pass | ||
from messages import DataTable, Step # type:ignore[attr-defined] | ||
from pytest_bdd import given, step, then, when | ||
|
||
|
||
return dummy() | ||
def get_books_from_data_table(data_table: DataTable): | ||
# Gherkin data-tables have no title row by default, but we could define them if we want. | ||
title_row, *book_rows = data_table.rows | ||
|
||
step_data_table_titles = [] | ||
for cell in title_row.cells: | ||
step_data_table_titles.append(cell.value) | ||
|
||
@given("these books in the catalog") | ||
def these_books_in_the_catalog(step): | ||
context.catalog = Catalog() | ||
context.catalog.add_books_to_catalog(step.data_table) | ||
assert step_data_table_titles == ["Author", "Title"] | ||
|
||
books = [] | ||
for row in book_rows: | ||
books.append(Book(row.cells[0].value, row.cells[1].value)) | ||
|
||
@when(parsers.re("a (?P<search_type>name|title) search is performed for " + | ||
"(?P<search_term>.+)")) | ||
def a_SEARCH_TYPE_is_performed_for_SEARCH_TERM(search_type, search_term): | ||
return books | ||
|
||
|
||
# Steps to be used in scenarios are defined with special decorators | ||
@given( | ||
"these books in the catalog", | ||
# Steps are allowed to inject new fixtures or overwrite existing ones | ||
target_fixture="catalog", | ||
) | ||
def these_books_in_the_catalog( | ||
# `step` fixture is injected by pytest dependency injection mechanism into scope of step by default; | ||
# So it could be used without extra effort | ||
step: Step, | ||
): | ||
books = get_books_from_data_table(step.data_table) | ||
|
||
catalog = Catalog() | ||
catalog.add_books_to_catalog(books) | ||
|
||
yield catalog | ||
|
||
|
||
@when( | ||
# Step definitions could have parameters. Here could be raw stings, cucumber expressions or regular expressions | ||
re.compile("a (?P<search_type>name|title) search is performed for (?P<search_term>.+)"), | ||
target_fixture="search_results", | ||
) | ||
def a_search_type_is_performed_for_search_term( | ||
# `search_results` is a usual pytest fixture defined somewhere else (at conftest.py, plugin or module) and injected by pytest dependency injection mechanism. | ||
# In this case it will be provided by conftest.py | ||
search_results: List[Book], | ||
# `search_type` and `search_term` are parameters of this step and are injected by step definition | ||
search_type: Literal["name", "title"], | ||
search_term: str, | ||
# `catalog` is a fixture injected by another step | ||
catalog: Catalog, | ||
): | ||
if search_type == "title": | ||
raise NotImplementedError("Title searches are not yet implemented.") | ||
context.search_results = context.catalog.search_by_author(search_term) | ||
search = catalog.search_by_title | ||
elif search_type == "name": | ||
search = catalog.search_by_author | ||
else: | ||
assert False, "Unknown" | ||
|
||
found_books = search(search_term) | ||
search_results.extend(found_books) | ||
yield search_results | ||
|
||
|
||
@then("only these books will be returned") | ||
def only_these_books_will_be_returned(step): | ||
expected_books = context.catalog.read_books_from_table(step.data_table) | ||
verify_returned_books(context.search_results, expected_books) | ||
def only_these_books_will_be_returned( | ||
# Fixtures persist during step execution, so usual `context` common for behave users is not required, | ||
# so if you define fixture dependencies debugging becomes much easier. | ||
search_results: List[Book], | ||
step: Step, | ||
catalog: Catalog, | ||
): | ||
expected_books = get_books_from_data_table(step.data_table) | ||
|
||
for book in search_results: | ||
if book not in expected_books: | ||
assert False, f"Book ${book} is not expected" |
This file contains 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,3 @@ | ||
[Desktop Entry] | ||
Type=Link | ||
URL=Features/Tutorial launch.feature |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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