-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c177332
Enable launch test discovery in pytest.
hidmic 67d53fe
Test examples using pytest.
hidmic 94922bc
Enable downstream customization of launch tests execution.
hidmic 8014b4c
Move launch testing examples README content to the root README.
hidmic 4491759
Mark launch tests explicitly.
hidmic 5be3345
Refactor parameterization support to play nice with pytest markers.
hidmic e7cc2b9
Improve pytest output on launch test failure.
hidmic b658591
Please flake8
hidmic d15a47c
Fix failing launch_testing tests.
hidmic b4b56a2
Prevent pytest from performing any assertion rewriting outside the la…
hidmic 0705777
Change explicit setattr for regular assignment.
hidmic c7e6f80
Better document PYTEST_DONT_REWRITE docstring.
hidmic dfa47c4
Please flake8
hidmic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
) | ||
|
||
runner.validate() | ||
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() | ||
}) if excinfo.value.results else '' | ||
return super().repr_failure(excinfo) | ||
|
||
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: | ||
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' | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
A
SyntaxError
is the only error I'm expecting. I'd rather not catch (and hide) unexpected errors e.g. some exception as a result of code being executed at the module level.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.
This is old code, but just as reference:
https://github.com/ros2/launch_ros/blob/bf3d800a53ea53d9465da9298c104998a7461dcf/ros2launch/ros2launch/command/launch.py#L125-L141
Couldn't value error happen here too?