Skip to content
Merged
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
51 changes: 29 additions & 22 deletions allure-pytest/src/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,52 @@ def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
status=get_status(exc_val),
statusDetails=get_status_details(exc_type, exc_val, exc_tb))

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_protocol(self, item, nextitem):
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_protocol(self, item):
uuid = self._cache.set(item.nodeid)
test_result = TestResult(name=item.name, uuid=uuid)
self.allure_logger.schedule_test(uuid, test_result)
yield
uuid = self._cache.pop(item.nodeid)
self.allure_logger.close_test(uuid)

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup(self, item):
yield
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
for fixturedef in _test_fixtures(item):
group_uuid = self._cache.get(fixturedef)
if not group_uuid:
group_uuid = self._cache.set(fixturedef)
group = TestResultContainer(uuid=group_uuid)
self.allure_logger.start_group(group_uuid, group)
self.allure_logger.update_group(group_uuid, children=uuid)

params = item.callspec.params if hasattr(item, 'callspec') else {}

test_result = TestResult(name=allure_name(item, params), uuid=uuid)
test_result.name = allure_name(item, params)
test_result.description = allure_description(item)
test_result.descriptionHtml = allure_description_html(item)
test_result.fullName = allure_full_name(item)
test_result.historyId = md5(test_result.fullName)
test_result.parameters.extend([Parameter(name=name, value=represent(value)) for name, value in params.items()])

self.allure_logger.schedule_test(uuid, test_result)
test_result.parameters.extend(
[Parameter(name=name, value=represent(value)) for name, value in params.items()])

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
if test_result:
test_result.start = now()
yield
if test_result:
test_result.stop = now()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(self, item):
yield
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
Expand All @@ -89,21 +111,6 @@ def pytest_runtest_protocol(self, item, nextitem):
test_result.labels.append(Label(name='package', value=allure_package(item)))
test_result.links.extend([Link(link_type, url, name) for link_type, url, name in allure_links(item)])

uuid = self._cache.pop(item.nodeid)
self.allure_logger.close_test(uuid)

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
if test_result:
test_result.start = now()

yield

if test_result:
test_result.stop = now()

@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(self, fixturedef, request):
fixture_name = fixturedef.argname
Expand Down
21 changes: 5 additions & 16 deletions allure-pytest/test/attachments/attachments_fixtures_test.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
"""
>>> allure_report = getfixture('allure_report')
>>> assert_that(allure_report,
... all_of(
... has_property('test_cases', has_length(8)),
... has_property('test_groups', has_length(2)),
... has_property('attachments', has_length(6))
... )) # doctest: +SKIP
"""

import pytest
import tempfile
from six import text_type
import allure

TEXT = "attachment body"


@pytest.fixture
def attach_file_in_function_scope_fixture(svg_file):
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)


def test_attach_file_in_function_scope_fixture(attach_file_in_function_scope_fixture):
Expand Down Expand Up @@ -55,7 +44,7 @@ def test_attach_file_in_reused_function_scope_fixture(attach_file_in_function_sc
@pytest.fixture
def attach_file_in_function_scope_finalizer(svg_file, request):
def finalizer_function_scope_fixture():
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
request.addfinalizer(finalizer_function_scope_fixture)


Expand Down Expand Up @@ -93,7 +82,7 @@ def test_attach_file_in_reused_function_scope_finalizer(attach_file_in_function_

@pytest.fixture(scope='module')
def attach_data_in_module_scope_fixture():
pytest.allure.attach(TEXT, attachment_type='text/plain')
allure.attach(TEXT, attachment_type='text/plain')


def test_attach_data_in_module_scope_fixture(attach_data_in_module_scope_fixture):
Expand Down Expand Up @@ -131,7 +120,7 @@ def test_attach_data_in_reused_module_scope_fixture(attach_data_in_module_scope_
@pytest.fixture(scope='module')
def attach_data_in_module_scope_finalizer(request):
def finalizer_module_scope_fixture():
pytest.allure.attach(TEXT, attachment_type='text/plain')
allure.attach(TEXT, attachment_type='text/plain')
request.addfinalizer(finalizer_module_scope_fixture)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import allure


PARAMS = ["first", "second", "third"]


@pytest.fixture(scope='module', params=PARAMS)
def attach_data_in_parametrized_fixture(request):
allure.attach(request.param, name=request.param, attachment_type='text/plain')


def test_attach_data_in_parametrized_fixture(attach_data_in_parametrized_fixture):
"""
>>> allure_report = getfixture('allure_report')
>>> for param in PARAMS:
... assert_that(allure_report,
... has_test_case('test_attach_data_in_parametrized_fixture[{param}]'.format(param=param),
... has_container(allure_report,
... has_before('attach_data_in_parametrized_fixture',
... has_attachment(attach_type='text/plain', name=param)
... )
... )
... )
... )
"""
pass
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
"""
>>> allure_report = getfixture('allure_report')
>>> assert_that(allure_report,
... all_of(
... has_property('test_cases', has_length(9)),
... has_property('test_groups', has_length(0)),
... has_property('attachments', has_length(11))
... )) # doctest: +SKIP
"""

import allure
import pytest

Expand Down
33 changes: 12 additions & 21 deletions allure-pytest/test/attachments/attachments_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
"""
>>> allure_report = getfixture('allure_report')
>>> assert_that(allure_report,
... all_of(
... has_property('test_cases', has_length(9)),
... has_property('test_groups', has_length(0)),
... has_property('attachments', has_length(11))
... )) # doctest: +SKIP
"""

import pytest
import allure


def test_attach_file_from_test(xml_file):
Expand All @@ -20,7 +11,7 @@ def test_attach_file_from_test(xml_file):
... )
... )
"""
pytest.allure.attach.file(xml_file)
allure.attach.file(xml_file)


def test_attach_data_from_test(xml_body):
Expand All @@ -32,7 +23,7 @@ def test_attach_data_from_test(xml_body):
... )
... )
"""
pytest.allure.attach(xml_body)
allure.attach(xml_body)


def test_attach_file_from_test_with_name_and_type(xml_file):
Expand All @@ -43,7 +34,7 @@ def test_attach_file_from_test_with_name_and_type(xml_file):
... has_attachment(attach_type='application/xml', name='my name')
... ))
"""
pytest.allure.attach.file(xml_file, name='my name', attachment_type=pytest.allure.attachment_type.XML)
allure.attach.file(xml_file, name='my name', attachment_type=pytest.allure.attachment_type.XML)


def test_attach_data_from_test_with_name_and_type(xml_body):
Expand All @@ -54,7 +45,7 @@ def test_attach_data_from_test_with_name_and_type(xml_body):
... has_attachment(attach_type='application/xml', name='my name')
... ))
"""
pytest.allure.attach(xml_body, name='my name', attachment_type=pytest.allure.attachment_type.XML)
allure.attach(xml_body, name='my name', attachment_type=pytest.allure.attachment_type.XML)


def test_attach_file_from_test_with_user_type(xml_file):
Expand All @@ -65,7 +56,7 @@ def test_attach_file_from_test_with_user_type(xml_file):
... has_attachment(attach_type='text/xml')
... ))
"""
pytest.allure.attach.file(xml_file, attachment_type='text/xml')
allure.attach.file(xml_file, attachment_type='text/xml')


def test_attach_data_from_test_with_user_type(xml_body):
Expand All @@ -76,11 +67,11 @@ def test_attach_data_from_test_with_user_type(xml_body):
... has_attachment(attach_type='text/xml')
... ))
"""
pytest.allure.attach(xml_body, attachment_type='text/xml')
allure.attach(xml_body, attachment_type='text/xml')


def attach_svg_file(svg_file):
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)


def test_attach_file_from_function(svg_file):
Expand All @@ -105,7 +96,7 @@ def test_many_attaches(svg_file, xml_body):
... ))
"""
attach_svg_file(svg_file)
pytest.allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)


def test_attach_from_step(svg_file, xml_body):
Expand All @@ -123,7 +114,7 @@ def test_attach_from_step(svg_file, xml_body):
... )
... )
"""
with pytest.allure.step('Step with attachment'):
pytest.allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
with allure.step('Step with attachment'):
allure.attach.file(svg_file, attachment_type=pytest.allure.attachment_type.SVG)
with pytest.allure.step('Nested step with attachment'):
pytest.allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
allure.attach(xml_body, attachment_type=pytest.allure.attachment_type.XML)
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def test_two_function_scope_parametrized_fixture(parametrized_fixture, parametri
... )
... )
"""
assert parametrized_fixture_with_ids and parametrized_fixture
assert parametrized_fixture_with_ids and parametrized_fixture
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
import allure


PARAMS = ["first", "second", "third"]


@pytest.fixture(scope='module', params=PARAMS)
def module_scope_parametrized_fixture():
pass


def test_module_scope_parametrized_fixture(module_scope_parametrized_fixture):
"""
>>> allure_report = getfixture('allure_report')

>>> for param in PARAMS:
... assert_that(allure_report,
... has_test_case('test_module_scope_parametrized_fixture[{param}]'.format(param=param),
... has_container(allure_report,
... has_before('module_scope_parametrized_fixture')
... )
... )
... )
"""
pass


def test_reuse_module_scope_parametrized_fixture(module_scope_parametrized_fixture):
"""
>>> allure_report = getfixture('allure_report')

>>> for param in PARAMS:
... assert_that(allure_report,
... has_test_case('test_reuse_module_scope_parametrized_fixture[{param}]'.format(param=param),
... has_container(allure_report,
... has_before('module_scope_parametrized_fixture')
... )
... )
... )

>>> for param in PARAMS:
... assert_that(allure_report,
... has_same_container('test_module_scope_parametrized_fixture[{param}]'.format(param=param),
... 'test_reuse_module_scope_parametrized_fixture[{param}]'.format(param=param),
... has_before('module_scope_parametrized_fixture')
... )
... )
"""
pass
42 changes: 42 additions & 0 deletions allure-pytest/test/fixtures/module_scope/fixtures_simple_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest


@pytest.fixture(scope='module')
def module_scope_simple_fixture():
pass


def test_module_scope_simple_fixture(module_scope_simple_fixture):
"""
>>> allure_report = getfixture('allure_report')
>>> assert_that(allure_report,
... has_test_case('test_module_scope_simple_fixture',
... has_container(allure_report,
... has_before('module_scope_simple_fixture')
... )
... )
... )
"""
pass


def test_reuse_module_scope_simple_fixture(module_scope_simple_fixture):
"""
>>> allure_report = getfixture('allure_report')

>>> assert_that(allure_report,
... has_test_case('test_reuse_module_scope_simple_fixture',
... has_container(allure_report,
... has_before('module_scope_simple_fixture')
... )
... )
... )

>>> assert_that(allure_report,
... has_same_container('test_module_scope_simple_fixture',
... 'test_reuse_module_scope_simple_fixture',
... has_before('module_scope_simple_fixture')
... )
... )
"""
pass