From 06ff00fd8f77c5460d9f4d7b5d2e5766c2411ceb Mon Sep 17 00:00:00 2001 From: khvirtru Date: Tue, 17 Sep 2019 09:49:46 -0400 Subject: [PATCH 1/4] Feature/issue 234 add scenario attach method (#1) * ISSUE-234 add scenario attach method --- pytest_bdd/cucumber_json.py | 1 + pytest_bdd/feature.py | 25 +++++++++++++++++++++++++ pytest_bdd/reporting.py | 1 + 3 files changed, 27 insertions(+) diff --git a/pytest_bdd/cucumber_json.py b/pytest_bdd/cucumber_json.py index 61e9c2b4..c3d96e53 100644 --- a/pytest_bdd/cucumber_json.py +++ b/pytest_bdd/cucumber_json.py @@ -143,6 +143,7 @@ def stepmap(step): "keyword": step["keyword"], "name": step_name, "line": step["line_number"], + "embeddings": step['embeddings'], "match": {"location": ""}, "result": self._get_result(step, report, error_message), } diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index 822a382e..ffc660c7 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -454,6 +454,14 @@ def add_step(self, step): step.scenario = self self._steps.append(step) + def attach(self, data, media_type="text/plain"): + """Add attachment to step, such as text, images. + + :param data: actual data of Attachment, can be plain text or base64 encoded + :param media_type: actual media type of Attachment, such as text/plain, image/png + """ + self.steps[-1].attach(data, media_type) + @property def steps(self): """Get scenario steps including background steps. @@ -521,6 +529,7 @@ def __init__(self, name, type, indent, line_number, keyword): self.type = type self.line_number = line_number self.failed = False + self.embeddings = [] self.start = 0 self.stop = 0 self.scenario = None @@ -533,6 +542,22 @@ def add_line(self, line): """ self.lines.append(line) + def attach(self, data, media_type="text/plain"): + """Add attachment to step, such as text, images. + + :param data: actual data of Attachment, can be plain text or base64 encoded + :param media_type: actual media type of Attachment, such as text/plain, image/png + """ + + json_attachment = \ + { + "data": data, + "media": { + "type": media_type + } + } + self.embeddings.append(json_attachment) + @property def name(self): """Get step name.""" diff --git a/pytest_bdd/reporting.py b/pytest_bdd/reporting.py index 45a8e9e7..a37a13bc 100644 --- a/pytest_bdd/reporting.py +++ b/pytest_bdd/reporting.py @@ -33,6 +33,7 @@ def serialize(self): """ return { "name": self.step.name, + "embeddings": self.step.embeddings, "type": self.step.type, "keyword": self.step.keyword, "line_number": self.step.line_number, From 3f4264ff49cba7c0add3eb6530ae44c06bf15f32 Mon Sep 17 00:00:00 2001 From: kenhosr Date: Tue, 17 Sep 2019 12:27:49 -0400 Subject: [PATCH 2/4] update test (#2) * ISSUE-234 add scenario attach method * add embeddings to logreport * add missing data * add media type * add to the end of the test * add embeddings --- tests/feature/test_cucumber_json.py | 6 ++++++ tests/feature/test_report.py | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/tests/feature/test_cucumber_json.py b/tests/feature/test_cucumber_json.py index de41e50b..d7c17a92 100644 --- a/tests/feature/test_cucumber_json.py +++ b/tests/feature/test_cucumber_json.py @@ -124,6 +124,7 @@ def test_passing_outline(): "steps": [ { "keyword": "Given", + "embeddings": [], "line": 6, "match": {"location": ""}, "name": "a passing step", @@ -131,6 +132,7 @@ def test_passing_outline(): }, { "keyword": "And", + "embeddings": [], "line": 7, "match": {"location": ""}, "name": "some other passing step", @@ -149,6 +151,7 @@ def test_passing_outline(): "steps": [ { "keyword": "Given", + "embeddings": [], "line": 11, "match": {"location": ""}, "name": "a passing step", @@ -156,6 +159,7 @@ def test_passing_outline(): }, { "keyword": "And", + "embeddings": [], "line": 12, "match": {"location": ""}, "name": "a failing step", @@ -176,6 +180,7 @@ def test_passing_outline(): "steps": [ { "line": 16, + "embeddings": [], "match": {"location": ""}, "result": {"status": "passed", "duration": equals_any(int)}, "keyword": "Given", @@ -212,6 +217,7 @@ def test_passing_outline(): "steps": [ { "line": 16, + "embeddings": [], "match": {"location": ""}, "result": {"status": "passed", "duration": equals_any(int)}, "keyword": "Given", diff --git a/tests/feature/test_report.py b/tests/feature/test_report.py index 732f0e60..99a0ee9f 100644 --- a/tests/feature/test_report.py +++ b/tests/feature/test_report.py @@ -122,6 +122,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): "steps": [ { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Given", "line_number": 6, @@ -130,6 +131,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "And", "line_number": 7, @@ -159,6 +161,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): "steps": [ { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Given", "line_number": 11, @@ -167,6 +170,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": True, "keyword": "And", "line_number": 12, @@ -195,6 +199,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): "steps": [ { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Given", "line_number": 15, @@ -203,6 +208,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "When", "line_number": 16, @@ -211,6 +217,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Then", "line_number": 17, @@ -246,6 +253,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): "steps": [ { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Given", "line_number": 15, @@ -254,6 +262,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "When", "line_number": 16, @@ -262,6 +271,7 @@ def should_have_left_cucumbers(start_cucumbers, start, eat, left): }, { "duration": equals_any(float), + "embeddings": [], "failed": False, "keyword": "Then", "line_number": 17, From 3391ca82f2540025465f1f16ee538d0036595d45 Mon Sep 17 00:00:00 2001 From: kenhosr Date: Tue, 17 Sep 2019 12:55:40 -0400 Subject: [PATCH 3/4] Feature/issue 234 add scenario attach method (#3) * fix unit tests/linting issues --- pytest_bdd/cucumber_json.py | 2 +- pytest_bdd/feature.py | 4 +--- tests/feature/test_cucumber_json.py | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pytest_bdd/cucumber_json.py b/pytest_bdd/cucumber_json.py index c3d96e53..8fe8f1c4 100644 --- a/pytest_bdd/cucumber_json.py +++ b/pytest_bdd/cucumber_json.py @@ -143,7 +143,7 @@ def stepmap(step): "keyword": step["keyword"], "name": step_name, "line": step["line_number"], - "embeddings": step['embeddings'], + "embeddings": step["embeddings"], "match": {"location": ""}, "result": self._get_result(step, report, error_message), } diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index ffc660c7..9ff1103b 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -548,9 +548,7 @@ def attach(self, data, media_type="text/plain"): :param data: actual data of Attachment, can be plain text or base64 encoded :param media_type: actual media type of Attachment, such as text/plain, image/png """ - - json_attachment = \ - { + json_attachment = { "data": data, "media": { "type": media_type diff --git a/tests/feature/test_cucumber_json.py b/tests/feature/test_cucumber_json.py index d7c17a92..b206c2d5 100644 --- a/tests/feature/test_cucumber_json.py +++ b/tests/feature/test_cucumber_json.py @@ -199,6 +199,7 @@ def test_passing_outline(): "steps": [ { "line": 16, + "embeddings": [], "match": {"location": ""}, "result": {"status": "passed", "duration": equals_any(int)}, "keyword": "Given", From 4a0909512b660cb1ac43c1881b08a8cce3838f00 Mon Sep 17 00:00:00 2001 From: kenhosr Date: Tue, 17 Sep 2019 13:25:57 -0400 Subject: [PATCH 4/4] Feature/issue 234 add scenario attach method (#4) * fixed lint issues of feature.py --- pytest_bdd/feature.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pytest_bdd/feature.py b/pytest_bdd/feature.py index 9ff1103b..131c7778 100644 --- a/pytest_bdd/feature.py +++ b/pytest_bdd/feature.py @@ -548,12 +548,7 @@ def attach(self, data, media_type="text/plain"): :param data: actual data of Attachment, can be plain text or base64 encoded :param media_type: actual media type of Attachment, such as text/plain, image/png """ - json_attachment = { - "data": data, - "media": { - "type": media_type - } - } + json_attachment = {"data": data, "media": {"type": media_type}} self.embeddings.append(json_attachment) @property