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 #108 from elchupanebrej/docs
Docs
- Loading branch information
Showing
22 changed files
with
306 additions
and
31 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 test path "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.
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,15 +1,21 @@ | ||
.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific | ||
use cases of **pytest-bdd-ng** by investigation of its regression | ||
test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests | ||
|
||
Gherkin feature launch by pytest.feature | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
||
.. include:: ../Features/Gherkin feature launch by pytest.feature | ||
:code: gherkin | ||
|
||
Tags for Scenario Outlines examples.feature | ||
########################################### | ||
|
||
.. include:: ../Features/Scenario/Outline/Tags for Scenario Outlines examples.feature | ||
:code: gherkin | ||
.. NOTE:: Features below are part of end-to-end test suite; You always could find most specific | ||
use cases of **pytest-bdd-ng** by investigation of its regression | ||
test suite https://github.com/elchupanebrej/pytest-bdd-ng/tree/default/tests | ||
|
||
Tutorial launch.feature | ||
!!!!!!!!!!!!!!!!!!!!!!! | ||
|
||
.. include:: ../Features/Tutorial launch.feature | ||
:code: gherkin | ||
|
||
Gherkin feature launch by pytest.feature | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
||
.. include:: ../Features/Gherkin feature launch by pytest.feature | ||
:code: gherkin | ||
|
||
Tags for Scenario Outlines examples.feature | ||
########################################### | ||
|
||
.. include:: ../Features/Scenario/Outline/Tags for Scenario Outlines examples.feature | ||
:code: gherkin |
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,12 @@ | ||
# Feature files represent `Application under test` functional capabilities | ||
# in form of acceptance test with representative examples | ||
Feature: Library book searches and book delivery | ||
Scenario: The catalog can be searched by author name. | ||
Given these books in the catalog | ||
| Author | Title | | ||
| Stephen King | The Shining | | ||
| James Baldwin | If Beale Street Could Talk | | ||
When a name search is performed for Stephen | ||
Then only these books will be returned | ||
| Author | Title | | ||
| Stephen King | The Shining | |
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,22 @@ | ||
Tutorial for Behave/Cucumber users | ||
================================== | ||
|
||
.. _tutorial: https://thebddcoach.com/post/a-quick-introduction-to-pytest-bdd-ng-for-people-who-are-already-familiar-with-cucumber-or-behave/ | ||
|
||
Leslie's tutorial_ ... | ||
|
||
.. toctree:: | ||
:glob: | ||
|
||
tests/features/*.feature | ||
src/*.py | ||
tests/*.py | ||
tests/*.desktop | ||
tests/steps/*.py | ||
|
||
|
||
Set up the tutorial | ||
------------------- | ||
|
||
From the project root, activate a virtual environment and run: | ||
``pip install -r docs/tutorial/requirements.txt`` |
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,2 @@ | ||
pytest | ||
pytest_bdd_ng |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
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 | ||
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 [] |
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,85 @@ | ||
import re | ||
from typing import List, Literal | ||
|
||
from messages import DataTable, Step # type:ignore[attr-defined] | ||
from pytest_bdd import given, step, then, when | ||
|
||
from ...src.catalog import Book, Catalog | ||
|
||
|
||
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) | ||
|
||
assert step_data_table_titles == ["Author", "Title"] | ||
|
||
books = [] | ||
for row in book_rows: | ||
books.append(Book(row.cells[0].value, row.cells[1].value)) | ||
|
||
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": | ||
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( | ||
# 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
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 @@ | ||
[Desktop Entry] | ||
Type=Link | ||
URL=Features/Tutorial launch.feature |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.