Skip to content

Commit

Permalink
Allow custom result fields - passed up on results
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKiazyk authored and d-rede committed Feb 9, 2023
1 parent 3883f1e commit e64215a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Options:
--case-fields List of case fields and values for new test cases
creation. Usage: --case-fields type_id:1 --case-fields
priority_id:3
--result-fields List of result fields and values for test results
creation. Usage: --result-fields custom_environment:2
--help Show this message and exit.
```

Expand Down Expand Up @@ -253,6 +255,7 @@ Possible fields:<br>
| run_id | specifies the Run ID for the Test Run to be created under |
| close_run | specifies whether to close the run after adding all the results (false by default) |
| case_fields | dictionary with case fields to be filled on case creation as a key value pair |
| result_fields | dictionary with result fields to be filled on results creation as a key value pair |
| run_description | text to be added to the run description (for example, if you want to add the link to your CI job) |

Below is an example of a sample configuration file for the TestRail CLI.
Expand Down
2 changes: 1 addition & 1 deletion trcli/api/api_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
self.environment = environment
self.client = api_client
self.suffix = api_client.VERSION
self.data_provider = ApiDataProvider(suites_data, environment.case_fields, environment.run_description)
self.data_provider = ApiDataProvider(suites_data, environment.case_fields, environment.run_description, environment.result_fields)
self.suites_data_from_provider = self.data_provider.suites_input
self.response_verifier = ApiResponseVerify(verify)

Expand Down
19 changes: 19 additions & 0 deletions trcli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self):
self.run_description = None
self.case_matcher = None
self._case_fields = None
self._result_fields = None

@property
def case_fields(self):
Expand All @@ -70,6 +71,24 @@ def case_fields(self, case_fields: Union[List[str], dict]):
exit(1)
self._case_fields = fields_dictionary

@property
def result_fields(self):
return self._result_fields

@result_fields.setter
def result_fields(self, result_fields: Union[List[str], dict]):
fields_dictionary = {}
if isinstance(result_fields, list) or isinstance(result_fields, tuple):
for result_field in result_fields:
field, value = result_field.split(":", maxsplit=1)
fields_dictionary[field] = value
elif isinstance(result_fields, dict):
fields_dictionary = result_fields
else:
self.elog(f"Invalid result fields type ({type(result_fields)}), supported types are tuple/list/dictionary.")
exit(1)
self._result_fields = fields_dictionary

def log(self, msg: str, new_line=True, *args):
"""Logs a message to stdout only is silent mode is disabled."""
if not self.silent:
Expand Down
8 changes: 8 additions & 0 deletions trcli/commands/cmd_parse_junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def print_config(env: Environment):
help="List of case fields and values for new test cases creation. "
"Usage: --case-fields type_id:1 --case-fields priority_id:3",
)
@click.option(
"--result-fields",
multiple=True,
metavar="",
default=[],
help="List of result fields and values for test results creation. "
"Usage: --result-fields custom_type_id:1 --result-fields custom_priority_id:3",
)
@click.pass_context
@pass_environment
def cli(environment: Environment, context: click.Context, *args, **kwargs):
Expand Down
27 changes: 19 additions & 8 deletions trcli/data_providers/api_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class ApiDataProvider:
ApiPostProvider is a place where you can convert TestRailSuite dataclass to bodies for API requests
"""

def __init__(self, suites_input: TestRailSuite, case_fields: dict = None, run_description: str = None):
def __init__(self, suites_input: TestRailSuite, case_fields: dict = None, run_description: str = None, result_fields: dict = None):
self.suites_input = suites_input
self.case_fields = case_fields
self.run_description = run_description
self.result_fields = result_fields

def add_suites_data(self):
"""Return list of bodies for adding suites"""
Expand Down Expand Up @@ -68,7 +69,7 @@ def add_run(self, run_name: str, case_ids=None):
"suite_id": self.suites_input.suite_id,
"description": '\n'.join(properties),
"include_all": False,
"case_ids": case_ids,
"case_ids": case_ids
}

def add_result_for_case(self, case_id):
Expand All @@ -81,6 +82,10 @@ def add_result_for_case(self, case_id):
if len(cases) == 1:
case_id_from_file = cases[0].case_id
result = to_dict(cases[0].result)
if self.result_fields:
for field, val in self.result_fields.items():
result[field] = val

if case_id_from_file is None or case_id_from_file == case_id:
result["case_id"] = case_id
results = [result]
Expand All @@ -94,13 +99,19 @@ def add_results_for_cases(self, bulk_size):
"""Return bodies for adding results for cases. Returns bodies for results that already have case ID."""
testcases = [sections.testcases for sections in self.suites_input.testsections]

bodies = []

for sublist in testcases:
for case in sublist:
if case.case_id is not None:
body = to_dict(case.result)
if self.result_fields:
for field, val in self.result_fields.items():
body[field] = val
bodies.append(body)

result_bulks = ApiDataProvider.divide_list_into_bulks(
[
to_dict(case.result)
for sublist in testcases
for case in sublist
if case.case_id is not None
],
bodies,
bulk_size=bulk_size,
)
return [{"results": result_bulk} for result_bulk in result_bulks]
Expand Down

0 comments on commit e64215a

Please sign in to comment.