Skip to content

Commit 9c8474b

Browse files
author
Allan Kilpatrick
authored
Fix/flake8 fixes (allankp#139)
* add setup cfg for flake8 * flake8 fixes
1 parent 4ec85db commit 9c8474b

6 files changed

Lines changed: 124 additions & 45 deletions

File tree

pytest_testrail/conftest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def pytest_addoption(parser):
5454
'--tr-testrun-suite-include-all',
5555
action='store_true',
5656
default=None,
57-
help='Include all test cases in specified test suite when creating test run (config file: include_all in TESTRUN section)')
57+
help='Include all test cases in specified test suite when creating test run \
58+
(config file: include_all in TESTRUN section)')
5859
group.addoption(
5960
'--tr-testrun-name',
6061
action='store',
@@ -70,12 +71,14 @@ def pytest_addoption(parser):
7071
action='store',
7172
default=0,
7273
required=False,
73-
help='Identifier of testrun, that appears in TestRail. If provided, option "--tr-testrun-name" will be ignored')
74+
help='Identifier of testrun, that appears in TestRail. If provided, \
75+
option "--tr-testrun-name" will be ignored')
7476
group.addoption(
7577
'--tr-plan-id',
7678
action='store',
7779
required=False,
78-
help='Identifier of testplan, that appears in TestRail (config file: plan_id in TESTRUN section). If provided, option "--tr-testrun-name" will be ignored')
80+
help='Identifier of testplan, that appears in TestRail (config file: plan_id in TESTRUN section).\
81+
If provided, option "--tr-testrun-name" will be ignored')
7982
group.addoption(
8083
'--tr-version',
8184
action='store',
@@ -115,7 +118,8 @@ def pytest_addoption(parser):
115118
action='store',
116119
default=None,
117120
required=False,
118-
help='Custom comment, to be appended to default comment for test case (config file: custom_comment in TESTCASE section)'
121+
help='Custom comment, to be appended to default comment for test case \
122+
(config file: custom_comment in TESTCASE section)'
119123
)
120124

121125

@@ -157,8 +161,8 @@ def pytest_configure(config):
157161
class ConfigManager(object):
158162
def __init__(self, cfg_file_path, config):
159163
'''
160-
Handles retrieving configuration values. Config options set in flags are given preferance over options set in the
161-
config file.
164+
Handles retrieving configuration values.
165+
Config options set in flags are given preferance over options set in the config file.
162166
163167
:param cfg_file_path: Path to the config file containing information about the TestRail server.
164168
:type cfg_file_path: str or None

pytest_testrail/plugin.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def testrail(*ids):
8181
:return pytest.mark:
8282
"""
8383
deprecation_msg = ('pytest_testrail: the @testrail decorator is deprecated and will be removed. Please use the '
84-
'@pytestrail.case decorator instead.')
84+
'@pytestrail.case decorator instead.')
8585
warnings.warn(deprecation_msg, DeprecatedTestDecorator)
8686
return pytestrail.case(*ids)
8787

@@ -119,7 +119,7 @@ def clean_test_defects(defect_ids):
119119
:param list defect_ids: list of defect_ids.
120120
:return list ints: contains list of defect_ids as ints.
121121
"""
122-
return [(re.search('(?P<defect_id>.*)',defect_id).groupdict().get('defect_id')) for defect_id in defect_ids]
122+
return [(re.search('(?P<defect_id>.*)', defect_id).groupdict().get('defect_id')) for defect_id in defect_ids]
123123

124124

125125
def get_testrail_keys(items):
@@ -139,9 +139,9 @@ def get_testrail_keys(items):
139139

140140

141141
class PyTestRailPlugin(object):
142-
def __init__(self, client, assign_user_id, project_id, suite_id, include_all, cert_check, tr_name, tr_description='', run_id=0,
143-
plan_id=0, version='', close_on_complete=False, publish_blocked=True, skip_missing=False,
144-
milestone_id=None, custom_comment=None):
142+
def __init__(self, client, assign_user_id, project_id, suite_id, include_all, cert_check, tr_name,
143+
tr_description='', run_id=0, plan_id=0, version='', close_on_complete=False,
144+
publish_blocked=True, skip_missing=False, milestone_id=None, custom_comment=None):
145145
self.assign_user_id = assign_user_id
146146
self.cert_check = cert_check
147147
self.client = client
@@ -221,14 +221,14 @@ def pytest_runtest_makereport(self, item, call):
221221
if item.get_closest_marker(TESTRAIL_PREFIX):
222222
testcaseids = item.get_closest_marker(TESTRAIL_PREFIX).kwargs.get('ids')
223223
if rep.when == 'call' and testcaseids:
224-
if defectids != None:
224+
if defectids:
225225
self.add_result(
226226
clean_test_ids(testcaseids),
227227
get_test_outcome(outcome.get_result().outcome),
228228
comment=comment,
229229
duration=rep.duration,
230230
defects=str(clean_test_defects(defectids)).replace('[', '').replace(']', '').replace("'", ''),
231-
test_parametrize = test_parametrize
231+
test_parametrize=test_parametrize
232232
)
233233
else:
234234
self.add_result(
@@ -277,13 +277,13 @@ def add_result(self, test_ids, status, comment='', defects=None, duration=0, tes
277277
"""
278278
for test_id in test_ids:
279279
data = {
280-
'case_id': test_id,
281-
'status_id': status,
282-
'comment': comment,
283-
'duration': duration,
284-
'defects': defects,
285-
'test_parametrize': test_parametrize
286-
}
280+
'case_id': test_id,
281+
'status_id': status,
282+
'comment': comment,
283+
'duration': duration,
284+
'defects': defects,
285+
'test_parametrize': test_parametrize
286+
}
287287
self.results.append(data)
288288

289289
def add_results(self, testrun_id):
@@ -300,7 +300,8 @@ def add_results(self, testrun_id):
300300
converter = lambda s, c: str(bytes(s, "utf-8"), c)
301301
# Results are sorted by 'case_id' and by 'status_id' (worst result at the end)
302302

303-
# Comment sort by status_id due to issue with pytest-rerun failures, for details refer to issue https://github.com/allankp/pytest-testrail/issues/100
303+
# Comment sort by status_id due to issue with pytest-rerun failures,
304+
# for details refer to issue https://github.com/allankp/pytest-testrail/issues/100
304305
# self.results.sort(key=itemgetter('status_id'))
305306
self.results.sort(key=itemgetter('case_id'))
306307

@@ -337,12 +338,12 @@ def add_results(self, testrun_id):
337338
# Indent text to avoid string formatting by TestRail. Limit size of comment.
338339
entry['comment'] += u"# Pytest result: #\n"
339340
entry['comment'] += u'Log truncated\n...\n' if len(str(comment)) > COMMENT_SIZE_LIMIT else u''
340-
entry['comment'] += u" " + converter(str(comment), "utf-8")[-COMMENT_SIZE_LIMIT:].replace('\n', '\n ')
341+
entry['comment'] += u" " + converter(str(comment), "utf-8")[-COMMENT_SIZE_LIMIT:].replace('\n', '\n ') # noqa
341342
else:
342343
# Indent text to avoid string formatting by TestRail. Limit size of comment.
343344
entry['comment'] += u"# Pytest result: #\n"
344345
entry['comment'] += u'Log truncated\n...\n' if len(str(comment)) > COMMENT_SIZE_LIMIT else u''
345-
entry['comment'] += u" " + converter(str(comment), "utf-8")[-COMMENT_SIZE_LIMIT:].replace('\n', '\n ')
346+
entry['comment'] += u" " + converter(str(comment), "utf-8")[-COMMENT_SIZE_LIMIT:].replace('\n', '\n ') # noqa
346347
elif comment == '':
347348
entry['comment'] = self.custom_comment
348349
duration = result.get('duration')
@@ -360,8 +361,8 @@ def add_results(self, testrun_id):
360361
if error:
361362
print('[{}] Info: Testcases not published for following reason: "{}"'.format(TESTRAIL_PREFIX, error))
362363

363-
def create_test_run(
364-
self, assign_user_id, project_id, suite_id, include_all, testrun_name, tr_keys, milestone_id, description=''):
364+
def create_test_run(self, assign_user_id, project_id, suite_id, include_all,
365+
testrun_name, tr_keys, milestone_id, description=''):
365366
"""
366367
Create testrun with ids collected from markers.
367368

pytest_testrail/testrail_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def send_get(self, uri, **kwargs):
8383
pause = int(r.headers.get('Retry-After', 60))
8484
print("Too many requests: pause for {}s".format(pause))
8585
time.sleep(pause)
86-
return self.send_get(uri,**kwargs)
86+
return self.send_get(uri, **kwargs)
8787
else:
8888
return r.json()
8989

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[flake8]
2+
ignore = E731
23
max-line-length = 119
4+
exclude = .git,__pycache__,conftest.py,build,dist

tests/livetest/livetest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,28 @@
44

55
from pytest_testrail.plugin import testrail, pytestrail
66

7+
78
@testrail('C344', 'C366')
89
def test_func1():
910
time.sleep(0.5)
1011

12+
1113
@testrail('C345')
1214
def test_func2():
1315
time.sleep(1.6)
1416
pytest.fail()
1517

18+
1619
@testrail('C99999')
1720
def test_func3():
1821
time.sleep(0.5)
1922

23+
2024
@pytestrail.case('C1788')
2125
def test_func4():
2226
pytest.skip()
2327

28+
2429
@pytestrail.case('C1789')
2530
def test_func5():
2631
time.sleep(0.5)

tests/test_plugin.py

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_other_func():
5757

5858
CUSTOM_COMMENT = "This is custom comment"
5959

60+
6061
@pytest.fixture
6162
def api_client():
6263
spec = create_autospec(APIClient)
@@ -66,8 +67,8 @@ def api_client():
6667

6768
@pytest.fixture
6869
def tr_plugin(api_client):
69-
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME, DESCRIPTION, version='1.0.0.0',
70-
milestone_id=MILESTONE_ID, custom_comment=CUSTOM_COMMENT)
70+
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME, DESCRIPTION,
71+
version='1.0.0.0', milestone_id=MILESTONE_ID, custom_comment=CUSTOM_COMMENT)
7172

7273

7374
@pytest.fixture
@@ -174,40 +175,87 @@ def get_result(self):
174175

175176
def test_pytest_sessionfinish(api_client, tr_plugin):
176177
tr_plugin.results = [
177-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["failed"], 'duration': 2.6, 'defects':'PF-516' },
178-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'comment': "An error", 'duration': 0.1, 'defects':None },
179-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'duration': 2.6, 'defects': ['PF-517', 'PF-113']}
178+
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["failed"], 'duration': 2.6, 'defects':'PF-516'},
179+
{
180+
'case_id': 5678,
181+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
182+
'comment': "An error",
183+
'duration': 0.1,
184+
'defects':None
185+
},
186+
{
187+
'case_id': 1234,
188+
'status_id': TESTRAIL_TEST_STATUS["passed"],
189+
'duration': 2.6,
190+
'defects': ['PF-517', 'PF-113']
191+
}
180192
]
181193
tr_plugin.testrun_id = 10
182194

183195
tr_plugin.pytest_sessionfinish(None, 0)
184196

185197
expected_data = {'results': [
186-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["failed"], 'defects':'PF-516', 'version': '1.0.0.0', 'elapsed': '3s', 'comment': CUSTOM_COMMENT},
187-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'defects':['PF-517', 'PF-113'], 'version': '1.0.0.0', 'elapsed': '3s', 'comment': CUSTOM_COMMENT},
188-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'defects':None, 'version': '1.0.0.0', 'elapsed': '1s',
189-
'comment': u'{}\n# Pytest result: #\n An error'.format(CUSTOM_COMMENT)}
198+
{
199+
'case_id': 1234,
200+
'status_id': TESTRAIL_TEST_STATUS["failed"],
201+
'defects':'PF-516',
202+
'version': '1.0.0.0',
203+
'elapsed': '3s',
204+
'comment': CUSTOM_COMMENT
205+
},
206+
{
207+
'case_id': 1234,
208+
'status_id': TESTRAIL_TEST_STATUS["passed"],
209+
'defects':['PF-517', 'PF-113'],
210+
'version': '1.0.0.0',
211+
'elapsed': '3s',
212+
'comment': CUSTOM_COMMENT
213+
},
214+
{
215+
'case_id': 5678,
216+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
217+
'defects': None,
218+
'version': '1.0.0.0',
219+
'elapsed': '1s',
220+
'comment': u'{}\n# Pytest result: #\n An error'.format(CUSTOM_COMMENT)}
190221
]}
191222

192223
api_client.send_post.assert_any_call(plugin.ADD_RESULTS_URL.format(tr_plugin.testrun_id), expected_data,
193-
cert_check=True)
224+
cert_check=True)
194225

195226

196227
def test_pytest_sessionfinish_testplan(api_client, tr_plugin):
197228
tr_plugin.results = [
198-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'comment': "An error", 'duration': 0.1, 'defects':None,},
199-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'duration': 2.6, 'defects':None,}
229+
{
230+
'case_id': 5678,
231+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
232+
'comment': "An error",
233+
'duration': 0.1,
234+
'defects':None,
235+
},
236+
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'duration': 2.6, 'defects':None}
200237
]
201238
tr_plugin.testplan_id = 100
202239
tr_plugin.testrun_id = 0
203240

204241
api_client.send_get.return_value = TESTPLAN
205242
tr_plugin.pytest_sessionfinish(None, 0)
206243
expected_data = {'results': [
207-
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'version': '1.0.0.0', 'elapsed': '3s', 'defects':None,
208-
'comment': CUSTOM_COMMENT},
209-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'version': '1.0.0.0', 'elapsed': '1s', 'defects':None,
210-
'comment': u'{}\n# Pytest result: #\n An error'.format(CUSTOM_COMMENT)}
244+
{
245+
'case_id': 1234,
246+
'status_id': TESTRAIL_TEST_STATUS["passed"],
247+
'version': '1.0.0.0',
248+
'elapsed': '3s',
249+
'defects':None,
250+
'comment': CUSTOM_COMMENT
251+
},
252+
{
253+
'case_id': 5678,
254+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
255+
'version': '1.0.0.0',
256+
'elapsed': '1s',
257+
'defects':None,
258+
'comment': u'{}\n# Pytest result: #\n An error'.format(CUSTOM_COMMENT)}
211259
]}
212260
print(api_client.send_post.call_args_list)
213261

@@ -277,7 +325,13 @@ def test_get_available_testruns(api_client, tr_plugin):
277325
def test_close_test_run(api_client, tr_plugin):
278326
tr_plugin.results = [
279327
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["failed"], 'duration': 2.6, 'defects':None},
280-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'comment': "An error", 'duration': 0.1, 'defects':None},
328+
{
329+
'case_id': 5678,
330+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
331+
'comment': "An error",
332+
'duration': 0.1,
333+
'defects':None
334+
},
281335
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'duration': 2.6, 'defects':None}
282336
]
283337
tr_plugin.testrun_id = 10
@@ -290,7 +344,13 @@ def test_close_test_run(api_client, tr_plugin):
290344

291345
def test_close_test_plan(api_client, tr_plugin):
292346
tr_plugin.results = [
293-
{'case_id': 5678, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'comment': "An error", 'duration': 0.1, 'defects':None},
347+
{
348+
'case_id': 5678,
349+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
350+
'comment': "An error",
351+
'duration': 0.1,
352+
'defects':None
353+
},
294354
{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["passed"], 'duration': 2.6, 'defects':None}
295355
]
296356
tr_plugin.testplan_id = 100
@@ -327,7 +387,14 @@ def test_dont_publish_blocked(api_client):
327387
api_client.send_get.assert_called_once_with(plugin.GET_TESTS_URL.format(my_plugin.testrun_id),
328388
cert_check=True)
329389
expected_uri = plugin.ADD_RESULTS_URL.format(my_plugin.testrun_id)
330-
expected_data = {'results': [{'case_id': 1234, 'status_id': TESTRAIL_TEST_STATUS["blocked"], 'version': '1.0.0.0'}]}
390+
expected_data = {
391+
'results': [
392+
{
393+
'case_id': 1234,
394+
'status_id': TESTRAIL_TEST_STATUS["blocked"],
395+
'version': '1.0.0.0'
396+
}
397+
]}
331398
len(api_client.send_post.call_args_list) == 1
332399
api_client.send_post.call_args_list[0] == call(expected_uri, expected_data, cert_check=True)
333400

0 commit comments

Comments
 (0)