Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python scripting: Add a test plan generator script #32718

Merged
merged 12 commits into from
Apr 26, 2024
62 changes: 62 additions & 0 deletions src/python_testing/test_plan_table_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env -S python3 -B
#
# Copyright (c) 2024 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import importlib
import os
from pathlib import Path

import click
from matter_testing_support import MatterTestConfig, generate_mobly_test_config


def indent_multiline(multiline: str, num_spaces: int) -> str:
''' Indents subsequent lines of a multiline string by num_spaces spaces'''
s = multiline.split('\n')
s = [(num_spaces * ' ' + line.lstrip()).rstrip() for line in s]
return '\n'.join(s).lstrip()


@click.command()
@click.argument('filename', type=click.Path(exists=True))
@click.argument('classname', type=str)
@click.argument('test', type=str)
def main(filename, classname, test):
cecille marked this conversation as resolved.
Show resolved Hide resolved
module = importlib.import_module(Path(os.path.basename(filename)).stem)
test_class = getattr(module, classname)
config = generate_mobly_test_config(MatterTestConfig())
test_instance = test_class(config)
steps = test_instance.get_test_steps(test)
cecille marked this conversation as resolved.
Show resolved Hide resolved
indent = 6
header_num = f'{"**#**":<{indent}}'
header_num_step = f'|{header_num} |*TestStep* '
s = ('[cols="5%,45%,45%"]\n'
'|===\n'
f'{header_num_step}|*Expected Outcome*\n')
jamesharrow marked this conversation as resolved.
Show resolved Hide resolved
for step in steps:
step_num = f'|{step.test_plan_number:<{indent}}a|'
s += f'{step_num}{indent_multiline(step.description, len(step_num))}\n'

padding = (len(header_num_step) - 1) * ' '
# add 2 to indent for a| at start
s += f'{padding}a|{indent_multiline(step.expectation, len(padding)+2)}\n\n'
s += '|===\n'

print(s)


if __name__ == "__main__":
main()
Loading