Skip to content

Commit

Permalink
Merge pull request #1 from fundakol/developing
Browse files Browse the repository at this point in the history
handling http errors
  • Loading branch information
fundakol committed Apr 11, 2021
2 parents ed4d4cb + 45ab250 commit d09b6e2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 58 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# pytest-jira-xray

[![PyPi](https://img.shields.io/pypi/v/pytest-jira-xray.png)](https://pypi.python.org/pypi/pytest-jira-xray)

pytest-jira-xray is a plugin for pytest that uploads test results to JIRA XRAY.

### Installation

```commandline
pip install pytest-jira-xray
```

or

```commandline
python setup.py install
```
Expand Down
35 changes: 35 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[metadata]
name=pytest-jira-xray
version=0.1.1
url=https://github.com/fundakol/pytest-jira-xray
author=Lukasz Fundakowski
author_email[email protected]
description=pytest plugin to integrate tests with JIRA XRAY
long_description=file: README.md
long_description_content_type=text/markdown
python_requires=>=3.6
keywords=pytest,JIRA,XRAY
classifiers=
Development Status :: 3 - Alpha
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
License :: OSI Approved :: Apache Software License
Operating System :: OS Independent

[options]
packages=find:
package_dir=
=src
install_requires=
pytest
requests

[options.packages.find]
where=src

[options.entry_points]
pytest11=
xray=pytest_xray.plugin
41 changes: 2 additions & 39 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
from setuptools import setup, find_packages
from setuptools import setup

__version__ = '0.1.0'

with open('README.md', 'r') as fh:
long_description = fh.read()

classifiers = [
'Development Status :: 3 - Alpha',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'License :: OSI Approved :: Apache Software License',
'Operating System :: OS Independent',
]

setup(
name='pytest-jira-xray',
version=__version__,
packages=find_packages('src'),
package_dir={"": "src"},
url='https://github.com/fundakol/pytest-jira-xray',
author='Lukasz Fundakowski',
author_email='[email protected]',
description='pytest plugin to integrate tests with JIRA XRAY',
long_description=long_description,
python_requires='>=3.6',
install_requires=[
'pytest',
'requests'
],
keywords='pytest JIRA XRAY',
classifiers=classifiers,
entry_points={
'pytest11': [
'xray = pytest_xray.plugin',
]
},
)
setup()
29 changes: 20 additions & 9 deletions src/pytest_xray/plugin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
from os import environ
from typing import List

from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.nodes import Item
from _pytest.terminal import TerminalReporter

from pytest_xray.constant import JIRA_XRAY_FLAG, XRAY_PLUGIN, XRAY_TESTPLAN_ID, XRAY_EXECUTION_ID
from pytest_xray.helper import (associate_marker_metadata_for, get_test_key_for, Status, TestCase,
from pytest_xray.helper import (associate_marker_metadata_for,
get_test_key_for,
Status,
TestCase,
TestExecution)
from pytest_xray.xray_publisher import XrayPublisher


def pytest_configure(config):
def pytest_configure(config: Config) -> None:
if not config.getoption(JIRA_XRAY_FLAG):
return

jira_url = str(environ["XRAY_API_BASE_URL"])
auth = (str(environ["XRAY_API_USER"]), str(environ["XRAY_API_PASSWORD"]))
jira_url = environ["XRAY_API_BASE_URL"]
auth = (environ["XRAY_API_USER"], environ["XRAY_API_PASSWORD"])

plugin = XrayPublisher(base_url=jira_url, auth=auth)
config.pluginmanager.register(plugin, XRAY_PLUGIN)

config.addinivalue_line(
"markers", "xray: mark test to run only on named environment"
"markers", "xray(JIRA_ID): mark test with JIRA XRAY test case ID"
)


def pytest_addoption(parser):
def pytest_addoption(parser: Parser):
xray = parser.getgroup('Jira Xray report')
xray.addoption(JIRA_XRAY_FLAG,
action="store_true",
Expand All @@ -37,15 +46,15 @@ def pytest_addoption(parser):
help="XRAY Test Plan ID")


def pytest_collection_modifyitems(config, items):
def pytest_collection_modifyitems(config: Config, items: List[Item]) -> None:
if not config.getoption(JIRA_XRAY_FLAG):
return

for item in items:
associate_marker_metadata_for(item)


def pytest_terminal_summary(terminalreporter):
def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
if not terminalreporter.config.getoption(JIRA_XRAY_FLAG):
return
test_execution_id = terminalreporter.config.getoption(XRAY_EXECUTION_ID)
Expand Down Expand Up @@ -75,4 +84,6 @@ def pytest_terminal_summary(terminalreporter):
test_execution.append(tc)

publish_results = terminalreporter.config.pluginmanager.get_plugin(XRAY_PLUGIN)
publish_results.publish(test_execution)
status = publish_results.publish(test_execution)
if status:
print('Test results published to JIRA-XRAY:', publish_results.base_url)
35 changes: 25 additions & 10 deletions src/pytest_xray/xray_publisher.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import json
import logging
from typing import Union

import requests
from requests.auth import AuthBase

from pytest_xray.constant import TEST_EXEXUTION_ENDPOINT
from pytest_xray.helper import TestExecution
from requests.auth import AuthBase

logging.basicConfig()
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)


class XrayError(Exception):
Expand All @@ -25,18 +31,27 @@ def publish_xray_results(self, url: str, auth: AuthBase, data: dict) -> dict:
headers = {'Accept': 'application/json',
'Content-Type': 'application/json'}
data = json.dumps(data)
response = requests.request(method='POST', url=url, headers=headers, data=data, auth=auth)
try:
response.raise_for_status()
except Exception as e:
response = requests.request(method='POST', url=url, headers=headers, data=data, auth=auth)
except requests.exceptions.ConnectionError as e:
_logger.exception('ConnectionError to JIRA service %s', self.base_url)
raise XrayError(e)
return response.json()

def publish(self, test_execution: TestExecution) -> None:
else:
try:
response.raise_for_status()
except Exception as e:
_logger.error('Could not post to JIRA service %s. Response status code: %s',
self.base_url, response.status_code)
raise XrayError(e)
return response.json()

def publish(self, test_execution: TestExecution) -> bool:
try:
result = self.publish_xray_results(self.endpoint_url, self.auth, test_execution.as_dict())
except XrayError as e:
print('Could not publish to Jira:', e)
except XrayError:
_logger.error('Could not publish results to Jira XRAY')
return False
else:
key = result['testExecIssue']['key']
print('Uploaded results to XRAY Test Execution:', key)
_logger.info('Uploaded results to JIRA XRAY Test Execution: %s', key)
return True

0 comments on commit d09b6e2

Please sign in to comment.