Added the ability to specify the name of the package that contains the

test file.  This is needed when the test file is a compiled program (e.g.,
gtest) that lives in the build tree, where rospkg's package-name-inference
strategy doesn't behave as expected.  The new stuff is optional; when not
given, the old behavior persists.
This commit is contained in:
Brian Gerkey 2012-01-03 02:08:30 +00:00
parent a0d795ef94
commit d97be9ae15
2 changed files with 18 additions and 4 deletions

View File

@ -62,7 +62,7 @@ class TestTimeoutException(Exception): pass
class BareTestCase(unittest.TestCase):
def __init__(self, exe, args, retry=0, time_limit=None, test_name=None, text_mode=False):
def __init__(self, exe, args, retry=0, time_limit=None, test_name=None, text_mode=False, package_name=None):
"""
@param exe: path to executable to run
@type exe: str
@ -73,10 +73,15 @@ class BareTestCase(unittest.TestCase):
@type time_limit: float
@param test_name: (optional) override automatically generated test name
@type test_name: str
@param package_name: (optional) override automatically inferred package name
@type package_name: str
"""
super(BareTestCase, self).__init__()
self.text_mode = text_mode
self.package = rospkg.get_package_name(exe)
if package_name:
self.package = package_name
else:
self.package = rospkg.get_package_name(exe)
self.exe = os.path.abspath(exe)
if test_name is None:
self.test_name = os.path.basename(exe)

View File

@ -63,6 +63,9 @@ def rosunitmain():
parser.add_option("--name", metavar="TEST_NAME",
dest="test_name", default=None,
help="Test name")
parser.add_option("--package", metavar="PACKAGE_NAME",
dest="pkg", default=None,
help="Package name (optional)")
(options, args) = parser.parse_args()
if len(args) < 1:
@ -78,8 +81,14 @@ def rosunitmain():
test_name = test_name[:test_name.rfind('.')]
time_limit = float(options.time_limit) if options.time_limit else None
# If the caller didn't tell us the package name, we'll try to infer it.
# compute some common names we'll be using to generate test names and files
pkg = rospkg.get_package_name(test_file)
pkg = options.pkg
if not pkg:
pkg = rospkg.get_package_name(test_file)
if not pkg:
print "Error: failed to determine package name for file '%s'; maybe you should supply the --package argument to rosunit?"%(test_file)
sys.exit(1)
try:
runner_result = None
@ -87,7 +96,7 @@ def rosunitmain():
test_case = BareTestCase(test_file, args[1:], \
retry=0, time_limit=time_limit, \
test_name=test_name, text_mode=options.text_mode)
test_name=test_name, text_mode=options.text_mode, package_name=pkg)
suite = unittest.TestSuite()
suite.addTest(test_case)