ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Any way to use a TestSuite (instead a TestCase) with rostest?

asked 2014-01-27 01:47:37 -0600

updated 2014-06-02 03:58:59 -0600

UPDATE: See rosticket #423

I am using rostest under rosbuild to create unittests with Python.

I understand how to create unittests and rostests, for instance, to create a TestsCase and call it from rostest.rosrun:

import unittest
class MyFirstTestCase(unittest.TestCase):
    def setUp(self):
        # SetUp code

    def tearDown(self):
        # TearDown code

    def test_something(self):
        # some test code

    def test_more_stuff(self):
        # more test code


if __name__ == '__main__':
    import rostest
    rostest.rosrun('my_package', 'test_my_first_test_case', MyFirstTestCase)

However, I you want to create more TestCases it is good idea to add them to a TestSuite. For instance something like this:

import unittest
class MyFirstTestCase(unittest.TestCase):
    # ...

class AnotherTestCase(unittest.TestCase):
    def setUp(self):
        # SetUp code

    def tearDown(self):
        # TearDown code

    def test_another_thing(self):
        # some test code

    def test_yet_another_thing(self):
        # more test code


if __name__ == '__main__':

    def suite():
        suite = unittest.TestSuite()
        suite.addTest(MyFirstTestCase('test_something'))
        suite.addTest(AnotherTestCase('test_another_thing'))
        return suite

    my_suite = suite()
    import rostest
    rostest.rosrun('my_package', 'test_my_first_test_suite', my_suite)

Note that the call to rostest.rosrun uses a unittest.TestSuite as argument rather than a unittest.TestCase.

But it seems that rostest.rosrun is not compatible with unittest.TestSuite, since after runing this code you would get a TypeError like the following:

rostest.rosrun('my_package','test_my_first_test_case', my_suite)
File "/opt/ros/groovy/lib/python2.7/dist-packages/rostest/__init__.py", line 136, in rosrun suite = unittest.TestLoader().loadTestsFromTestCase(test) File "/usr/lib/python2.7/unittest/loader.py", line 50, in loadTestsFromTestCase if issubclass(testCaseClass, suite.TestSuite): TypeError: issubclass() arg 1 must be a class

After reading the documentation and looking for some examples I did not found any way to call a TestSuite from rostest. Is it any way to properly create a TestSuite under rostest?

Some partial solution would be calling the different TestCases in different calls to rostest.rosrun like that:

rostest.rosrun('my_package', 'test_my_first_test_case', MyFirstTestCase)
rostest.rosrun('my_package', 'test_another_test_case', AnotherTestCase)

But, somehow, rostest only outputs the results for the second call to rostest.rosrun unless --text is passed (see this question). This is not desirable since --text is more verbose and increases testing time. Moreover, I believe using a TestSuite would be much more flexible than manully adding calls to rostest.rosrun.

EDIT: I edited the title to make the question clearer.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-08-30 06:45:33 -0600

Keivan gravatar image

A small update on this in case people come here for an answer. Rostests do work with TestSuite objects now. This issue fixed the problem in ROS Indigo release. There is also a simple example on the github page.

What is missing is how to run tests that are part of a test case and not override runTest method of a TestCase object. And this is also more of a Python thing than ROS. Implementing the TestSuite like this solves the issue and allows you to run the test suite:

class SuiteTest(unittest.TestSuite):

    def __init__(self) -> None:
        super(SuiteTest, self).__init__()
        self.loader = unittest.TestLoader()
        self.addTest(self.loader.loadTestsFromTestCase(CaseA))
        self.addTest(self.loader.loadTestsFromTestCase(CaseB))

Running the test as explained in the github link would be python rostest.rosrun('test_rosbag', 'temp_tests', 'tmp.SuiteTest', sys.argv) where tmp is the file name.

edit flag offensive delete link more

Comments

I marked your post as an answer, as it looks indeed ros_comm#423is addressed. And there's already wiki document that utilizes this development http://wiki.ros.org/unittest#Using_Te...

130s gravatar image 130s  ( 2022-08-30 11:36:30 -0600 )edit
1

answered 2014-05-30 17:34:08 -0600

130s gravatar image

updated 2014-05-30 17:35:44 -0600

My guess is that (someone correct me if I'm wrong) the term suite may be used in rostest not as its specific usage in software testing context (as in testsuite) but as something more general, like composite or group of TestCases. And unittest.TestSuite isn't handled yet in rostest.

Thus it's worth opening an enhancement request on its issue tracker.

(As a workaround I usually put TestCase classes in separate .py files, which isn't that cumbersome if I have only several TestCases.)

edit flag offensive delete link more

Comments

1

Thanks Isaac, I opened an issue 423

Victor Gonzalez-Pacheco gravatar image Victor Gonzalez-Pacheco  ( 2014-06-02 03:59:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-01-27 01:47:37 -0600

Seen: 1,180 times

Last updated: Aug 30 '22