Skip to content

Commit

Permalink
catkin test: add option to manually specify test target
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Apr 18, 2021
1 parent fa095d7 commit dcd8730
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 4 deletions.
14 changes: 13 additions & 1 deletion catkin_tools/jobs/catkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ def create_catkin_test_job(
context,
package,
package_path,
test_target,
):
"""Generate a job that tests a package"""

Expand All @@ -606,10 +607,21 @@ def create_catkin_test_job(
verbose=False,
))

# Check if the test target exists
# make -q target_name returns 2 if the target does not exist, in that case we want to terminate this test job
# the other cases (0=target is up-to-date, 1=target exists but is not up-to-date) can be ignored
stages.append(CommandStage(
'findtest',
[MAKE_EXEC, '-q', test_target],
cwd=build_space,
early_termination_retcode=2,
success_retcodes=(0, 1, 2),
))

# Make command
stages.append(CommandStage(
'make',
[MAKE_EXEC, 'run_tests'],
[MAKE_EXEC, test_target],
cwd=build_space,
logger_factory=CMakeMakeRunTestsIOBufferProtocol.factory,
))
Expand Down
7 changes: 4 additions & 3 deletions catkin_tools/jobs/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def create_cmake_test_job(
context,
package,
package_path,
test_target
):
"""Generate a job to test a cmake package"""
# Package build space path
Expand All @@ -433,11 +434,11 @@ def create_cmake_test_job(
))

# Check if the test target exists
# make -q test returns 2 if the test target does not exist, in that case we want to terminate this test job
# make -q target_name returns 2 if the target does not exist, in that case we want to terminate this test job
# the other cases (0=target is up-to-date, 1=target exists but is not up-to-date) can be ignored
stages.append(CommandStage(
'findtest',
[MAKE_EXEC, '-q', 'test'],
[MAKE_EXEC, '-q', test_target],
cwd=build_space,
early_termination_retcode=2,
success_retcodes=(0, 1, 2),
Expand All @@ -446,7 +447,7 @@ def create_cmake_test_job(
# Make command
stages.append(CommandStage(
'make',
[MAKE_EXEC, 'test'],
[MAKE_EXEC, test_target],
cwd=build_space,
logger_factory=IOBufferProtocol.factory,
))
Expand Down
15 changes: 15 additions & 0 deletions catkin_tools/verbs/catkin_test/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def prepare_arguments(parser):
add = config_group.add_argument
add('-p', '--parallel-packages', metavar='PACKAGE_JOBS', dest='parallel_jobs', default=None, type=int,
help='Maximum number of packages allowed to be built in parallel (default is cpu count)')
add('-t', '--test-target', metavar='TARGET', default=None, type=str,
help='Make target to run for tests (default is "run_tests" for catkin and "test" for cmake)')
add('--catkin-test-target', metavar='TARGET', default=None, type=str,
help='Make target to run for tests for catkin packages, overwrites --test-target (default is "run_tests")')

interface_group = parser.add_argument_group('Interface', 'The behavior of the command-line interface.')
add = interface_group.add_argument
Expand Down Expand Up @@ -124,6 +128,15 @@ def main(opts):
if opts.verbose and 'VERBOSE' not in os.environ:
os.environ['VERBOSE'] = '1'

# Get test targets
catkin_test_target = 'run_tests'
cmake_test_target = 'test'
if opts.test_target:
catkin_test_target = opts.test_target
cmake_test_target = opts.test_target
if opts.catkin_test_target:
catkin_test_target = opts.catkin_test_target

return test_workspace(
ctx,
packages=opts.packages,
Expand All @@ -134,4 +147,6 @@ def main(opts):
no_notify=opts.no_notify,
continue_on_failure=opts.continue_on_failure,
summarize_build=opts.summarize,
catkin_test_target=catkin_test_target,
cmake_test_target=cmake_test_target,
)
11 changes: 11 additions & 0 deletions catkin_tools/verbs/catkin_test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_workspace(
no_notify=False,
continue_on_failure=False,
summarize_build=False,
catkin_test_target='run_tests',
cmake_test_target='test',
):
"""Tests a catkin workspace
Expand All @@ -56,6 +58,10 @@ def test_workspace(
:type continue_on_failure: bool
:param summarize_build: summarizes the build at the end
:type summarize_build: bool
:param catkin_test_target: make target for tests in catkin packages
:type catkin_test_target: str
:param cmake_test_target: make target for tests in cmake packages
:type cmake_test_target: str
"""
pre_start_time = time.time()

Expand Down Expand Up @@ -141,6 +147,11 @@ def test_workspace(
# Create the job based on the build type
build_type = pkg.get_build_type()

if build_type == 'catkin':
test_job_kwargs['test_target'] = catkin_test_target
elif build_type == 'cmake':
test_job_kwargs['test_target'] = cmake_test_target

if build_type in test_job_creators:
jobs.append(test_job_creators[build_type](**test_job_kwargs))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.8.12)
project(python_tests_targets)
find_package(catkin REQUIRED)

catkin_package()

if(CATKIN_ENABLE_TESTING)
catkin_add_nosetests(test_good.py)
catkin_add_nosetests(test_bad.py)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<package>
<name>python_tests_targets</name>
<version>0.1.0</version>
<license>BSD</license>
<maintainer email="todo@todo.com">todo</maintainer>
<description>This package contains two python tests.</description>

<buildtool_depend>catkin</buildtool_depend>
<test_depend>unittest</test_depend>
</package>

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup()
setup(**d)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

import unittest


class TestBad(unittest.TestCase):

def test_zero(self):
self.assertEqual(0, 1)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

import unittest


class TestGood(unittest.TestCase):

def test_zero(self):
self.assertEqual(0, 0)

if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions tests/system/verbs/catkin_test/test_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ def test_skip_missing_test():
with redirected_stdio() as (out, err):
assert catkin_success(['build', 'cmake_pkg', '--no-notify', '--no-status'])
assert catkin_success(['test', '--no-notify', '--no-status'])


@in_temporary_directory
def test_other_target():
"""Test with a manually specified target"""
cwd = os.getcwd()
source_space = os.path.join(cwd, 'src')
shutil.copytree(os.path.join(RESOURCES_DIR, 'catkin_pkgs', 'python_tests_targets'), source_space)

with redirected_stdio() as (out, err):
assert catkin_success(['build', 'python_tests_targets', '--no-notify', '--no-status'])
assert catkin_success(['test', '--test-target', 'run_tests_python_tests_targets_nosetests_test_good.py',
'--no-notify', '--no-status'])
assert catkin_failure(['test', '--test-target', 'run_tests_python_tests_targets_nosetests_test_bad.py',
'--no-notify', '--no-status'])

0 comments on commit dcd8730

Please # to comment.