diff --git a/README.md b/README.md index 73ac4d7..ce45093 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,10 @@ py.test --testrail --tr-config=.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) @@ -111,4 +115,4 @@ py.test --testrail --tr-config=.cfg Do not publish results of "blocked" testcases in TestRail --tr-skip-missing Skip test cases that are not present in testrun -``` \ No newline at end of file +``` diff --git a/README.rst b/README.rst index b4ad438..e81caa8 100644 --- a/README.rst +++ b/README.rst @@ -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) diff --git a/pytest_testrail/conftest.py b/pytest_testrail/conftest.py index aed2526..1d44805 100644 --- a/pytest_testrail/conftest.py +++ b/pytest_testrail/conftest.py @@ -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', @@ -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', @@ -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'), @@ -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 diff --git a/pytest_testrail/plugin.py b/pytest_testrail/plugin.py index 5e9a5d7..77d4831 100644 --- a/pytest_testrail/plugin.py +++ b/pytest_testrail/plugin.py @@ -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 @@ -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 ) @@ -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']} @@ -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. @@ -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, } diff --git a/tests/test_plugin.py b/tests/test_plugin.py index eabb976..c1ad85f 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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 @@ -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 @@ -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 ) @@ -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, @@ -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,