-
Notifications
You must be signed in to change notification settings - Fork 139
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
Enable launch test discovery in pytest #312
Changes from 6 commits
c177332
67d53fe
94922bc
8014b4c
4491759
5be3345
e7cc2b9
b658591
d15a47c
b4b56a2
0705777
c7e6f80
dfa47c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# Copyright 2019 Open Source Robotics Foundation, 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 pytest | ||
|
||
from ..loader import LoadTestsFromPythonModule | ||
from ..test_runner import LaunchTestRunner | ||
|
||
|
||
class LaunchTestFailure(Exception): | ||
|
||
def __init__(self, message, results): | ||
super().__init__() | ||
self.message = message | ||
self.results = results | ||
|
||
def __str__(self): | ||
return self.message | ||
|
||
|
||
class LaunchTestItem(pytest.Item): | ||
|
||
def __init__(self, name, parent, test_runs, runner_cls=LaunchTestRunner): | ||
super().__init__(name, parent) | ||
self.test_runs = test_runs | ||
self.runner_cls = runner_cls | ||
|
||
def runtest(self): | ||
launch_args = sum(( | ||
args_set for args_set in self.config.getoption('--launch-args') | ||
), []) | ||
runner = self.runner_cls( | ||
test_runs=self.test_runs, | ||
launch_file_arguments=launch_args, | ||
debug=self.config.getoption('verbose') | ||
) | ||
try: | ||
runner.validate() | ||
except Exception as e: | ||
raise LaunchTestFailure(message=str(e), results=[]) | ||
|
||
results_per_run = runner.run() | ||
if any(not result.wasSuccessful() for result in results_per_run.values()): | ||
raise LaunchTestFailure( | ||
message='some test cases have failed', results=results_per_run | ||
) | ||
|
||
def repr_failure(self, excinfo): | ||
if isinstance(excinfo.value, LaunchTestFailure): | ||
return excinfo.value.message + ':\n' + '\n'.join({ | ||
'{} failed at {}.{}'.format( | ||
str(test_run), | ||
type(test_case).__name__, | ||
test_case._testMethodName | ||
) | ||
for test_run, test_result in excinfo.value.results.items() | ||
for test_case, _ in (test_result.errors + test_result.failures) | ||
if not test_result.wasSuccessful() | ||
}) | ||
|
||
def reportinfo(self): | ||
return self.fspath, 0, 'launch tests: {}'.format(self.name) | ||
|
||
|
||
class LaunchTestModule(pytest.File): | ||
|
||
def makeitem(self, *args, **kwargs): | ||
return LaunchTestItem(*args, **kwargs) | ||
|
||
def collect(self): | ||
module = self.fspath.pyimport() | ||
yield self.makeitem( | ||
name=module.__name__, parent=self, | ||
test_runs=LoadTestsFromPythonModule( | ||
module, name=module.__name__ | ||
) | ||
) | ||
|
||
|
||
def find_launch_test_entrypoint(path): | ||
try: | ||
return getattr(path.pyimport(), 'generate_test_description', None) | ||
except SyntaxError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that's the only possible exception, I would catch everything. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is old code, but just as reference: Couldn't value error happen here too? |
||
return None | ||
|
||
|
||
def pytest_pycollect_makemodule(path, parent): | ||
entrypoint = find_launch_test_entrypoint(path) | ||
if entrypoint is not None: | ||
ihook = parent.session.gethookproxy(path) | ||
module = ihook.pytest_launch_collect_makemodule( | ||
path=path, parent=parent, entrypoint=entrypoint | ||
) | ||
if module is not None: | ||
return module | ||
if path.basename == '__init__.py': | ||
return pytest.Package(path, parent) | ||
return pytest.Module(path, parent) | ||
|
||
|
||
@pytest.hookimpl(trylast=True) | ||
def pytest_launch_collect_makemodule(path, parent, entrypoint): | ||
marks = getattr(entrypoint, 'pytestmark', []) | ||
if marks and any(m.name == 'launch_test' for m in marks): | ||
return LaunchTestModule(path, parent) | ||
|
||
|
||
def pytest_addhooks(pluginmanager): | ||
import launch_testing.pytest.hookspecs as hookspecs | ||
pluginmanager.add_hookspecs(hookspecs) | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption( | ||
'--launch-args', action='append', nargs='*', | ||
default=[], help='One or more Launch test arguments' | ||
) | ||
|
||
|
||
def pytest_configure(config): | ||
config.addinivalue_line( | ||
'markers', 'launch_test: mark a generate_test_description function as a launch test entrypoint' | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2019 Open Source Robotics Foundation, 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 pytest | ||
|
||
|
||
@pytest.hookspec(firstresult=True) | ||
def pytest_launch_collect_makemodule(path, parent, entrypoint): | ||
"""Make launch test module appropriate for the found test entrypoint.""" | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[pytest] | ||
# Set testpaths, otherwise pytest finds 'tests' in the examples directory | ||
testpaths = test | ||
# Add arguments for launch tests | ||
addopts = --launch-args dut_arg:=test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't be:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it can be. Done in 0705777.