From 675497b63ff7a38265e9db51cc2046695d29dfd2 Mon Sep 17 00:00:00 2001 From: Shawn P Emhe II Date: Mon, 17 Feb 2020 23:27:09 -0800 Subject: [PATCH 1/3] Creates path to json file if it does not exist Fixes error when attempting to create a cucumber json in a directory has not been created yet. --- pytest_bdd/cucumber_json.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pytest_bdd/cucumber_json.py b/pytest_bdd/cucumber_json.py index 193bdefb..a7c2c770 100644 --- a/pytest_bdd/cucumber_json.py +++ b/pytest_bdd/cucumber_json.py @@ -171,6 +171,8 @@ def pytest_sessionstart(self): self.suite_start_time = time.time() def pytest_sessionfinish(self): + if not os.path.exists(os.path.dirname(self.logfile)): + os.makedirs(os.path.dirname(self.logfile)) if sys.version_info[0] < 3: logfile_open = codecs.open else: From 25b7867328db018dcefbe02340d03283e00a0eb3 Mon Sep 17 00:00:00 2001 From: Shawn P Emhe II Date: Sun, 1 Mar 2020 13:12:15 -0800 Subject: [PATCH 2/3] Added test_step_trace_with_missing_directory Modified runandparse to check if the new test has added a "missing_subdirectory" attribute. Verified that this doesn't break the functionality of the already existing cucumber tests. --- tests/feature/test_cucumber_json.py | 61 ++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/feature/test_cucumber_json.py b/tests/feature/test_cucumber_json.py index de41e50b..fc945ec5 100644 --- a/tests/feature/test_cucumber_json.py +++ b/tests/feature/test_cucumber_json.py @@ -6,7 +6,10 @@ def runandparse(testdir, *args): """Run tests in testdir and parse json output.""" - resultpath = testdir.tmpdir.join("cucumber.json") + if hasattr(testdir, "missing_subdirectory"): + resultpath = testdir.missing_subdirectory.join("cucumber.json") + else: + resultpath = testdir.tmpdir.join("cucumber.json") result = testdir.runpytest("--cucumberjson={0}".format(resultpath), "-s", *args) jsonobject = json.load(resultpath.open()) return result, jsonobject @@ -290,3 +293,59 @@ def test_passing_outline(): assert jsonobject[0]["elements"][0]["steps"][0]["name"] == "type str and value hello" assert jsonobject[0]["elements"][1]["steps"][0]["name"] == "type int and value 42" assert jsonobject[0]["elements"][2]["steps"][0]["name"] == "type float and value 1.0" + +def test_step_trace_with_missing_directory(testdir): + """Test step trace.""" + testdir.makefile( + ".ini", + pytest=textwrap.dedent( + """ + [pytest] + markers = + feature-tag + scenario-outline-passing-tag + """ + ), + ) + testdir.makefile( + ".feature", + test=textwrap.dedent( + """ + @feature-tag + Feature: One scenario outline, expanded to multiple scenarios + + @scenario-outline-passing-tag + Scenario: Passing outline + Given type and value + + Examples: example1 + | type | value | + | str | hello | + | int | 42 | + | float | 1.0 | + """ + ), + ) + testdir.makepyfile( + textwrap.dedent( + """ + import pytest + from pytest_bdd import given, scenario + + @given('type and value ') + def type_type_and_value_value(): + return 'pass' + + @scenario('test.feature', 'Passing outline') + def test_passing_outline(): + pass + """ + ) + ) + testdir.missing_subdirectory = testdir.tmpdir.join("a") + result, jsonobject = runandparse(testdir, "--cucumber-json-expanded") + assert result.ret == 0 + + assert jsonobject[0]["elements"][0]["steps"][0]["name"] == "type str and value hello" + assert jsonobject[0]["elements"][1]["steps"][0]["name"] == "type int and value 42" + assert jsonobject[0]["elements"][2]["steps"][0]["name"] == "type float and value 1.0" \ No newline at end of file From 1ec12d6a17a0fd306d37f8678a524a9b7d19bf0d Mon Sep 17 00:00:00 2001 From: Shawn P Emhe II Date: Sun, 1 Mar 2020 14:01:29 -0800 Subject: [PATCH 3/3] Changed test approach and fixed formatting Using a fixture now to run all cucumber tests with and without missing subdirectory. Also fixed formatting issues identified in build test. --- tests/feature/test_cucumber_json.py | 66 ++++------------------------- 1 file changed, 9 insertions(+), 57 deletions(-) diff --git a/tests/feature/test_cucumber_json.py b/tests/feature/test_cucumber_json.py index fc945ec5..ec20d089 100644 --- a/tests/feature/test_cucumber_json.py +++ b/tests/feature/test_cucumber_json.py @@ -2,6 +2,14 @@ import json import os.path import textwrap +import pytest + + +@pytest.fixture(params=[None, "a"]) +def testdir(request, testdir): + if request.param: + testdir.missing_subdirectory = testdir.tmpdir.join(request.param) + return testdir def runandparse(testdir, *args): @@ -227,7 +235,7 @@ def test_passing_outline(): "name": "Passing outline", }, ], - "id": os.path.join("test_step_trace0", "test.feature"), + "id": os.path.join(testdir.tmpdir.basename, "test.feature"), "keyword": "Feature", "line": 2, "name": "One passing scenario, one failing scenario", @@ -293,59 +301,3 @@ def test_passing_outline(): assert jsonobject[0]["elements"][0]["steps"][0]["name"] == "type str and value hello" assert jsonobject[0]["elements"][1]["steps"][0]["name"] == "type int and value 42" assert jsonobject[0]["elements"][2]["steps"][0]["name"] == "type float and value 1.0" - -def test_step_trace_with_missing_directory(testdir): - """Test step trace.""" - testdir.makefile( - ".ini", - pytest=textwrap.dedent( - """ - [pytest] - markers = - feature-tag - scenario-outline-passing-tag - """ - ), - ) - testdir.makefile( - ".feature", - test=textwrap.dedent( - """ - @feature-tag - Feature: One scenario outline, expanded to multiple scenarios - - @scenario-outline-passing-tag - Scenario: Passing outline - Given type and value - - Examples: example1 - | type | value | - | str | hello | - | int | 42 | - | float | 1.0 | - """ - ), - ) - testdir.makepyfile( - textwrap.dedent( - """ - import pytest - from pytest_bdd import given, scenario - - @given('type and value ') - def type_type_and_value_value(): - return 'pass' - - @scenario('test.feature', 'Passing outline') - def test_passing_outline(): - pass - """ - ) - ) - testdir.missing_subdirectory = testdir.tmpdir.join("a") - result, jsonobject = runandparse(testdir, "--cucumber-json-expanded") - assert result.ret == 0 - - assert jsonobject[0]["elements"][0]["steps"][0]["name"] == "type str and value hello" - assert jsonobject[0]["elements"][1]["steps"][0]["name"] == "type int and value 42" - assert jsonobject[0]["elements"][2]["steps"][0]["name"] == "type float and value 1.0" \ No newline at end of file