Skip to content

How to implement an integration test?

Konstantinos Panayiotou edited this page Jul 13, 2016 · 5 revisions

It is recommended to study on Rapp-Testing-Tools first.

Basic documentation on "Developing Integration Tests" can be found here.

A more-in-depth information on how to write your first integration test is presented here.

The template_test.py will be used as a reference.

Each integration test must have the following characteristics:

  • Each test class is written as a seperate python source file (.py).
  • Each test inherits from unittest.TestCase class.
  • The Python RAPP Platform API is used to call RAPP Platform Services.

If you are not familiar with the Python unit testing framework (unittest), start reading from there as the rapp_testing_tools is build ontop of it. Also, basic knowledge of using the python-rapp-platform-api is required.

So lets create a test class for testing the integration behaviour of the Face-Detection RAPP Platform service/functionality.

Step 1: Create the source file for the test class

Copy the template_test.py file and name it face_detection_tests.py under the default_tests directory

$ cd ~/rapp_platform/rapp-platform-catkin-ws/src/rapp-platform/rapp_testing_tools/scripts/default_tests
$ cp template_test.py face_detection_tests.py

Rename the test class to FaceDetectionTests:

class FaceDetectionTests(unittest.TestCase):

    def setUp(self):
        self.ch = RappPlatformAPI()

        rospack = rospkg.RosPack()
        self.pkgDir = rospack.get_path('rapp_testing_tools')

    def test_templateTest(self):
        self.assertEqual(1, 1)

Note that the setUp() method instantiates a RappPlatformAPI() object for calling RAPP Platform Services.

Step 2: Implement a test case

As the FaceDetectionTests class inherits from unittest.TestCase, individual tests are defined with methods whose names start with the letters test

We will implement a test of performing face-detection on a single image file (Lenna.png). We assume that the image file was previously stored under the test_data directory. So lets create a member method named test_lenna This method will be responsible for loading the image file, call the RAPP Platform Service, through the API, and evaluate the results using unittest assertions

class FaceDetectionTests(unittest.TestCase):

    def setUp(self):
        self.ch = RappPlatformAPI()

        rospack = rospkg.RosPack()
        self.pkgDir = rospack.get_path('rapp_testing_tools')

    def test_lenna(self):
        self.assertEqual(1, 1)
        response = self.ch.faceDetection(imagepath)

        valid_faces = [{
            'up_left_point': {'y': 201.0, 'x': 213.0},
            'down_right_point': {'y': 378.0, 'x': 390.0}
        }]

        self.assertEqual(response['error'], u'')
        self.assertEqual(response['faces'], valid_faces)

The first assertion, self.assertEqual(response['error'], u'') evaluates that no error was reported from the RAPP Platform The second assertion evaluates the response from the FaceDetector.

The complete face_detection_tests.py source file is:

from os import path
import timeit
import unittest
import rospkg

__path__ = os.path.dirname(os.path.realpath(__file__))

from RappCloud import RappPlatformAPI


class FaceDetectionTests(unittest.TestCase):

    def setUp(self):
        self.ch = RappPlatformAPI()

        rospack = rospkg.RosPack()
        self.pkgDir = rospack.get_path('rapp_testing_tools')

    def test_lenna(self):
        self.assertEqual(1, 1)
        response = self.ch.faceDetection(imagepath)

        valid_faces = [{
            'up_left_point': {'y': 201.0, 'x': 213.0},
            'down_right_point': {'y': 378.0, 'x': 390.0}
        }]

        self.assertEqual(response['error'], u'')
        self.assertEqual(response['faces'], valid_faces)


if __name__ == "__main__":
    unittest.main()

Step 3: Executing the Face Detection test case

Head to the scripts directory and execute the rapp_run_test.py script, giving as input argument the face_detection_tests.py file to execute:

$ cd ~/rapp_platform/rapp-platform-catkin-ws/src/rapp-platform/rapp_testing_tools/scripts
$ ./rapp_run_test.py -i default_tests/face_detection_tests.py

On successful test execution, the output should be:

***************************
    RAPP Platfrom Tests
***************************

* Parameters:
  -- Number of Executions for each given test: [1]
  -- Serial execution

* Tests to Execute:
1] face_detection_tests x1


Running face_detection_tests...
        Ran 1 tests in 0.383s
        Success

If an error occures on the RAPP Platform, the returned error message will be reported to the console output. For example, if the RAPP Platform Web Server was not previously launched properly, we should get a "Connection" error on the assertion of the error property of the response:

***************************
     RAPP Platfrom Tests
***************************

* Parameters:
-- Number of Executions for each given test: [1]
-- Serial execution

* Tests to Execute:
1] face_detection_tests x1


Running face_detection_tests...
        Ran 1 test in 0.078s
        Failed

======================================================================
FAIL: test_lenna (__main__.FaceDetectionTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/rappuser/rapp_platform/rapp-platform-catkin-ws/src/rapp-platform/rapp_testing_tools/scripts/default_tests/face_detection_tests.py", line 61, in test_lenna
    self.assertEqual(response['error'], u'')
AssertionError: 'Connection Error' != u''

----------------------------------------------------------------------
Ran 1 test in 0.078s

FAILED (failures=1)
Clone this wiki locally