forked from allure-framework/allure-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
163 lines (129 loc) · 4.39 KB
/
report.py
File metadata and controls
163 lines (129 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
>>> from hamcrest import assert_that
>>> class Report(object):
... def __init__(self):
... self.test_cases = [
... {
... 'fullName': 'package.module.test',
... 'id': '1'
... },
... {
... 'fullName': 'package.module.test[param]',
... 'id': '2'
... },
... {
... 'fullName': 'package.module.Class#test[param]',
... 'id': '3'
... }
... ]
>>> assert_that(Report(),
... has_test_case('test')
... )
>>> assert_that(Report(),
... has_test_case('test[param]')
... )
>>> assert_that(Report(),
... has_test_case('Class#test[param]')
... )
>>> assert_that(Report(),
... has_test_case('wrong_test_case_name')
... ) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AssertionError: ...
Expected: ...
but: property 'test_cases' was <[{...}]>
<BLANKLINE>
>>> assert_that(Report(),
... has_test_case('test',
... has_entry('id', '1')
... )
... )
>>> assert_that(Report(),
... has_test_case('Class#test[param]',
... has_entry('id', '2')
... )
... ) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
AssertionError: ...
Expected: ...
but: property 'test_cases' was <[{...}]>
<BLANKLINE>
"""
import sys
import os
import json
import fnmatch
from hamcrest import all_of, any_of
from hamcrest import has_property
from hamcrest import has_item
from hamcrest import has_entry
from hamcrest import ends_with, starts_with
from hamcrest import only_contains
from hamcrest.core.base_matcher import BaseMatcher
if sys.version_info[0] < 3:
from io import open
class AllureReport(object):
def __init__(self, result):
self.result_dir = result
self.test_cases = [json.load(item) for item in self._report_items(result, '*result.json')]
self.test_containers = [json.load(item) for item in self._report_items(result, '*container.json')]
self.attachments = [item.read() for item in self._report_items(result, '*attachment.*')]
@staticmethod
def _report_items(report_dir, glob):
for _file in os.listdir(report_dir):
if fnmatch.fnmatch(_file, glob):
with open(os.path.join(report_dir, _file), encoding="utf-8") as report_file:
yield report_file
def has_test_case(name, *matchers):
return has_property('test_cases',
has_item(
all_of(
any_of(
has_entry('fullName', ends_with(name)),
has_entry('name', starts_with(name))
),
*matchers
)
)
)
class HasOnlyTetcases(BaseMatcher):
def __init__(self, *matchers):
self.matchers = matchers
def _matches(self, item):
return has_property('test_cases',
only_contains(any_of(*self.matchers))
).matches(item)
def describe_to(self, description):
pass
def has_only_testcases(*matchers):
return HasOnlyTetcases(*matchers)
class ContainsExactly(BaseMatcher):
def __init__(self, num, matcher):
self.matcher = matcher
self.count = 0
self.num = num
def _matches(self, item):
self.count = 0
for subitem in item:
if self.matcher.matches(subitem):
self.count += 1
if self.count == self.num:
return True
else:
return False
def describe_to(self, description):
description.append_text('exactly {} item(s) matching '.format(self.num)).append_text(self.matcher)
def has_only_n_test_cases(name, num, *matchers):
return has_property('test_cases',
ContainsExactly(num,
all_of(
any_of(
has_entry('fullName', ends_with(name)),
has_entry('name', ends_with(name))
),
*matchers
)
)
)