Skip to content
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

docs(guidelines): update integration unit testing guidelines #142

Merged
2 commits merged into from Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions docs/contributing/testing-guidelines/integration-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ and the output is a set of properly integrated software modules that is ready fo

Integration tests determine if independently developed software modules work correctly when the modules are connected to each other.
In ROS 2, the software modules are called nodes.
As a special case, testing a single node can be referred to as component testing.
Testing a single node is a special type of integration test that is commonly referred to as component testing.

Integration tests help to find the following types of errors:

- Incompatible interactions between nodes, such as non-matching topics, different message types, or incompatible QoS settings.
- Edge cases that were not touched by unit testing, such as a critical timing issue, network communication delays, disk I/O failures, and many other problems that can occur in production environments.
- Edge cases that were not touched by unit testing, such as a critical timing issue, network communication delays, disk I/O failures, and other such problems that can occur in production environments.
- Issues that can occur while the system is under high CPU/memory load, such as `malloc` failures. This can be tested using tools like `stress` and `udpreplay` to test the performance of nodes with real data.

With ROS 2, it is possible to program complex autonomous-driving applications with a large number of nodes.
Expand Down Expand Up @@ -99,11 +99,8 @@ In addition to the command `add_ros_test`, we also install any data that is requ

!!! note

The `TIMEOUT` argument is given in seconds; see the [add_ros_test.cmake file](https://github.com/ros2/ros_testing/blob/master/ros_testing/cmake/add_ros_test.cmake) for details.

!!! note

The `add_ros_test` command will run the test in a unique `ROS_DOMAIN_ID` which avoids interference between tests running in parallel.
- The `TIMEOUT` argument is given in seconds; see the [add_ros_test.cmake file](https://github.com/ros2/ros_testing/blob/master/ros_testing/cmake/add_ros_test.cmake) for details.
- The `add_ros_test` command will run the test in a unique `ROS_DOMAIN_ID` which avoids interference between tests running in parallel.

To create a test,
either read the [launch_testing quick-start example](https://github.com/ros2/launch/tree/master/launch_testing#quick-start-example),
Expand Down Expand Up @@ -160,13 +157,9 @@ def generate_test_description():

!!! note

Since the node need time to process the input lanelet2 map,
we use a `TimerAction` to delay the start of the test by 1s.

!!! note

Here the `context` is empty but could be used to pass objects to the test cases.
You can find an example of using the `context`[here](https://github.com/ros2/launch/blob/humble/launch_testing/test/launch_testing/examples/context_launch_test.py)
- Since the node need time to process the input lanelet2 map, we use a `TimerAction` to delay the start of the test by 1s.
- In the example above, the `context` is empty but it can be used to pass objects to the test cases.
- You can find an example of using the `context` in the [ROS 2 context_launch_test.py](https://github.com/ros2/launch/blob/humble/launch_testing/test/launch_testing/examples/context_launch_test.py) test example.

Finally, a test is executed after the node executable has been shut down (`post_shutdown_test`).
Here we ensure that the node was launched without error and exited cleanly.
Expand Down Expand Up @@ -210,7 +203,7 @@ build/map_loader/test_results/map_loader/test_lanelet2_map_loader_launch.test.py

### Next steps

The simple test described in [Integration test with a single node: component test](#integration-test-with-a-single-node-component-test) can be extended in numerous directions:
The simple test described in [Integration test with a single node: component test](#integration-test-with-a-single-node-component-test) can be extended in numerous directions, such as testing a node's output.

#### Testing the output of a node

Expand Down
13 changes: 7 additions & 6 deletions docs/contributing/testing-guidelines/unit-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Unit testing is the first phase of testing and is used to validate units of source code such as classes and functions.
Typically, a unit of code is tested by validating its output for various inputs.
Unit testing helps ensure that the code behaves as intended and prevents accidental change of behavior.
Unit testing helps ensure that the code behaves as intended and prevents accidental changes of behavior.

Autoware uses the `ament_cmake` framework to build and run tests.
The same framework is also used to analyze the test results.
Expand All @@ -28,8 +28,6 @@ TEST(TestMyCoolPkg, TestHello) {
}
```

For more examples of `gtest` features, see the [gtest repo](https://github.com/google/googletest).

In `package.xml`, add the following line:

```xml
Expand All @@ -48,7 +46,7 @@ endif()
```

This automatically links the test with the default main function provided by `gtest`.
The code under test is usually in a different CMake target (`${PROJECT_NAME}` in the example) and its shared object for linking need to be added.
The code under test is usually in a different CMake target (`${PROJECT_NAME}` in the example) and its shared object for linking needs to be added.

To register a new `gtest` item, wrap the test code with the macro `TEST ()`.
`TEST ()` is a predefined macro that helps generate the final test code,
Expand All @@ -58,8 +56,11 @@ The test case name should be in CamelCase, since gtest inserts an underscore bet
`gtest/gtest.h` also contains predefined macros of `gtest` like `ASSERT_TRUE(condition)`,
`ASSERT_FALSE(condition)`, `ASSERT_EQ(val1,val2)`, `ASSERT_STREQ(str1,str2)`, `EXPECT_EQ()`, etc.
`ASSERT_*` will abort the test if the condition is not satisfied,
while `EXPECT_*` will mark the test as failed but continue to next test condition.
More information about `gtest` can be found in the [gtest repo](https://github.com/google/googletest).
while `EXPECT_*` will mark the test as failed but continue on to the next test condition.

!!! info

More information about `gtest` and its features can be found in the [gtest repo](https://github.com/google/googletest).

In the demo `CMakeLists.txt`, `ament_add_ros_isolated_gtest` is a predefined macro in `ament_cmake_ros` that helps simplify adding `gtest` code.
Details can be viewed in [ament_add_gtest.cmake](https://github.com/ros2/ament_cmake_ros/tree/master/ament_cmake_ros/cmake).
Expand Down