Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments tests #554

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 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
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
5 changes: 5 additions & 0 deletions allure-pytest-bdd/src/pytest_bdd_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
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 @@ -113,6 +115,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
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
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Feature: Attachments

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

When run pytest-bdd with allure

Then attachment given allure attachment must be in Given given step with attach
And this attachment with content:
"""
Given Attachment content
"""
And attachment when allure attachment must be in When when step with attach
And this attachment with content:
"""
When Attachment content
"""
And attachment then allure attachment must be in Then then step with attach
And this attachment with content:
"""
Then Attachment content
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Feature: Attachment

Scenario: attachment in a step, that is also a fixture
Given example.feature with content:
"""
Feature: Feature Test
Scenario: My Scenario Test
Given passed step
When when-step is fixture with attachment
Then passed step using fixture
"""
And py file with name: example_test
And with imports: pytest, pytest_bdd, allure
And with passed steps
And with func:
"""
@pytest.fixture()
@pytest_bdd.when("when-step is fixture with attachment")
def step_with_attachment():
allure.attach('Attachment content', 'allure attachment', allure.attachment_type.TEXT)
"""
And with func:
"""
@pytest_bdd.then("passed step using fixture")
def then_step(step_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 When when-step is fixture with attachment
And this attachment with content:
"""
Attachment content
"""
And attachments must not be in attachments
Loading