|
1 | 1 | """ |
2 | | ->>> from hamcrest import assert_that |
3 | | -
|
4 | 2 | >>> class Report(object): |
5 | 3 | ... def __init__(self): |
6 | 4 | ... self.test_cases = [ |
|
36 | 34 |
|
37 | 35 | >>> assert_that(Report(), |
38 | 36 | ... has_test_case('wrong_test_case_name') |
39 | | -... ) # doctest: +ELLIPSIS |
| 37 | +... ) |
40 | 38 | Traceback (most recent call last): |
41 | 39 | ... |
42 | 40 | AssertionError: ... |
|
56 | 54 | ... has_test_case('Class#test[param]', |
57 | 55 | ... has_entry('id', '2') |
58 | 56 | ... ) |
59 | | -... ) # doctest: +ELLIPSIS |
| 57 | +... ) |
60 | 58 | Traceback (most recent call last): |
61 | 59 | ... |
62 | 60 | AssertionError: ... |
|
66 | 64 |
|
67 | 65 | """ |
68 | 66 |
|
69 | | -import sys |
70 | 67 | import os |
71 | 68 | import json |
72 | 69 | import fnmatch |
73 | 70 | from hamcrest import all_of, any_of |
74 | 71 | from hamcrest import has_property |
75 | 72 | from hamcrest import has_item |
76 | 73 | from hamcrest import has_entry |
77 | | -from hamcrest import ends_with, starts_with |
78 | | -from hamcrest import only_contains |
79 | | -from hamcrest.core.base_matcher import BaseMatcher |
80 | | - |
81 | | -if sys.version_info[0] < 3: |
82 | | - from io import open |
| 74 | +from hamcrest import ends_with |
83 | 75 |
|
84 | 76 |
|
85 | 77 | class AllureReport(object): |
86 | | - def __init__(self, result): |
87 | | - self.result_dir = result |
88 | | - self.test_cases = [json.load(item) for item in self._report_items(result, '*result.json')] |
89 | | - self.test_containers = [json.load(item) for item in self._report_items(result, '*container.json')] |
90 | | - self.attachments = [item.read() for item in self._report_items(result, '*attachment.*')] |
91 | | - |
92 | | - @staticmethod |
93 | | - def _report_items(report_dir, glob): |
94 | | - for _file in os.listdir(report_dir): |
| 78 | + def __init__(self, report_dir): |
| 79 | + self.report_dir = report_dir |
| 80 | + self.test_cases = [json.load(item) for item in self._report_items('*result.json')] |
| 81 | + self.test_containers = [json.load(item) for item in self._report_items('*container.json')] |
| 82 | + self.attachments = [item.read() for item in self._report_items('*attachment.*')] |
| 83 | + |
| 84 | + def _report_items(self, glob): |
| 85 | + for _file in os.listdir(self.report_dir): |
95 | 86 | if fnmatch.fnmatch(_file, glob): |
96 | | - with open(os.path.join(report_dir, _file), encoding="utf-8") as report_file: |
| 87 | + with open(os.path.join(self.report_dir, _file)) as report_file: |
97 | 88 | yield report_file |
98 | 89 |
|
99 | 90 |
|
100 | 91 | def has_test_case(name, *matchers): |
101 | 92 | return has_property('test_cases', |
102 | 93 | has_item( |
103 | | - all_of( |
104 | | - any_of( |
105 | | - has_entry('fullName', ends_with(name)), |
106 | | - has_entry('name', starts_with(name)) |
107 | | - ), |
108 | | - *matchers |
109 | | - ) |
110 | | - ) |
111 | | - ) |
112 | | - |
113 | | - |
114 | | -class HasOnlyTetcases(BaseMatcher): |
115 | | - def __init__(self, *matchers): |
116 | | - self.matchers = matchers |
117 | | - |
118 | | - def _matches(self, item): |
119 | | - return has_property('test_cases', |
120 | | - only_contains(any_of(*self.matchers)) |
121 | | - ).matches(item) |
122 | | - |
123 | | - def describe_to(self, description): |
124 | | - pass |
125 | | - |
126 | | - |
127 | | -def has_only_testcases(*matchers): |
128 | | - return HasOnlyTetcases(*matchers) |
129 | | - |
130 | | - |
131 | | -class ContainsExactly(BaseMatcher): |
132 | | - def __init__(self, num, matcher): |
133 | | - self.matcher = matcher |
134 | | - self.count = 0 |
135 | | - self.num = num |
136 | | - |
137 | | - def _matches(self, item): |
138 | | - self.count = 0 |
139 | | - for subitem in item: |
140 | | - if self.matcher.matches(subitem): |
141 | | - self.count += 1 |
142 | | - |
143 | | - if self.count == self.num: |
144 | | - return True |
145 | | - else: |
146 | | - return False |
147 | | - |
148 | | - def describe_to(self, description): |
149 | | - description.append_text('exactly {} item(s) matching '.format(self.num)).append_text(self.matcher) |
150 | | - |
151 | | - |
152 | | -def has_only_n_test_cases(name, num, *matchers): |
153 | | - return has_property('test_cases', |
154 | | - ContainsExactly(num, |
155 | | - all_of( |
156 | | - any_of( |
157 | | - has_entry('fullName', ends_with(name)), |
158 | | - has_entry('name', ends_with(name)) |
159 | | - ), |
160 | | - *matchers |
161 | | - ) |
| 94 | + all_of( |
| 95 | + any_of( |
| 96 | + has_entry('fullName', ends_with(name)), |
| 97 | + has_entry('name', ends_with(name)) |
| 98 | + ), |
| 99 | + *matchers |
162 | 100 | ) |
| 101 | + ) |
163 | 102 | ) |
0 commit comments