Skip to content

Commit 9f16b3d

Browse files
committed
Making bdd features optional as examples
1 parent ce9523b commit 9f16b3d

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

hooks/post_gen_project.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{%- if cookiecutter.include_examples != "true" -%}
2+
"""A module for removing specific directories.
3+
4+
This module provides a function to remove specific directories
5+
using the `shutil` and `pathlib` modules.
6+
7+
Attributes:
8+
REMOVE_PATHS (List[str]): A list of directory paths to be removed.
9+
10+
"""
11+
12+
import shutil
13+
from pathlib import Path
14+
15+
REMOVE_PATHS = []
16+
REMOVE_PATHS = [
17+
"features",
18+
"tests/scenarios/steps",
19+
]
20+
21+
for path in REMOVE_PATHS:
22+
p = Path(".") / Path(path)
23+
if p and p.exists() and p.is_dir():
24+
shutil.rmtree(p)
25+
{% endif %}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{%- if cookiecutter.include_examples == "true" -%}
2+
Feature: divide
3+
The user should be able to divide two numbers.
4+
5+
Scenario Outline: Divide 'a' by 'b'
6+
Given I have two numbers <a> and <b>
7+
8+
When I divide <a> by <b>
9+
10+
Then I should see <output>
11+
12+
Examples:
13+
| a | b | output |
14+
| 2.0 | 2.0 | 1.0 |
15+
| 6.0 | 2.0 | 3.0 |
16+
| 1.0 | 2.0 | 0.5 |
17+
{% endif %}

{{cookiecutter.project_slug}}/tests/scenarios/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
"""Configuration file for pytest.
1+
"""A module for configuring pytest to include new features.
22
3-
This module will inject configuration scripts before running tests.
3+
This module provides a function to add new features automatically
4+
as test files in pytest. The new features will trigger errors because
5+
steps are not implemented.
46
"""
57
from pathlib import Path
68

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{%- if cookiecutter.include_examples == "true" -%}
2+
"""Feature steps implementation.
3+
4+
This script defines three steps for a BDD test using pytest-bdd. The test scenario is described in the simple_calculation.feature file.
5+
6+
"""
7+
8+
from pytest_bdd import given, parsers, scenarios, then, when
9+
10+
from {{cookiecutter.package_name}}.{{cookiecutter.module_name}} import Calculator
11+
12+
scenarios("simple_calculation.feature")
13+
14+
15+
@given(
16+
parsers.parse("I have two numbers {a:f} and {b:f}"), target_fixture="varl"
17+
)
18+
def given_numbers(a: float, b: float) -> dict:
19+
"""Set the initial values for the calculator test.
20+
21+
Args:
22+
a (float): The first number.
23+
b (float): The second number.
24+
25+
Returns:
26+
dict: A dictionary with keys "a" and "b" and their corresponding values.
27+
28+
"""
29+
return {"a": a, "b": b}
30+
31+
32+
@when(parsers.parse("I divide {a:f} by {b:f}"))
33+
def when_divide(varl: dict, a: float, b: float) -> None:
34+
"""Perform a division operation using the calculator.
35+
36+
Args:
37+
varl (dict): A dictionary with the initial values for the calculator.
38+
a (float): The dividend.
39+
b (float): The divisor.
40+
41+
Returns:
42+
None
43+
"""
44+
calc = Calculator()
45+
varl["output"] = calc.divide(varl["a"], varl["b"])
46+
47+
48+
@then(parsers.parse("I should see {output:f}"))
49+
def then_should_see(varl: dict, output: float) -> None:
50+
"""Verify if the result of the operation matches the expected output.
51+
52+
Args:
53+
varl (dict): A dictionary with the initial values for the calculator and the result of the operation.
54+
output (float): The expected output.
55+
56+
Returns:
57+
None
58+
"""
59+
assert varl["output"] == output
60+
{%- elif cookiecutter.include_examples != "true" -%}
61+
"""Feature steps implementation.
62+
63+
This script defines three steps for a BDD test using pytest-bdd. The test scenario is described in the simple_calculation.feature file.
64+
65+
"""{% endif %}

0 commit comments

Comments
 (0)