Skip to content
Closed
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
16 changes: 16 additions & 0 deletions allure-pytest-bdd/test/acceptance.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Created by denis at 11.01.2021
Feature: test
# Enter feature description here

Scenario: scenario
Given py file with name: example
And with imports: pytest, pytest_bdd, allure
And with func:
"""
@pytest_bdd.given("given_step")
def my_func():
allure.attach("blah", ...)
raise Exception("message")
"""
And test for scenario_name from file.feature
And py file saved
52 changes: 51 additions & 1 deletion allure-pytest-bdd/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import allure_commons
from allure_commons_test.report import AllureReport
from allure_commons.logger import AllureFileLogger
from .steps import * # noqa F401 F403
from .steps import * # noqa F401 F403
from pytest_bdd import given, when, parsers

from .py_file_builder import PyFileBuilder

pytest_plugins = "pytester"


@contextmanager
def fake_logger(path, logger):
Expand Down Expand Up @@ -61,3 +65,49 @@ def feature_definition(name, extension, content, testdir):
@when("run pytest-bdd with allure")
def run(allured_testdir):
allured_testdir.run_with_allure()


@pytest.fixture()
@given(parsers.parse("py file with name: {name}"))
def current_py_file_builder(name):
return PyFileBuilder(name)


@given(parsers.parse("with imports: {modules}"))
def add_imports_in_builder(modules, current_py_file_builder):
modules_names = [module.strip() for module in modules.split(",")]
current_py_file_builder.add_imports(*modules_names)


@given(parsers.re("with func:(?:\n)(?P<content>[\\S|\\s]*)"))
def add_func_in_builder(content, current_py_file_builder):
current_py_file_builder.add_func(content)


@given("with passed steps")
def add_passed_steps(current_py_file_builder):

passed_steps = '@pytest_bdd.given("passed step")\n' \
'@pytest_bdd.when("passed step")\n' \
'@pytest_bdd.then("passed step")\n' \
'def step_impl():\n' \
' pass'

current_py_file_builder.add_func(passed_steps)


@given(parsers.parse("test for {scenario_name} from {feature_file}"))
def add_scenario_step(scenario_name, feature_file, current_py_file_builder):

scenario_func = f'@pytest_bdd.scenario("{feature_file}", "{scenario_name}")\n' \
'def test_scenario():\n' \
' pass'

current_py_file_builder.add_func(scenario_func)


@given(parsers.parse("py file saved"))
def save_py_file(current_py_file_builder, testdir):
testdir.makefile(
".py",
**dict([(current_py_file_builder.name, current_py_file_builder.get_content())]))
29 changes: 29 additions & 0 deletions allure-pytest-bdd/test/py_file_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class PyFileBuilder:

def __init__(self, name):
self._import_line = None
self._file_funcs = []
self.name = name

def add_imports(self, *modules_names):
import_list = []

for module in modules_names:
import_list.append("import " + module)

if len(import_list) != 0:
self._import_line = "\n".join(import_list)

def add_func(self, str_func):
self._file_funcs.append(str_func)

def get_content(self):
if len(self._file_funcs) == 0:
raise Exception("There are no functions in this file")

content = "\n\n\n".join(self._file_funcs)

if self._import_line is not None:
content = self._import_line + "\n\n\n" + content

return content
146 changes: 146 additions & 0 deletions allure-pytest-bdd/test/py_file_builder_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from .py_file_builder import PyFileBuilder
import pytest


def test_common_func():
imports = ["pytest", "pytest_bdd", "allure"]
funcs = [
"""@given("given_step")
def given_func():
allure.attach("blah", ...)
raise Exception("message")""",
"""@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")""",
"""@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""
]

expected_answer = """import pytest
import pytest_bdd
import allure


@given("given_step")
def given_func():
allure.attach("blah", ...)
raise Exception("message")


@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")


@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""

file_builder = PyFileBuilder("test")

file_builder.add_imports(*imports)

for func in funcs:
file_builder.add_func(func)

assert file_builder.get_content() == expected_answer


def test_without_imports_func():
funcs = [
"""@given("given_step")
def given_func():
allure.attach("blah", ...)
raise Exception("message")""",
"""@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")""",
"""@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""
]

expected_answer = """@given("given_step")
def given_func():
allure.attach("blah", ...)
raise Exception("message")


@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")


@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""

file_builder = PyFileBuilder("test")

file_builder.add_imports()

for func in funcs:
file_builder.add_func(func)

assert file_builder.get_content() == expected_answer


def test_empty_func_str():
funcs = [
"",
"""@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")""",
"""@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""
]

expected_answer = """


@when("when_step")
def when_func():
allure.attach("blah", ...)
raise Exception("message")


@then("then_step")
def then_func():
allure.attach("blah", ...)
raise Exception("message")"""

file_builder = PyFileBuilder("test")

file_builder.add_imports()

for func in funcs:
file_builder.add_func(func)

assert file_builder.get_content() == expected_answer


def test_have_no_added_funcs():
imports = ["pytest", "pytest_bdd", "allure"]
funcs = []

file_builder = PyFileBuilder("test")

file_builder.add_imports(*imports)

for func in funcs:
file_builder.add_func(func)

with pytest.raises(Exception):
file_builder.get_content()