pytest-bdd/tests/feature/test_cucumber_json.py

308 lines
10 KiB
Python
Raw Normal View History

2014-04-09 06:40:30 +08:00
"""Test cucumber json output."""
import json
2014-05-11 22:43:45 +08:00
import os.path
2014-04-09 06:40:30 +08:00
import textwrap
def runandparse(testdir, *args):
"""Run tests in testdir and parse json output."""
resultpath = testdir.tmpdir.join("cucumber.json")
2014-05-11 19:18:43 +08:00
result = testdir.runpytest('--cucumberjson={0}'.format(resultpath), '-s', *args)
2014-05-11 17:26:18 +08:00
jsonobject = json.load(resultpath.open())
2014-04-09 06:40:30 +08:00
return result, jsonobject
class equals_any(object):
2014-05-11 21:26:10 +08:00
"""Helper object comparison to which is always 'equal'."""
def __init__(self, type=None):
self.type = type
def __eq__(self, other):
return isinstance(other, self.type) if self.type else True
def __cmp__(self, other):
return 0 if (isinstance(other, self.type) if self.type else False) else -1
2014-05-11 21:26:10 +08:00
string = type(u'')
2014-05-11 21:26:10 +08:00
def test_step_trace(testdir):
2014-04-09 06:40:30 +08:00
"""Test step trace."""
testdir.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
Scenario: Passing outline
Given type <type> and value <value>
Examples: example1
| type | value |
| str | hello |
| int | 42 |
| float | 1.0 |
2014-04-09 06:40:30 +08:00
"""))
testdir.makepyfile(textwrap.dedent("""
import pytest
from pytest_bdd import given, when, scenario
@given('a passing step')
def a_passing_step():
return 'pass'
2014-07-26 04:17:58 +08:00
@given('some other passing step')
def some_other_passing_step():
return 'pass'
2014-04-09 06:40:30 +08:00
@given('a failing step')
2014-05-11 17:21:09 +08:00
def a_failing_step():
2014-04-09 06:40:30 +08:00
raise Exception('Error')
@given('type <type> and value <value>')
def type_type_and_value_value():
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
2014-04-09 06:40:30 +08:00
"""))
result, jsonobject = runandparse(testdir)
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,
2014-04-09 06:40:30 +08:00
"match": {
2014-05-11 22:15:11 +08:00
"location": ""
2014-04-09 06:40:30 +08:00
},
"name": "a passing step",
"result": {
2014-09-11 07:24:14 +08:00
"status": "passed",
"duration": equals_any(int)
2014-04-09 06:40:30 +08:00
}
2014-07-26 04:17:58 +08:00
},
{
"keyword": "And",
2014-07-27 19:42:56 +08:00
"line": 7,
2014-07-26 04:17:58 +08:00
"match": {
"location": ""
},
"name": "some other passing step",
"result": {
2014-09-11 07:24:14 +08:00
"status": "passed",
"duration": equals_any(int)
2014-07-26 04:17:58 +08:00
}
2014-04-09 06:40:30 +08:00
}
2014-07-26 04:17:58 +08:00
2014-04-09 06:40:30 +08:00
],
2014-07-27 19:42:56 +08:00
"tags": [
{
'name': 'scenario-passing-tag',
2014-07-27 19:42:56 +08:00
'line': 4,
}
],
2014-04-09 06:40:30 +08:00
"type": "scenario"
},
{
"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,
2014-04-09 06:40:30 +08:00
"match": {
2014-05-11 22:15:11 +08:00
"location": ""
2014-04-09 06:40:30 +08:00
},
2014-07-26 20:37:55 +08:00
"name": "a passing step",
"result": {
2014-09-11 07:24:14 +08:00
"status": "passed",
"duration": equals_any(int)
2014-07-26 20:37:55 +08:00
}
},
{
"keyword": "And",
2014-07-27 19:42:56 +08:00
"line": 12,
2014-07-26 20:37:55 +08:00
"match": {
"location": ""
},
2014-04-09 06:40:30 +08:00
"name": "a failing step",
"result": {
"error_message": equals_any(string),
2014-09-11 07:24:14 +08:00
"status": "failed",
"duration": equals_any(int)
2014-04-09 06:40:30 +08:00
}
}
],
2014-07-27 19:42:56 +08:00
"tags": [
{
'name': 'scenario-failing-tag',
2014-07-27 19:42:56 +08:00
'line': 9,
}
],
2014-04-09 06:40:30 +08:00
"type": "scenario"
},
{
"description": "",
"keyword": "Scenario",
"tags": [
{
"line": 14,
"name": "scenario-outline-passing-tag"
}
],
"steps": [
{
"line": 16,
"match": {"location": ""},
"result": {
"status": "passed",
"duration": equals_any(int)
},
"keyword": "Given",
"name": "type <type> and value <value>"
}
],
"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": equals_any(int)
},
"keyword": "Given",
"name": "type <type> and value <value>"
}
],
"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": equals_any(int)
},
"keyword": "Given",
"name": "type <type> and value <value>"
}
],
"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",
2014-07-27 19:42:56 +08:00
"tags": [
{
'name': 'feature-tag',
2014-07-27 19:42:56 +08:00
'line': 1,
}
],
2014-05-11 22:47:37 +08:00
"uri": os.path.join(testdir.tmpdir.basename, 'test.feature'),
2014-04-09 06:40:30 +08:00
}
]
2014-07-27 19:42:56 +08:00
assert jsonobject == expected
def test_step_trace_with_expand_option(testdir):
"""Test step trace."""
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 <type> and value <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 <type> and value <value>')
def type_type_and_value_value():
return 'pass'
@scenario('test.feature', 'Passing outline')
def test_passing_outline():
pass
"""))
result, jsonobject = runandparse(testdir, '--cucumber-json-expand')
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"