Skip to content
This repository has been archived by the owner on Jan 27, 2020. It is now read-only.

Commit

Permalink
WIP parametrized example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete Baughman committed Apr 5, 2019
1 parent 21f8a07 commit 83d860b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions apex_launchtest/apex_launchtest/apex_launchtest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def apex_launchtest_main():
try:
runner.validate()
except Exception as e:
raise
parser.error(e)

if args.show_args:
Expand Down
12 changes: 7 additions & 5 deletions apex_launchtest/apex_launchtest/apex_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,17 @@ def run(self):
active, and another set for tests that ran after processes were shutdown
"""
test_ld, test_context = self._test_run.normalized_test_description(
lambda: self._processes_launched.set()
ready_fn=lambda: self._processes_launched.set()
)

# Data that needs to be bound to the tests:
proc_info = ActiveProcInfoHandler()
proc_output = ActiveIoHandler()
test_context = test_context
full_context = dict(test_context, **self._test_run.param_args)
# TODO pete: this can be simplified as a call to the dict ctor:
parsed_launch_arguments = parse_launch_arguments(self._launch_file_arguments)
test_args = {}

for k, v in parsed_launch_arguments:
test_args[k] = v

Expand All @@ -85,7 +86,7 @@ def run(self):
'test_args': test_args,
},
injected_args=dict(
test_context,
full_context,
# Add a few more things to the args dictionary:
**{
'proc_info': proc_info,
Expand All @@ -102,7 +103,7 @@ def run(self):
'test_args': test_args,
},
injected_args=dict(
test_context,
full_context,
# Add a few more things to the args dictionary:
**{
'proc_info': proc_info._proc_info_handler,
Expand Down Expand Up @@ -223,4 +224,5 @@ def validate(self):
# Make sure the function signature of the launch configuration
# generator is correct
for run in self._test_runs:
inspect.getcallargs(run.test_description_function, lambda: None)
print(run.test_description_function)
inspect.getcallargs(run.test_description_function, ready_fn=lambda: None)
15 changes: 12 additions & 3 deletions apex_launchtest/apex_launchtest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ def wrapper(*args, **kwargs):

class TestRun:

def __init__(self, test_description_function, pre_shutdown_tests, post_shutdown_tests):
def __init__(self,
test_description_function,
param_args,
pre_shutdown_tests,
post_shutdown_tests):

self.test_description_function = test_description_function
self.normalized_test_description = _normalize_ld(test_description_function)

self.param_args = param_args

self.pre_shutdown_tests = pre_shutdown_tests
self.post_shutdown_tests = post_shutdown_tests

Expand Down Expand Up @@ -74,14 +82,15 @@ def LoadTestsFromPythonModule(module):
if hasattr(module.generate_test_description, '__parametrized__'):
normalized_test_description_func = module.generate_test_description
else:
normalized_test_description_func = [module.generate_test_description]
normalized_test_description_func = [(module.generate_test_description, {})]

# If our test description is parameterized, we'll load a set of tests for each
# individual launch
return [TestRun(description,
args,
PreShutdownTestLoader().loadTestsFromModule(module),
PostShutdownTestLoader().loadTestsFromModule(module))
for description in normalized_test_description_func]
for description, args in normalized_test_description_func]


def PreShutdownTestLoader():
Expand Down
4 changes: 3 additions & 1 deletion apex_launchtest/apex_launchtest/parametrize.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def __init__(self, func):
for val in argvalues:
partial_args = dict(zip(argnames, val))

partial = functools.partial(func, **partial_args)
functools.update_wrapper(partial, func)
self.__calls.append(
functools.partial(func, **partial_args)
(partial, partial_args)
)

def __iter__(self):
Expand Down
56 changes: 56 additions & 0 deletions apex_launchtest/examples/parameters.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2019 Apex.AI, Inc.
#
# 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 os
import unittest

import ament_index_python
import apex_launchtest
import apex_launchtest.util
import launch
import launch.actions


@apex_launchtest.parametrize("arg_param", ['thing=On', 'thing=Off', 'flag1'])
def generate_test_description(arg_param, ready_fn):

terminating_process = launch.actions.ExecuteProcess(
cmd=[
os.path.join(
ament_index_python.get_package_prefix('apex_launchtest'),
'lib/apex_launchtest',
'terminating_proc',
),
# Use the parameter passed to generate_test_description as an argument
# to the terminating_proc
'--{}'.format(arg_param),
]
)

return (
launch.LaunchDescription([
terminating_process,
apex_launchtest.util.KeepAliveProc(),
launch.actions.OpaqueFunction(function=lambda context: ready_fn())
]),
{'dut_process': terminating_process}
)


class TestProcessOutput(unittest.TestCase):

# Note that 'arg_param' is automatically given to the test case, even though it was not
# part of the test context.
def test_process_outputs_expectd_value(self, proc_output, arg_param):
proc_output.assertWaitFor('--' + arg_param, timeout=10)
2 changes: 2 additions & 0 deletions apex_launchtest/test/test_apex_runner_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_catches_bad_signature(self):
dut = ApexRunner(
[TR(
test_description_function=lambda: None,
param_args={},
pre_shutdown_tests=None,
post_shutdown_tests=None,
)]
Expand All @@ -36,6 +37,7 @@ def test_catches_bad_signature(self):
dut = ApexRunner(
[TR(
test_description_function=lambda ready_fn: None,
param_args={},
pre_shutdown_tests=None,
post_shutdown_tests=None,
)]
Expand Down
8 changes: 4 additions & 4 deletions apex_launchtest/test/test_parametrize_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_binding_arguments():
def fake_test_description(val):
results.append(val)

for func in fake_test_description:
for func, params in fake_test_description:
func()

assert results == [1, 2, 3]
Expand All @@ -46,7 +46,7 @@ def test_binding_one_tuples():
def fake_test_description(val):
results.append(val)

for func in fake_test_description:
for func, params in fake_test_description:
func()

assert results == [1, 2, 3]
Expand All @@ -60,7 +60,7 @@ def test_partial_binding():
def fake_test_description(val, arg):
results.append((val, arg))

for index, func in enumerate(fake_test_description):
for index, (func, params) in enumerate(fake_test_description):
func(arg=index)

assert results == [('x', 0), ('y', 1), ('z', 2)]
Expand All @@ -74,7 +74,7 @@ def test_multiple_args():
def fake_test_description(arg_1, arg_2):
results.append((arg_1, arg_2))

for index, func in enumerate(fake_test_description):
for index, (func, params) in enumerate(fake_test_description):
func()

assert results == [(5, 10), (15, 20), (25, 30)]
4 changes: 2 additions & 2 deletions apex_launchtest/test/test_runner_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def generate_test_description(ready_fn):

with mock.patch('apex_launchtest.apex_runner._RunnerWorker._run_test'):
runner = ApexRunner(
[TR(generate_test_description, [], [])]
[TR(generate_test_description, {}, [], [])]
)

pre_result, post_result = runner.run()
Expand Down Expand Up @@ -93,7 +93,7 @@ def generate_test_description(ready_fn):

with mock.patch('apex_launchtest.apex_runner._RunnerWorker._run_test'):
runner = ApexRunner(
[TR(generate_test_description, [], [])]
[TR(generate_test_description, {}, [], [])]
)

pre_result, post_result = runner.run()
Expand Down

0 comments on commit 83d860b

Please sign in to comment.