Skip to content

Commit 291acd1

Browse files
reverbcallankp
authored andcommitted
configurable include_all (#60)
* add include_all toggle * bugfix: failed to read bool from cfg file; cfg file value being ignored * fix indent * update readme
1 parent b781852 commit 291acd1

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ py.test --testrail --tr-config=<settings file>.cfg
9191
--tr-testrun-suite-id=TR_TESTRUN_SUITE_ID
9292
ID of the test suite containing the test cases (config
9393
file: suite_id in TESTRUN section)
94+
--tr-testrun-suite-include-all
95+
Include all test cases in specified test suite when
96+
creating test run (config file: include_all in TESTRUN
97+
section)
9498
--tr-testrun-name=TR_TESTRUN_NAME
9599
Name given to testrun, that appears in TestRail
96100
(config file: name in TESTRUN section)
@@ -111,4 +115,4 @@ py.test --testrail --tr-config=<settings file>.cfg
111115
Do not publish results of "blocked" testcases in
112116
TestRail
113117
--tr-skip-missing Skip test cases that are not present in testrun
114-
```
118+
```

README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ All available options
9797
--tr-testrun-suite-id=TR_TESTRUN_SUITE_ID
9898
ID of the test suite containing the test cases (config
9999
file: suite_id in TESTRUN section)
100+
--tr-testrun-suite-include-all
101+
Include all test cases in specified test suite when
102+
creating test run (config file: include_all in TESTRUN
103+
section)
100104
--tr-testrun-name=TR_TESTRUN_NAME
101105
Name given to testrun, that appears in TestRail
102106
(config file: name in TESTRUN section)

pytest_testrail/conftest.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def pytest_addoption(parser):
4747
'--tr-testrun-suite-id',
4848
action='store',
4949
help='ID of the test suite containing the test cases (config file: suite_id in TESTRUN section)')
50+
group.addoption(
51+
'--tr-testrun-suite-include-all',
52+
action='store_true',
53+
default=None,
54+
help='Include all test cases in specified test suite when creating test run (config file: include_all in TESTRUN section)')
5055
group.addoption(
5156
'--tr-testrun-name',
5257
action='store',
@@ -73,6 +78,7 @@ def pytest_addoption(parser):
7378
group.addoption(
7479
'--tr-no-ssl-cert-check',
7580
action='store_false',
81+
default=None,
7682
help='Do not check for valid SSL certificate on TestRail host')
7783
group.addoption(
7884
'--tr-close-on-complete',
@@ -105,7 +111,8 @@ def pytest_configure(config):
105111
assign_user_id=config_manager.getoption('tr-testrun-assignedto-id', 'assignedto_id', 'TESTRUN'),
106112
project_id=config_manager.getoption('tr-testrun-project-id', 'project_id', 'TESTRUN'),
107113
suite_id=config_manager.getoption('tr-testrun-suite-id', 'suite_id', 'TESTRUN'),
108-
cert_check=config_manager.getoption('tr-no-ssl-cert-check', 'no_ssl_cert_check', 'API', default=True),
114+
include_all=config_manager.getoption('tr-testrun-suite-include-all', 'include_all', 'TESTRUN', is_bool=True, default=False),
115+
cert_check=config_manager.getoption('tr-no-ssl-cert-check', 'no_ssl_cert_check', 'API', is_bool=True, default=True),
109116
tr_name=config_manager.getoption('tr-testrun-name', 'name', 'TESTRUN'),
110117
run_id=config.getoption('--tr-run-id'),
111118
plan_id=config.getoption('--tr-plan-id'),
@@ -137,10 +144,21 @@ def __init__(self, cfg_file_path, config):
137144

138145
self.config = config
139146

140-
def getoption(self, flag, cfg_name, section=None, default=None):
147+
def getoption(self, flag, cfg_name, section=None, is_bool=False, default=None):
148+
# priority: cli > config file > default
149+
150+
# 1. return cli option (if set)
141151
value = self.config.getoption('--{}'.format(flag))
142152
if value is not None:
143153
return value
154+
155+
# 2. return default if not config file path is specified
144156
if section is None or self.cfg_file is None:
145-
return None
146-
return self.cfg_file.get(section, cfg_name)
157+
return default
158+
159+
if self.cfg_file.has_option(section, cfg_name):
160+
# 3. return config file value
161+
return self.cfg_file.getboolean(section, cfg_name) if is_bool else self.cfg_file.get(section, cfg_name)
162+
else:
163+
# 4. if entry not found in config file
164+
return default

pytest_testrail/plugin.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ def get_testrail_keys(items):
117117

118118

119119
class PyTestRailPlugin(object):
120-
def __init__(self, client, assign_user_id, project_id, suite_id, cert_check, tr_name, run_id=0, plan_id=0,
121-
version='', close_on_complete=False, publish_blocked=True, skip_missing=False):
120+
def __init__(self, client, assign_user_id, project_id, suite_id, include_all, cert_check, tr_name, run_id=0,
121+
plan_id=0, version='', close_on_complete=False, publish_blocked=True, skip_missing=False):
122122
self.assign_user_id = assign_user_id
123123
self.cert_check = cert_check
124124
self.client = client
125125
self.project_id = project_id
126126
self.results = []
127127
self.suite_id = suite_id
128+
self.include_all = include_all
128129
self.testrun_name = tr_name
129130
self.testrun_id = run_id
130131
self.testplan_id = plan_id
@@ -171,6 +172,7 @@ def pytest_collection_modifyitems(self, session, config, items):
171172
self.assign_user_id,
172173
self.project_id,
173174
self.suite_id,
175+
self.include_all,
174176
self.testrun_name,
175177
tr_keys
176178
)
@@ -261,6 +263,10 @@ def add_results(self, testrun_id):
261263
', '.join(str(elt) for elt in blocked_tests_list)))
262264
self.results = [result for result in self.results if result.get('case_id') not in blocked_tests_list]
263265

266+
# prompt enabling include all test cases from test suite when creating test run
267+
if self.include_all:
268+
print('[{}] Option "Include all testcases from test suite for test run" activated'.format(TESTRAIL_PREFIX))
269+
264270
# Publish results
265271
for result in self.results:
266272
data = {'status_id': result['status_id']}
@@ -288,7 +294,7 @@ def add_results(self, testrun_id):
288294
error))
289295

290296
def create_test_run(
291-
self, assign_user_id, project_id, suite_id, testrun_name, tr_keys):
297+
self, assign_user_id, project_id, suite_id, include_all, testrun_name, tr_keys):
292298
"""
293299
Create testrun with ids collected from markers.
294300
@@ -298,7 +304,7 @@ def create_test_run(
298304
'suite_id': suite_id,
299305
'name': testrun_name,
300306
'assignedto_id': assign_user_id,
301-
'include_all': False,
307+
'include_all': include_all,
302308
'case_ids': tr_keys,
303309
}
304310

tests/test_plugin.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def api_client():
6363

6464
@pytest.fixture
6565
def tr_plugin(api_client):
66-
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, True, TR_NAME, version='1.0.0.0')
66+
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME, version='1.0.0.0')
6767

6868

6969
@pytest.fixture
@@ -211,18 +211,19 @@ def test_pytest_sessionfinish_testplan(api_client, tr_plugin):
211211
expected_data_5678, cert_check=True)
212212

213213

214-
def test_create_test_run(api_client, tr_plugin):
214+
@pytest.mark.parametrize('include_all', [True, False])
215+
def test_create_test_run(api_client, tr_plugin, include_all):
215216
expected_tr_keys = [3453, 234234, 12]
216217
expect_name = 'testrun_name'
217218

218-
tr_plugin.create_test_run(ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, expect_name, expected_tr_keys)
219+
tr_plugin.create_test_run(ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, include_all, expect_name, expected_tr_keys)
219220

220221
expected_uri = plugin.ADD_TESTRUN_URL.format(PROJECT_ID)
221222
expected_data = {
222223
'suite_id': SUITE_ID,
223224
'name': expect_name,
224225
'assignedto_id': ASSIGN_USER_ID,
225-
'include_all': False,
226+
'include_all': include_all,
226227
'case_ids': expected_tr_keys
227228
}
228229
check_cert = True
@@ -296,7 +297,7 @@ def test_close_test_plan(api_client, tr_plugin):
296297

297298
def test_dont_publish_blocked(api_client):
298299
""" Case: one test is blocked"""
299-
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, True, TR_NAME,
300+
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME,
300301
version='1.0.0.0',
301302
publish_blocked=False
302303
)
@@ -324,7 +325,7 @@ def test_dont_publish_blocked(api_client):
324325

325326
def test_skip_missing_only_one_test(api_client, pytest_test_items):
326327
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID,
327-
SUITE_ID, True, TR_NAME,
328+
SUITE_ID, False, True, TR_NAME,
328329
run_id=10,
329330
version='1.0.0.0',
330331
publish_blocked=False,
@@ -343,7 +344,7 @@ def test_skip_missing_only_one_test(api_client, pytest_test_items):
343344

344345
def test_skip_missing_correlation_tests(api_client, pytest_test_items):
345346
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID,
346-
SUITE_ID, True, TR_NAME,
347+
SUITE_ID, False, True, TR_NAME,
347348
run_id=10,
348349
version='1.0.0.0',
349350
publish_blocked=False,

0 commit comments

Comments
 (0)