From c4e05170ef4661de838b1280911cb7ec3badaf35 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Fri, 2 Aug 2024 15:41:33 +0200 Subject: [PATCH] feature: ability to attach attachments to step implementations Implement the `qase.attach()` method which adds attachments to a step. --- qase-robotframework/changelog.md | 15 ++++++ qase-robotframework/pyproject.toml | 2 +- .../src/qase/robotframework/listener.py | 4 +- .../src/qase/robotframework/method.py | 46 +++++++++++++++++++ .../src/qase/robotframework/plugin.py | 16 +++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 qase-robotframework/src/qase/robotframework/method.py create mode 100644 qase-robotframework/src/qase/robotframework/plugin.py diff --git a/qase-robotframework/changelog.md b/qase-robotframework/changelog.md index f48039e0..0c60bab2 100644 --- a/qase-robotframework/changelog.md +++ b/qase-robotframework/changelog.md @@ -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 diff --git a/qase-robotframework/pyproject.toml b/qase-robotframework/pyproject.toml index 6c6ce9ff..509db0c0 100644 --- a/qase-robotframework/pyproject.toml +++ b/qase-robotframework/pyproject.toml @@ -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 = "support@qase.io"}] diff --git a/qase-robotframework/src/qase/robotframework/listener.py b/qase-robotframework/src/qase/robotframework/listener.py index 06d328de..7059638f 100644 --- a/qase-robotframework/src/qase/robotframework/listener.py +++ b/qase-robotframework/src/qase/robotframework/listener.py @@ -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 * @@ -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 = {} diff --git a/qase-robotframework/src/qase/robotframework/method.py b/qase-robotframework/src/qase/robotframework/method.py new file mode 100644 index 00000000..0272f92f --- /dev/null +++ b/qase-robotframework/src/qase/robotframework/method.py @@ -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) diff --git a/qase-robotframework/src/qase/robotframework/plugin.py b/qase-robotframework/src/qase/robotframework/plugin.py new file mode 100644 index 00000000..f4ff5d30 --- /dev/null +++ b/qase-robotframework/src/qase/robotframework/plugin.py @@ -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()")