Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4d88920
Add to coftest.py reference on pytester plugin
Duisus Jan 8, 2021
0719d64
Move feature directory to allure-pytest-bdd\test\
Duisus Jan 8, 2021
0600bca
v1.0 Bug fix #474
Duisus Jan 9, 2021
3dc630d
Add files for test
Duisus Jan 9, 2021
c6f1048
Small refactor
Duisus Jan 9, 2021
fb27f2d
Fix AttachmentWorker
Duisus Jan 9, 2021
dce0f92
Small refactoring of AttachmentWorker
Duisus Jan 9, 2021
5979b60
Add test for bug fix #474
Duisus Jan 9, 2021
e694c50
Fix and refactor AttachmentWorker
Duisus Jan 11, 2021
a1900e4
AttachmentWorker refactor
ReoneAbakkio Jan 11, 2021
76c668a
Create directory attachments_tests and move bug474 tests to there
Duisus Jan 11, 2021
cd590ea
Create directory tests and move outline_test.py, scenario_test.py to …
Duisus Jan 11, 2021
aec2b58
Create basis for PyFileBuilder, and acceptance.feature
Duisus Jan 11, 2021
7e396b5
Make a PyFileBuilder class and add a tests
ReoneAbakkio Jan 11, 2021
aa1f176
Add a valid reactions on unexpected cases and expand tests
ReoneAbakkio Jan 11, 2021
c802bb9
Add common steps for testing
Duisus Jan 11, 2021
4260af1
Merge branch 'test-infrastructure' into master
Duisus Jan 11, 2021
d0f27d9
Merge branch 'master' into bugfix-attach
Duisus Jan 11, 2021
9534cf2
Add steps for testing attachments
Duisus Jan 12, 2021
b8d37e6
Change test on bug #474
Duisus Jan 12, 2021
e735251
Merge branch 'bugfix-attach' into master
Duisus Jan 12, 2021
364b71f
Create base tests on attachments
Duisus Jan 12, 2021
3e18f2b
Add more tests for attachments
ReoneAbakkio Jan 12, 2021
58b6b15
Add new func in top conftest.py
Duisus Jan 12, 2021
79ff201
Fix and refactor exist tests for attachments
Duisus Jan 12, 2021
f94f29c
Add new test for attachments
Duisus Jan 12, 2021
69f3dbd
Rename and move test for bug #474
Duisus Jan 12, 2021
842b8b4
Merge branch 'attachments-tests'
ReoneAbakkio Jan 13, 2021
d2b3035
Add support of links in allure-pytest-bdd
Duisus Jan 14, 2021
d0b658f
Add conftest.py with functions for testing links
Duisus Jan 14, 2021
556470a
Add tests to links
ReoneAbakkio Jan 14, 2021
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
61 changes: 61 additions & 0 deletions allure-pytest-bdd/src/attachment_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os.path
import os


class AttachmentWorker:

def __init__(self, test_result, item):
self.test_result = test_result
self.attachments_dir = AttachmentWorker.get_path_to_attachments(item)

def delete_duplicates(self):
if len(self.test_result.attachments) == 0:
return

for step in self.test_result.steps:
for attach in step.attachments:
to_delete = self._find_duplicate(attach)
if to_delete is not None:
self.test_result.attachments.remove(to_delete)
os.remove(os.path.join(self.attachments_dir, to_delete.source))

@staticmethod
def get_path_to_attachments(item):
splitted_param = AttachmentWorker._get_allurdir_param(item).split('=')

project_dir = str(item.config.invocation_params.dir)
if len(splitted_param) == 1:
return project_dir

allure_dir = os.path.normpath(splitted_param[1])
if not os.path.isabs(allure_dir):
allure_dir = os.path.join(project_dir, allure_dir.lstrip("\\"))

return allure_dir

def _find_duplicate(self, attachment_from_step):
for attachment in self.test_result.attachments:
if self._are_attachments_equal(attachment, attachment_from_step):
return attachment

return None

def _are_attachments_equal(self, first, second):
first_file = open(os.path.join(self.attachments_dir, first.source), 'br')
first_content = first_file.read()
first_file.close()

second_file = open(os.path.join(self.attachments_dir, second.source), 'br')
second_content = second_file.read()
second_file.close()

return \
first.name == second.name and \
first.type == second.type and \
first_content == second_content

@staticmethod
def _get_allurdir_param(item):
for param in item.config.invocation_params.args:
if param.startswith("--alluredir"):
return param
16 changes: 16 additions & 0 deletions allure-pytest-bdd/src/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import allure_commons
import pytest
from .utils import ALLURE_LINK_MARK


class AllureTestHelper(object):
def __init__(self, config):
self.config = config

@allure_commons.hookimpl
def decorate_as_link(self, url, link_type, name):
pattern = dict(self.config.option.allure_link_pattern).get(link_type, u'{}')
url = pattern.format(url)
allure_link = getattr(pytest.mark, ALLURE_LINK_MARK)
name = url if name is None else name
return allure_link(url, name=name, link_type=link_type)
29 changes: 29 additions & 0 deletions allure-pytest-bdd/src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import os
from allure_commons.logger import AllureFileLogger
from .pytest_bdd_listener import PytestBDDListener
from .helper import AllureTestHelper
from .utils import ALLURE_LINK_MARK

import argparse


def pytest_addoption(parser):
Expand All @@ -17,6 +21,25 @@ def pytest_addoption(parser):
dest="clean_alluredir",
help="Clean alluredir folder if it exists")

def link_pattern(string):
pattern = string.split(':', 1)
if not pattern[0]:
raise argparse.ArgumentTypeError('Link type is mandatory.')

if len(pattern) != 2:
raise argparse.ArgumentTypeError('Link pattern is mandatory')
return pattern

parser.getgroup("general").addoption('--allure-link-pattern',
action="append",
dest="allure_link_pattern",
metavar="LINK_TYPE:LINK_PATTERN",
default=[],
type=link_pattern,
help="""Url pattern for link type. Allows short links in test,
like 'issue-1'. Text will be formatted to full url with python
str.format().""")


def cleanup_factory(plugin):
def clean_up():
Expand All @@ -29,6 +52,10 @@ def pytest_configure(config):
report_dir = config.option.allure_report_dir
clean = config.option.clean_alluredir

test_helper = AllureTestHelper(config)
allure_commons.plugin_manager.register(test_helper)
config.add_cleanup(cleanup_factory(test_helper))

if report_dir:
report_dir = os.path.abspath(report_dir)

Expand All @@ -40,3 +67,5 @@ def pytest_configure(config):
file_logger = AllureFileLogger(report_dir, clean)
allure_commons.plugin_manager.register(file_logger)
config.add_cleanup(cleanup_factory(file_logger))

config.addinivalue_line("markers", "{mark}: allure link marker".format(mark=ALLURE_LINK_MARK))
10 changes: 9 additions & 1 deletion allure-pytest-bdd/src/pytest_bdd_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import allure_commons
from allure_commons.utils import now
from allure_commons.utils import uuid4
from allure_commons.model2 import Label
from allure_commons.model2 import Label, Link
from allure_commons.model2 import Status

from allure_commons.types import LabelType
Expand All @@ -12,11 +12,14 @@
from .utils import get_step_name
from .utils import get_status_details
from .utils import get_pytest_report_status
from .utils import allure_links
from allure_commons.model2 import StatusDetails
from functools import partial
from allure_commons.lifecycle import AllureLifecycle
from .utils import get_full_name, get_name, get_params

from .attachment_worker import AttachmentWorker


class PytestBDDListener(object):
def __init__(self):
Expand Down Expand Up @@ -46,6 +49,8 @@ def pytest_bdd_before_scenario(self, request, feature, scenario):
test_result.labels.append(Label(name=LabelType.FRAMEWORK, value="pytest-bdd"))
test_result.labels.append(Label(name=LabelType.LANGUAGE, value=platform_label()))
test_result.labels.append(Label(name=LabelType.FEATURE, value=feature.name))
test_result.links.extend([Link(link_type, url, name)
for link_type, url, name in allure_links(request.node)])
test_result.parameters = get_params(request.node)

finalizer = partial(self._scenario_finalizer, scenario)
Expand Down Expand Up @@ -113,6 +118,9 @@ def pytest_runtest_makereport(self, item, call):
test_result.status = status
test_result.statusDetails = status_details

if test_result and test_result.status:
AttachmentWorker(test_result, item).delete_duplicates()

if report.when == 'teardown':
self.lifecycle.write_test_case(uuid=uuid)

Expand Down
8 changes: 8 additions & 0 deletions allure-pytest-bdd/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from allure_commons.utils import format_exception


ALLURE_LINK_MARK = 'allure_link'


def get_step_name(node, step):
name = "{step_keyword} {step_name}".format(step_keyword=step.keyword, step_name=step.name)
if hasattr(node, 'callspec'):
Expand Down Expand Up @@ -50,3 +53,8 @@ def get_params(node):
if hasattr(node, 'callspec'):
params = node.callspec.params
return [Parameter(name=name, value=value) for name, value in params.items()]


def allure_links(item):
for mark in item.iter_markers(name=ALLURE_LINK_MARK):
yield (mark.kwargs["link_type"], mark.args[0], mark.kwargs["name"])
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
42 changes: 42 additions & 0 deletions allure-pytest-bdd/test/attachments_tests/attachment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pytest_bdd import scenario


@scenario("attachments_features\\attachment_in_step.feature", "Attachment in Given")
def test_given_with_attachments():
pass


@scenario("attachments_features\\attachment_in_step.feature", "Attachment in When")
def test_when_with_attachments():
pass


@scenario("attachments_features\\attachment_in_step.feature", "Attachment in Then")
def test_then_with_attachments():
pass


@scenario("attachments_features\\attachment_in_exception_step.feature", "Attachment and exception in step")
def test_step_with_attachments_and_exception():
pass


@scenario("attachments_features\\attachment_in_many_steps.feature", "Attachment in many steps")
def test_attachment_in_many_steps():
pass


@scenario("attachments_features\\attachment_in_failed_step.feature", "Attachment in failed step")
def test_failed_step_with_attachment():
pass


@scenario("attachments_features\\attachment_in_called_fixture.feature", "Attachment in called fixture")
def test_attachment_in_called_fixture():
pass


@scenario("attachments_features\\attachment_in_step-fixture.feature",
"attachment in a step, that is also a fixture")
def test_attachment_in_step_fixture():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Feature: Attachments

Scenario: Attachment in called fixture
Given example.feature with content:
"""
Feature: Feature Test
Scenario: My Scenario Test
Given passed step
When when-step use fixture, that have attachment
Then passed step
"""
And py file with name: example_test
And with imports: pytest, pytest_bdd, allure
And with passed steps
And with func:
"""
@pytest.fixture()
def fixture_with_attachment():
allure.attach('Attachment content', 'allure attachment', allure.attachment_type.TEXT)
"""
And with func:
"""
@pytest_bdd.when("when-step use fixture, that have attachment")
def step_with_attachment(fixture_with_attachment):
pass
"""
And test for My Scenario Test from example.feature
And py file saved

When run pytest-bdd with allure

Then attachment allure attachment must be in attachments
And this attachment with content:
"""
Attachment content
"""
And attachments must not be in When when-step use fixture, that have attachment
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: Attachments

Scenario: Attachment and exception in step
Given example.feature with content:
"""
Feature: Feature Test
Scenario: My Scenario Test
Given step with attach and exception
When passed step
Then passed step
"""
And py file with name: example_test
And with imports: pytest_bdd, allure
And with passed steps
And with func:
"""
@pytest_bdd.given("step with attach and exception")
def step_with_attachment():
allure.attach('Attachment content', 'allure attachment', allure.attachment_type.TEXT)
raise Exception("Exception message")
"""
And test for My Scenario Test from example.feature
And py file saved

When run pytest-bdd with allure

Then attachment allure attachment must be in Given step with attach and exception
And this attachment with content:
"""
Attachment content
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: Attachments

Scenario: Attachment in failed step
Given example.feature with content:
"""
Feature: Feature Test
Scenario: My Scenario Test
Given failed step with attach
When passed step
Then passed step
"""
And py file with name: example_test
And with imports: pytest_bdd, allure
And with passed steps
And with func:
"""
@pytest_bdd.given("failed step with attach")
def step_with_attachment():
allure.attach('Attachment content', 'allure attachment', allure.attachment_type.TEXT)
assert False
"""
And test for My Scenario Test from example.feature
And py file saved

When run pytest-bdd with allure

Then attachment allure attachment must be in Given failed step with attach
And this attachment with content:
"""
Attachment content
"""
Loading