Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
configurable include_all (#60)
Browse files Browse the repository at this point in the history
* add include_all toggle

* bugfix: failed to read bool from cfg file; cfg file value being ignored

* fix indent

* update readme
  • Loading branch information
reverbc authored and allankp committed Oct 26, 2018
1 parent b781852 commit 291acd1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ py.test --testrail --tr-config=<settings file>.cfg
--tr-testrun-suite-id=TR_TESTRUN_SUITE_ID
ID of the test suite containing the test cases (config
file: suite_id in TESTRUN section)
--tr-testrun-suite-include-all
Include all test cases in specified test suite when
creating test run (config file: include_all in TESTRUN
section)
--tr-testrun-name=TR_TESTRUN_NAME
Name given to testrun, that appears in TestRail
(config file: name in TESTRUN section)
Expand All @@ -111,4 +115,4 @@ py.test --testrail --tr-config=<settings file>.cfg
Do not publish results of "blocked" testcases in
TestRail
--tr-skip-missing Skip test cases that are not present in testrun
```
```
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ All available options
--tr-testrun-suite-id=TR_TESTRUN_SUITE_ID
ID of the test suite containing the test cases (config
file: suite_id in TESTRUN section)
--tr-testrun-suite-include-all
Include all test cases in specified test suite when
creating test run (config file: include_all in TESTRUN
section)
--tr-testrun-name=TR_TESTRUN_NAME
Name given to testrun, that appears in TestRail
(config file: name in TESTRUN section)
Expand Down
26 changes: 22 additions & 4 deletions pytest_testrail/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def pytest_addoption(parser):
'--tr-testrun-suite-id',
action='store',
help='ID of the test suite containing the test cases (config file: suite_id in TESTRUN section)')
group.addoption(
'--tr-testrun-suite-include-all',
action='store_true',
default=None,
help='Include all test cases in specified test suite when creating test run (config file: include_all in TESTRUN section)')
group.addoption(
'--tr-testrun-name',
action='store',
Expand All @@ -73,6 +78,7 @@ def pytest_addoption(parser):
group.addoption(
'--tr-no-ssl-cert-check',
action='store_false',
default=None,
help='Do not check for valid SSL certificate on TestRail host')
group.addoption(
'--tr-close-on-complete',
Expand Down Expand Up @@ -105,7 +111,8 @@ def pytest_configure(config):
assign_user_id=config_manager.getoption('tr-testrun-assignedto-id', 'assignedto_id', 'TESTRUN'),
project_id=config_manager.getoption('tr-testrun-project-id', 'project_id', 'TESTRUN'),
suite_id=config_manager.getoption('tr-testrun-suite-id', 'suite_id', 'TESTRUN'),
cert_check=config_manager.getoption('tr-no-ssl-cert-check', 'no_ssl_cert_check', 'API', default=True),
include_all=config_manager.getoption('tr-testrun-suite-include-all', 'include_all', 'TESTRUN', is_bool=True, default=False),
cert_check=config_manager.getoption('tr-no-ssl-cert-check', 'no_ssl_cert_check', 'API', is_bool=True, default=True),
tr_name=config_manager.getoption('tr-testrun-name', 'name', 'TESTRUN'),
run_id=config.getoption('--tr-run-id'),
plan_id=config.getoption('--tr-plan-id'),
Expand Down Expand Up @@ -137,10 +144,21 @@ def __init__(self, cfg_file_path, config):

self.config = config

def getoption(self, flag, cfg_name, section=None, default=None):
def getoption(self, flag, cfg_name, section=None, is_bool=False, default=None):
# priority: cli > config file > default

# 1. return cli option (if set)
value = self.config.getoption('--{}'.format(flag))
if value is not None:
return value

# 2. return default if not config file path is specified
if section is None or self.cfg_file is None:
return None
return self.cfg_file.get(section, cfg_name)
return default

if self.cfg_file.has_option(section, cfg_name):
# 3. return config file value
return self.cfg_file.getboolean(section, cfg_name) if is_bool else self.cfg_file.get(section, cfg_name)
else:
# 4. if entry not found in config file
return default
14 changes: 10 additions & 4 deletions pytest_testrail/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ def get_testrail_keys(items):


class PyTestRailPlugin(object):
def __init__(self, client, assign_user_id, project_id, suite_id, cert_check, tr_name, run_id=0, plan_id=0,
version='', close_on_complete=False, publish_blocked=True, skip_missing=False):
def __init__(self, client, assign_user_id, project_id, suite_id, include_all, cert_check, tr_name, run_id=0,
plan_id=0, version='', close_on_complete=False, publish_blocked=True, skip_missing=False):
self.assign_user_id = assign_user_id
self.cert_check = cert_check
self.client = client
self.project_id = project_id
self.results = []
self.suite_id = suite_id
self.include_all = include_all
self.testrun_name = tr_name
self.testrun_id = run_id
self.testplan_id = plan_id
Expand Down Expand Up @@ -171,6 +172,7 @@ def pytest_collection_modifyitems(self, session, config, items):
self.assign_user_id,
self.project_id,
self.suite_id,
self.include_all,
self.testrun_name,
tr_keys
)
Expand Down Expand Up @@ -261,6 +263,10 @@ def add_results(self, testrun_id):
', '.join(str(elt) for elt in blocked_tests_list)))
self.results = [result for result in self.results if result.get('case_id') not in blocked_tests_list]

# prompt enabling include all test cases from test suite when creating test run
if self.include_all:
print('[{}] Option "Include all testcases from test suite for test run" activated'.format(TESTRAIL_PREFIX))

# Publish results
for result in self.results:
data = {'status_id': result['status_id']}
Expand Down Expand Up @@ -288,7 +294,7 @@ def add_results(self, testrun_id):
error))

def create_test_run(
self, assign_user_id, project_id, suite_id, testrun_name, tr_keys):
self, assign_user_id, project_id, suite_id, include_all, testrun_name, tr_keys):
"""
Create testrun with ids collected from markers.
Expand All @@ -298,7 +304,7 @@ def create_test_run(
'suite_id': suite_id,
'name': testrun_name,
'assignedto_id': assign_user_id,
'include_all': False,
'include_all': include_all,
'case_ids': tr_keys,
}

Expand Down
15 changes: 8 additions & 7 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def api_client():

@pytest.fixture
def tr_plugin(api_client):
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, True, TR_NAME, version='1.0.0.0')
return PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME, version='1.0.0.0')


@pytest.fixture
Expand Down Expand Up @@ -211,18 +211,19 @@ def test_pytest_sessionfinish_testplan(api_client, tr_plugin):
expected_data_5678, cert_check=True)


def test_create_test_run(api_client, tr_plugin):
@pytest.mark.parametrize('include_all', [True, False])
def test_create_test_run(api_client, tr_plugin, include_all):
expected_tr_keys = [3453, 234234, 12]
expect_name = 'testrun_name'

tr_plugin.create_test_run(ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, expect_name, expected_tr_keys)
tr_plugin.create_test_run(ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, include_all, expect_name, expected_tr_keys)

expected_uri = plugin.ADD_TESTRUN_URL.format(PROJECT_ID)
expected_data = {
'suite_id': SUITE_ID,
'name': expect_name,
'assignedto_id': ASSIGN_USER_ID,
'include_all': False,
'include_all': include_all,
'case_ids': expected_tr_keys
}
check_cert = True
Expand Down Expand Up @@ -296,7 +297,7 @@ def test_close_test_plan(api_client, tr_plugin):

def test_dont_publish_blocked(api_client):
""" Case: one test is blocked"""
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, True, TR_NAME,
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID, SUITE_ID, False, True, TR_NAME,
version='1.0.0.0',
publish_blocked=False
)
Expand Down Expand Up @@ -324,7 +325,7 @@ def test_dont_publish_blocked(api_client):

def test_skip_missing_only_one_test(api_client, pytest_test_items):
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID,
SUITE_ID, True, TR_NAME,
SUITE_ID, False, True, TR_NAME,
run_id=10,
version='1.0.0.0',
publish_blocked=False,
Expand All @@ -343,7 +344,7 @@ def test_skip_missing_only_one_test(api_client, pytest_test_items):

def test_skip_missing_correlation_tests(api_client, pytest_test_items):
my_plugin = PyTestRailPlugin(api_client, ASSIGN_USER_ID, PROJECT_ID,
SUITE_ID, True, TR_NAME,
SUITE_ID, False, True, TR_NAME,
run_id=10,
version='1.0.0.0',
publish_blocked=False,
Expand Down

0 comments on commit 291acd1

Please sign in to comment.