pytest-bdd/tests/feature/test_cucumber_json.py

235 lines
7.7 KiB
Python
Raw Permalink Normal View History

2014-04-09 06:40:30 +08:00
"""Test cucumber json output."""
2022-03-03 20:49:05 +08:00
from __future__ import annotations
2014-04-09 06:40:30 +08:00
import json
2014-05-11 22:43:45 +08:00
import os.path
2014-04-09 06:40:30 +08:00
import textwrap
2022-03-03 20:43:01 +08:00
from typing import TYPE_CHECKING, Any
2014-04-09 06:40:30 +08:00
2021-10-02 02:51:59 +08:00
if TYPE_CHECKING:
2022-11-04 17:51:48 +08:00
from _pytest.pytester import Pytester, RunResult
2014-04-09 06:40:30 +08:00
def runandparse(pytester: Pytester, *args: Any) -> tuple[RunResult, list[dict[str, Any]]]:
2014-04-09 06:40:30 +08:00
"""Run tests in testdir and parse json output."""
resultpath = pytester.path.joinpath("cucumber.json")
result = pytester.runpytest(f"--cucumberjson={resultpath}", "-s", *args)
with resultpath.open() as f:
jsonobject = json.load(f)
2014-04-09 06:40:30 +08:00
return result, jsonobject
class OfType:
"""Helper object to help compare object type to initialization type"""
2014-05-11 21:26:10 +08:00
def __init__(self, type: type = None) -> None:
self.type = type
2022-02-24 23:53:27 +08:00
def __eq__(self, other: object) -> bool:
return isinstance(other, self.type) if self.type else True
2014-05-11 21:26:10 +08:00
def test_step_trace(pytester):
2014-04-09 06:40:30 +08:00
"""Test step trace."""
pytester.makefile(
".ini",
pytest=textwrap.dedent(
"""
2019-07-04 23:24:40 +08:00
[pytest]
markers =
scenario-passing-tag
scenario-failing-tag
scenario-outline-passing-tag
feature-tag
"""
),
)
pytester.makefile(
".feature",
test=textwrap.dedent(
"""
2014-07-27 19:42:56 +08:00
@feature-tag
2014-04-09 06:40:30 +08:00
Feature: One passing scenario, one failing scenario
2014-07-27 19:42:56 +08:00
@scenario-passing-tag
Scenario: Passing
Given a passing step
And some other passing step
2014-04-09 06:40:30 +08:00
2014-07-27 19:42:56 +08:00
@scenario-failing-tag
Scenario: Failing
Given a passing step
And a failing step
@scenario-outline-passing-tag
2022-07-04 20:29:01 +08:00
Scenario Outline: Passing outline
Given type <type> and value <value>
Examples: example1
| type | value |
| str | hello |
| int | 42 |
| float | 1.0 |
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""
2014-04-09 06:40:30 +08:00
import pytest
from pytest_bdd import given, when, scenario, parsers
2014-04-09 06:40:30 +08:00
@given('a passing step')
def _():
2014-04-09 06:40:30 +08:00
return 'pass'
2014-07-26 04:17:58 +08:00
@given('some other passing step')
def _():
2014-07-26 04:17:58 +08:00
return 'pass'
2014-04-09 06:40:30 +08:00
@given('a failing step')
def _():
2014-04-09 06:40:30 +08:00
raise Exception('Error')
@given(parsers.parse('type {type} and value {value}'))
def _():
return 'pass'
2014-04-09 06:40:30 +08:00
@scenario('test.feature', 'Passing')
def test_passing():
pass
@scenario('test.feature', 'Failing')
def test_failing():
pass
@scenario('test.feature', 'Passing outline')
def test_passing_outline():
pass
"""
)
)
result, jsonobject = runandparse(pytester)
result.assert_outcomes(passed=4, failed=1)
2014-04-09 06:40:30 +08:00
assert result.ret
2014-07-27 19:42:56 +08:00
expected = [
2014-04-09 06:40:30 +08:00
{
"description": "",
"elements": [
{
"description": "",
2014-05-11 21:52:18 +08:00
"id": "test_passing",
2014-04-09 06:40:30 +08:00
"keyword": "Scenario",
2014-07-27 19:42:56 +08:00
"line": 5,
2014-04-09 06:40:30 +08:00
"name": "Passing",
"steps": [
{
2014-05-11 22:15:11 +08:00
"keyword": "Given",
2014-07-27 19:42:56 +08:00
"line": 6,
"match": {"location": ""},
2014-04-09 06:40:30 +08:00
"name": "a passing step",
"result": {"status": "passed", "duration": OfType(int)},
2014-07-26 04:17:58 +08:00
},
{
"keyword": "And",
2014-07-27 19:42:56 +08:00
"line": 7,
"match": {"location": ""},
2014-07-26 04:17:58 +08:00
"name": "some other passing step",
"result": {"status": "passed", "duration": OfType(int)},
},
2014-07-27 19:42:56 +08:00
],
"tags": [{"name": "scenario-passing-tag", "line": 4}],
"type": "scenario",
2014-04-09 06:40:30 +08:00
},
{
"description": "",
2014-05-11 21:52:18 +08:00
"id": "test_failing",
2014-04-09 06:40:30 +08:00
"keyword": "Scenario",
2014-07-27 19:42:56 +08:00
"line": 10,
2014-04-09 06:40:30 +08:00
"name": "Failing",
"steps": [
{
2014-05-11 22:15:11 +08:00
"keyword": "Given",
2014-07-27 19:42:56 +08:00
"line": 11,
"match": {"location": ""},
2014-07-26 20:37:55 +08:00
"name": "a passing step",
"result": {"status": "passed", "duration": OfType(int)},
2014-07-26 20:37:55 +08:00
},
{
"keyword": "And",
2014-07-27 19:42:56 +08:00
"line": 12,
"match": {"location": ""},
2014-04-09 06:40:30 +08:00
"name": "a failing step",
"result": {"error_message": OfType(str), "status": "failed", "duration": OfType(int)},
},
2014-07-27 19:42:56 +08:00
],
"tags": [{"name": "scenario-failing-tag", "line": 9}],
"type": "scenario",
},
{
"description": "",
"keyword": "Scenario",
"tags": [{"line": 14, "name": "scenario-outline-passing-tag"}],
"steps": [
{
"line": 16,
"match": {"location": ""},
"result": {"status": "passed", "duration": OfType(int)},
"keyword": "Given",
"name": "type str and value hello",
}
],
"line": 15,
"type": "scenario",
"id": "test_passing_outline[str-hello]",
"name": "Passing outline",
},
{
"description": "",
"keyword": "Scenario",
"tags": [{"line": 14, "name": "scenario-outline-passing-tag"}],
"steps": [
{
"line": 16,
"match": {"location": ""},
"result": {"status": "passed", "duration": OfType(int)},
"keyword": "Given",
"name": "type int and value 42",
}
],
"line": 15,
"type": "scenario",
"id": "test_passing_outline[int-42]",
"name": "Passing outline",
},
{
"description": "",
"keyword": "Scenario",
"tags": [{"line": 14, "name": "scenario-outline-passing-tag"}],
"steps": [
{
"line": 16,
"match": {"location": ""},
"result": {"status": "passed", "duration": OfType(int)},
"keyword": "Given",
"name": "type float and value 1.0",
}
],
"line": 15,
"type": "scenario",
"id": "test_passing_outline[float-1.0]",
"name": "Passing outline",
},
2014-04-09 06:40:30 +08:00
],
2016-08-19 05:32:21 +08:00
"id": os.path.join("test_step_trace0", "test.feature"),
2014-04-09 06:40:30 +08:00
"keyword": "Feature",
2014-07-27 19:42:56 +08:00
"line": 2,
2014-04-09 06:40:30 +08:00
"name": "One passing scenario, one failing scenario",
"tags": [{"name": "feature-tag", "line": 1}],
"uri": os.path.join(pytester.path.name, "test.feature"),
2014-04-09 06:40:30 +08:00
}
]
2014-07-27 19:42:56 +08:00
assert jsonobject == expected