pytest-bdd/tests/feature/test_cucumber_json.py

176 lines
5.4 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
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')
@scenario('test.feature', 'Passing')
def test_passing():
pass
@scenario('test.feature', 'Failing')
def test_failing():
pass
"""))
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"
}
],
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