File size: 11,570 Bytes
d6b3397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | .. _testing_mock_interfaces:
Mock Interfaces for Unit Testing
================================
Isaac Lab provides mock implementations of sensor, asset, and PhysX view classes
for unit testing without requiring Isaac Sim or GPU simulation.
Overview
--------
Two levels of mock interfaces are available:
1. **High-level mocks** (``isaaclab.test.mock_interfaces``) - Mock sensors and assets
2. **Low-level PhysX mocks** (``isaaclab_physx.test.mock_interfaces``) - Mock PhysX TensorAPI views
High-Level Mock Interfaces
--------------------------
Located in ``isaaclab.test.mock_interfaces``, these mock the public API of sensor
and asset base classes.
Available Mocks
~~~~~~~~~~~~~~~
**Sensors:**
.. list-table::
:header-rows: 1
:widths: 25 25 50
* - Mock Class
- Real Class
- Description
* - ``MockImu``
- ``BaseImu``
- IMU sensor (accelerometer + gyroscope)
* - ``MockContactSensor``
- ``BaseContactSensor``
- Contact force sensor
* - ``MockFrameTransformer``
- ``BaseFrameTransformer``
- Frame tracking sensor
**Assets:**
.. list-table::
:header-rows: 1
:widths: 25 25 50
* - Mock Class
- Real Class
- Description
* - ``MockArticulation``
- ``BaseArticulation``
- Articulated robot (joints + bodies)
* - ``MockRigidObject``
- ``BaseRigidObject``
- Single rigid body
* - ``MockRigidObjectCollection``
- ``BaseRigidObjectCollection``
- Collection of rigid bodies
Quick Start
~~~~~~~~~~~
.. code-block:: python
import torch
from isaaclab.test.mock_interfaces import MockArticulation, MockContactSensor
# Create a mock quadruped robot
robot = MockArticulation(
num_instances=4,
num_joints=12,
num_bodies=13,
joint_names=[f"joint_{i}" for i in range(12)],
device="cpu"
)
# Set joint states
robot.data.set_mock_data(
joint_pos=torch.zeros(4, 12),
joint_vel=torch.randn(4, 12) * 0.1
)
# Access data like the real asset
positions = robot.data.joint_pos # Shape: (4, 12)
# Create a mock contact sensor
sensor = MockContactSensor(num_instances=4, num_bodies=4, device="cpu")
sensor.data.set_net_forces_w(torch.randn(4, 4, 3))
Factory Functions
~~~~~~~~~~~~~~~~~
Pre-configured factories for common use cases:
.. code-block:: python
from isaaclab.test.mock_interfaces import (
create_mock_quadruped,
create_mock_humanoid,
create_mock_foot_contact_sensor,
)
# Pre-configured quadruped (12 joints, 13 bodies)
robot = create_mock_quadruped(num_instances=4)
# Pre-configured humanoid (21 joints, 22 bodies)
humanoid = create_mock_humanoid(num_instances=2)
# Foot contact sensor for quadruped
foot_contact = create_mock_foot_contact_sensor(num_instances=4, num_feet=4)
Patching Utilities
~~~~~~~~~~~~~~~~~~
Use context managers or decorators to inject mocks into tests:
**Context Managers:**
.. code-block:: python
from isaaclab.test.mock_interfaces.utils import patch_articulation, patch_sensor
with patch_articulation("my_module.Articulation", num_joints=12) as MockRobot:
robot = my_function_that_creates_robot()
robot.data.set_joint_pos(torch.zeros(1, 12))
with patch_sensor("my_module.ContactSensor", "contact", num_bodies=4):
sensor = my_function_that_creates_sensor()
**Decorators:**
.. code-block:: python
from isaaclab.test.mock_interfaces.utils import mock_articulation, mock_sensor
@mock_articulation(num_joints=12, num_bodies=13)
def test_observation_function(mock_robot):
mock_robot.data.set_joint_pos(torch.zeros(1, 12))
obs = compute_observation(mock_robot)
assert obs.shape == (1, 24)
@mock_sensor("contact", num_instances=4, num_bodies=4)
def test_contact_reward(mock_contact):
mock_contact.data.set_net_forces_w(torch.randn(4, 4, 3))
reward = compute_contact_reward(mock_contact)
Low-Level PhysX Mock Interfaces
-------------------------------
Located in ``isaaclab_physx.test.mock_interfaces``, these mock the PhysX TensorAPI
views used internally by assets and sensors.
Available Mocks
~~~~~~~~~~~~~~~
.. list-table::
:header-rows: 1
:widths: 25 25 50
* - Mock Class
- PhysX Class
- Used By
* - ``MockRigidBodyView``
- ``physx.RigidBodyView``
- ``RigidObject``
* - ``MockArticulationView``
- ``physx.ArticulationView``
- ``Articulation``
* - ``MockRigidContactView``
- ``physx.RigidContactView``
- ``ContactSensor``
Quick Start
~~~~~~~~~~~
.. code-block:: python
from isaaclab_physx.test.mock_interfaces import (
MockRigidBodyView,
MockArticulationView,
MockRigidContactView,
)
# Create a mock rigid body view
view = MockRigidBodyView(count=4, device="cpu")
transforms = view.get_transforms() # Shape: (4, 7)
# Set mock data
view.set_mock_transforms(torch.randn(4, 7))
# Create a mock articulation view
art_view = MockArticulationView(
count=4,
num_dofs=12,
num_links=13,
device="cpu"
)
positions = art_view.get_dof_positions() # Shape: (4, 12)
Factory Functions
~~~~~~~~~~~~~~~~~
.. code-block:: python
from isaaclab_physx.test.mock_interfaces import (
create_mock_rigid_body_view,
create_mock_articulation_view,
create_mock_quadruped_view,
create_mock_rigid_contact_view,
)
# Pre-configured quadruped view (12 DOFs, 13 links)
view = create_mock_quadruped_view(count=4)
# Pre-configured humanoid view (21 DOFs, 22 links)
humanoid = create_mock_humanoid_view(count=2)
Patching Utilities
~~~~~~~~~~~~~~~~~~
.. code-block:: python
from isaaclab_physx.test.mock_interfaces.utils import (
patch_rigid_body_view,
patch_articulation_view,
mock_articulation_view,
)
# Context manager
with patch_rigid_body_view("my_module.physx.RigidBodyView", count=4):
view = create_rigid_body()
# Decorator
@mock_articulation_view(count=4, num_dofs=12, num_links=13)
def test_my_function(mock_view):
positions = mock_view.get_dof_positions()
assert positions.shape == (4, 12)
Data Shapes Reference
---------------------
High-Level Mocks
~~~~~~~~~~~~~~~~
**IMU Data:**
.. list-table::
:header-rows: 1
:widths: 25 20 55
* - Property
- Shape
- Description
* - ``pos_w``
- ``(N, 3)``
- Position in world frame
* - ``quat_w``
- ``(N, 4)``
- Orientation (w,x,y,z) in world frame
* - ``lin_vel_b``
- ``(N, 3)``
- Linear velocity in body frame
* - ``ang_vel_b``
- ``(N, 3)``
- Angular velocity in body frame
* - ``projected_gravity_b``
- ``(N, 3)``
- Gravity in body frame
**Contact Sensor Data:**
.. list-table::
:header-rows: 1
:widths: 25 20 55
* - Property
- Shape
- Description
* - ``net_forces_w``
- ``(N, B, 3)``
- Net contact forces
* - ``force_matrix_w``
- ``(N, B, M, 3)``
- Filtered forces
* - ``current_contact_time``
- ``(N, B)``
- Time in contact
* - ``current_air_time``
- ``(N, B)``
- Time in air
Where N = instances, B = bodies, M = filter bodies
**Articulation Data:**
.. list-table::
:header-rows: 1
:widths: 25 20 55
* - Property
- Shape
- Description
* - ``joint_pos``
- ``(N, J)``
- Joint positions
* - ``joint_vel``
- ``(N, J)``
- Joint velocities
* - ``root_link_pose_w``
- ``(N, 7)``
- Root pose in world
* - ``body_link_pose_w``
- ``(N, B, 7)``
- Body poses in world
Where N = instances, J = joints, B = bodies
PhysX View Mocks
~~~~~~~~~~~~~~~~
**RigidBodyView:**
.. list-table::
:header-rows: 1
:widths: 30 20 50
* - Method
- Shape
- Description
* - ``get_transforms()``
- ``(N, 7)``
- [pos(3), quat_xyzw(4)]
* - ``get_velocities()``
- ``(N, 6)``
- [lin_vel(3), ang_vel(3)]
* - ``get_masses()``
- ``(N, 1, 1)``
- mass per body
* - ``get_inertias()``
- ``(N, 1, 3, 3)``
- 3x3 inertia matrix
**ArticulationView:**
.. list-table::
:header-rows: 1
:widths: 35 20 45
* - Method
- Shape
- Description
* - ``get_root_transforms()``
- ``(N, 7)``
- Root pose
* - ``get_link_transforms()``
- ``(N, L, 7)``
- Link poses
* - ``get_dof_positions()``
- ``(N, J)``
- Joint positions
* - ``get_dof_limits()``
- ``(N, J, 2)``
- [lower, upper]
**RigidContactView:**
.. list-table::
:header-rows: 1
:widths: 40 20 40
* - Method
- Shape
- Description
* - ``get_net_contact_forces(dt)``
- ``(N*B, 3)``
- Flat net forces
* - ``get_contact_force_matrix(dt)``
- ``(N*B, F, 3)``
- Per-filter forces
Design Patterns
---------------
All mock interfaces follow these patterns:
1. **Lazy Initialization** - Tensors created on first access with correct shapes
2. **Device Transfer** - All setters call ``.to(device)``
3. **Identity Quaternion Defaults** - Quaternions default to identity
4. **Clone on Getters** - Return ``.clone()`` to prevent mutation
5. **No-op Actions** - Simulation operations (reset, update, write_to_sim) do nothing
6. **Mock Setters** - Direct ``set_mock_*`` methods for test setup
Example: Testing an Observation Function
----------------------------------------
.. code-block:: python
import pytest
import torch
from isaaclab.test.mock_interfaces import MockArticulation
@pytest.fixture
def robot():
return MockArticulation(
num_instances=4,
num_joints=12,
num_bodies=13,
device="cpu"
)
def test_joint_observation(robot):
# Setup
joint_pos = torch.randn(4, 12)
robot.data.set_joint_pos(joint_pos)
# Test your observation function
obs = compute_joint_observation(robot)
# Verify
assert obs.shape == (4, 24) # pos + vel
assert torch.allclose(obs[:, :12], joint_pos)
def test_body_observation(robot):
# Setup
body_poses = torch.randn(4, 13, 7)
robot.data.set_body_link_pose_w(body_poses)
# Test
obs = compute_body_observation(robot)
assert obs.shape == (4, 13, 7)
Example: Testing with PhysX View Mocks
--------------------------------------
.. code-block:: python
import pytest
import torch
from isaaclab_physx.test.mock_interfaces import create_mock_quadruped_view
from isaaclab_physx.test.mock_interfaces.utils import mock_articulation_view
def test_quadruped_joint_limits():
"""Test quadruped joint limit handling."""
view = create_mock_quadruped_view(count=4)
# Set custom joint limits
limits = torch.zeros(4, 12, 2)
limits[:, :, 0] = -1.5 # lower limit
limits[:, :, 1] = 1.5 # upper limit
view.set_mock_dof_limits(limits)
# Verify limits
result = view.get_dof_limits()
assert torch.allclose(result[:, :, 0], torch.full((4, 12), -1.5))
assert torch.allclose(result[:, :, 1], torch.full((4, 12), 1.5))
@mock_articulation_view(count=4, num_dofs=12, num_links=13)
def test_articulation_with_decorator(mock_view):
"""Test using the decorator pattern."""
# Set some initial positions
positions = torch.randn(4, 12)
mock_view.set_mock_dof_positions(positions)
# Verify
result = mock_view.get_dof_positions()
assert torch.allclose(result, positions)
|