Skip to content

Commit

Permalink
Add Context.attach, docs and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Korijn authored and bittner committed Dec 14, 2020
1 parent 4ab1e17 commit 29b6d47
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
6 changes: 4 additions & 2 deletions behave/formatter/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,12 @@ def result(self, step):
self._step_index += 1

def embedding(self, mime_type, data):
step = self.current_feature_element["steps"][-1]
step = self.current_feature_element["steps"][self._step_index]
if "embeddings" not in step:
step["embeddings"] = []
step["embeddings"].append({
"mime_type": mime_type,
"data": base64.b64encode(data).replace("\n", ""),
"data": base64.b64encode(data).decode(self.stream.encoding or "utf-8"),
})

def eof(self):
Expand Down
11 changes: 11 additions & 0 deletions behave/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ def internal_cleanup_func():
# -- AVOID DUPLICATES:
current_frame["@cleanups"].append(internal_cleanup_func)

def attach(self, mime_type, data):
"""Embeds data (e.g. a screenshot) in reports for all
formatters that support it, such as the JSON formatter.
:param mime_type: MIME type of the binary data.
:param data: Bytes-like object to embed.
"""
is_compatible = lambda f: hasattr(f, "embedding")
for formatter in filter(is_compatible, self._runner.formatters):
formatter.embedding(mime_type, data)


@contextlib.contextmanager
def use_context_with_mode(context, mode):
Expand Down
18 changes: 18 additions & 0 deletions docs/formatters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ teamcity :pypi:`behave-teamcity`, a formatter for Jetbrains TeamCity CI te
[behave.formatters]
allure = allure_behave.formatter:AllureFormatter
teamcity = behave_teamcity:TeamcityFormatter
Embedding data (e.g. screenshots) in reports
------------------------------------------------------------------------------

You can embed data in reports with the :class:`~behave.runner.Context` method
:func:`~behave.runner.Context.attach`, if you have configured a formatter that
supports it. Currently only the JSON formatter supports embedding data.

For example:

.. code-block:: python
@when(u'I open the Google webpage')
def step_impl(context):
context.browser.get('http://www.google.com')
img = context.browser.get_full_page_screenshot_as_png()
context.attach("image/png", img)
42 changes: 42 additions & 0 deletions features/formatter.json.feature
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,48 @@ Feature: JSON Formatter
But note that "both matched arguments.values are provided as string"


Scenario: Use JSON formatter and embed binary data in report from two steps
Given a file named "features/json_embeddings.feature" with:
"""
Feature:
Scenario: Use embeddings
Given "foobar" as plain text
And "red" as plain text
"""
And a file named "features/steps/json_embeddings_steps.py" with:
"""
from behave import step
@step('"{data}" as plain text')
def step_string(context, data):
context.attach("text/plain", data.encode("utf-8"))
"""
When I run "behave -f json.pretty features/json_embeddings.feature"
Then it should pass with:
"""
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
"""
And the command output should contain:
"""
"embeddings": [
{
"data": "Zm9vYmFy",
"mime_type": "text/plain"
}
],
"""
And the command output should contain:
"""
"embeddings": [
{
"data": "cmVk",
"mime_type": "text/plain"
}
],
"""


@xfail
@regression_problem.with_duration
Scenario: Use JSON formatter with feature and one scenario with steps
Expand Down

0 comments on commit 29b6d47

Please sign in to comment.