Skip to content

Commit

Permalink
feature: ability to attach attachments to step implementations
Browse files Browse the repository at this point in the history
Implement the `qase.attach()` method which adds attachments to a step.
  • Loading branch information
gibiw committed Aug 2, 2024
1 parent b15b01b commit c4e0517
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
15 changes: 15 additions & 0 deletions qase-robotframework/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# qase-pytest 3.1.0b3

## What's new

Added the ability to attach attachments to step implementations.

```python
from qase.robotframework.method import qase

def step01(a: int, b: int):
qase.attach("/some_path/file.xml")
qase.attach((str.encode("This is a simple string attachment"), "text/plain", "simple.txt"))
return str(a + b)
```

# qase-pytest 3.1.0b1

## What's new
Expand Down
2 changes: 1 addition & 1 deletion qase-robotframework/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "qase-robotframework"
version = "3.1.0b2"
version = "3.1.0b3"
description = "Qase Robot Framework Plugin"
readme = "README.md"
authors = [{name = "Qase Team", email = "[email protected]"}]
Expand Down
4 changes: 2 additions & 2 deletions qase-robotframework/src/qase/robotframework/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from qase.commons.models import Result, Suite, Step, Field
from qase.commons.models.step import StepType, StepGherkinData
from qase.commons.reporters import QaseCoreReporter
from qase.commons.models.runtime import Runtime

from .plugin import QaseRuntimeSingleton
from .types import STATUSES
from .models import *

Expand All @@ -21,7 +21,7 @@ class Listener:
def __init__(self):
config = ConfigManager()
self.reporter = QaseCoreReporter(config)
self.runtime = Runtime()
self.runtime = QaseRuntimeSingleton.get_instance()
self.step_uuid = None
self.suite = {}

Expand Down
46 changes: 46 additions & 0 deletions qase-robotframework/src/qase/robotframework/method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Union, Tuple
from qase.commons import QaseUtils
import mimetypes
from qase.commons.models.attachment import Attachment

from .plugin import QaseRuntimeSingleton


class qase:
"""Class with decorators for robotframework"""

@staticmethod
def attach(
self, *files: Union[str, Tuple[str, str], Tuple[bytes, str, str]]
):
"""
Attach files to test results
`files` could be:
- str - only `filepath`
- str, str - `filepath` and `mime-type` for it
- bytes, str, str - `source` data, `mime-type` and `filename`
>>> from src.client.models import MimeTypes
... qase.attach(
... (driver.get_screenshot_as_png(), MimeTypes.PNG, "page.png")
... )
"""
for file in files:
filename = None
content = None
file_path = None

if isinstance(file, tuple):
if len(file) == 2:
file_path, mime = file
filename = QaseUtils.get_filename(file_path)
else:
content, mime, filename = file
elif isinstance(file, str):
file_path = file
mime = mimetypes.guess_type(file)[0]
filename = QaseUtils.get_filename(file_path)

attachment = Attachment(file_name=filename, content=content, mime_type=mime, file_path=file_path)
QaseRuntimeSingleton.get_instance().add_attachment(attachment)
16 changes: 16 additions & 0 deletions qase-robotframework/src/qase/robotframework/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from qase.commons.models.runtime import Runtime


class QaseRuntimeSingleton:
_instance: Runtime = None

@staticmethod
def get_instance() -> Runtime:
""" Static access method"""
if QaseRuntimeSingleton._instance is None:
QaseRuntimeSingleton._instance = Runtime()
return QaseRuntimeSingleton._instance

def __init__(self):
""" Virtually private constructor"""
raise Exception("Use get_instance()")

0 comments on commit c4e0517

Please sign in to comment.