Unverified Commit d6702dfa authored by Kelly Guo's avatar Kelly Guo Committed by GitHub

Updates some more tests to pytest (#543)

# Description

Updates a few missing tests that are still on unittest to pytest.

## Type of change

<!-- As you go through the list, delete the ones that are not
applicable. -->

- Bug fix (non-breaking change which fixes an issue)


## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [ ] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

<!--
As you go through the checklist above, you can mark something as done by
putting an x character in it

For example,
- [x] I have done this task
- [ ] I have not done this task
-->
parent 7ac5ad53
...@@ -5,70 +5,60 @@ ...@@ -5,70 +5,60 @@
import os import os
import subprocess import subprocess
import unittest
from pathlib import Path from pathlib import Path
import pytest
class TestDocker(unittest.TestCase):
"""Test starting and stopping of the docker container with both currently supported profiles and with and without
a suffix. This assumes that docker is installed and configured correctly so that the user can use the docker def start_stop_docker(profile, suffix):
commands from the current shell.""" """Test starting and stopping docker profile with suffix."""
environ = os.environ
def start_stop_docker(self, profile, suffix): context_dir = Path(__file__).resolve().parent.parent
"""Test starting and stopping docker profile with suffix."""
environ = os.environ # generate parameters for the arguments
context_dir = Path(__file__).resolve().parent.parent if suffix != "":
container_name = f"isaac-lab-{profile}-{suffix}"
# generate parameters for the arguments suffix_args = ["--suffix", suffix]
if suffix != "": else:
container_name = f"isaac-lab-{profile}-{suffix}" container_name = f"isaac-lab-{profile}"
suffix_args = ["--suffix", suffix] suffix_args = []
else:
container_name = f"isaac-lab-{profile}" run_kwargs = {
suffix_args = [] "check": False,
"capture_output": True,
run_kwargs = { "text": True,
"check": False, "cwd": context_dir,
"capture_output": True, "env": environ,
"text": True, }
"cwd": context_dir,
"env": environ, # start the container
} docker_start = subprocess.run(["python", "container.py", "start", profile] + suffix_args, **run_kwargs)
assert docker_start.returncode == 0
# start the container
docker_start = subprocess.run(["python", "container.py", "start", profile] + suffix_args, **run_kwargs) # verify that the container is running
self.assertEqual(docker_start.returncode, 0) docker_running_true = subprocess.run(["docker", "ps"], **run_kwargs)
assert docker_running_true.returncode == 0
# verify that the container is running assert container_name in docker_running_true.stdout
docker_running_true = subprocess.run(["docker", "ps"], **run_kwargs)
self.assertEqual(docker_running_true.returncode, 0) # stop the container
self.assertIn(container_name, docker_running_true.stdout) docker_stop = subprocess.run(["python", "container.py", "stop", profile] + suffix_args, **run_kwargs)
assert docker_stop.returncode == 0
# stop the container
docker_stop = subprocess.run(["python", "container.py", "stop", profile] + suffix_args, **run_kwargs) # verify that the container has stopped
self.assertEqual(docker_stop.returncode, 0) docker_running_false = subprocess.run(["docker", "ps"], **run_kwargs)
assert docker_running_false.returncode == 0
# verify that the container has stopped assert container_name not in docker_running_false.stdout
docker_running_false = subprocess.run(["docker", "ps"], **run_kwargs)
self.assertEqual(docker_running_false.returncode, 0)
self.assertNotIn(container_name, docker_running_false.stdout) @pytest.mark.parametrize(
"profile,suffix",
def test_docker_base(self): [
"""Test starting and stopping docker base.""" ("base", ""),
self.start_stop_docker("base", "") ("base", "test"),
("ros2", ""),
def test_docker_base_suffix(self): ("ros2", "test"),
"""Test starting and stopping docker base with a test suffix.""" ],
self.start_stop_docker("base", "test") )
def test_docker_profiles(profile, suffix):
def test_docker_ros2(self): """Test starting and stopping docker profiles with and without suffixes."""
"""Test starting and stopping docker ros2.""" start_stop_docker(profile, suffix)
self.start_stop_docker("ros2", "")
def test_docker_ros2_suffix(self):
"""Test starting and stopping docker ros2 with a test suffix."""
self.start_stop_docker("ros2", "test")
if __name__ == "__main__":
unittest.main(verbosity=2, exit=True)
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# #
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
""" """
This script checks the functionality of scale randomization. This script checks the functionality of scale randomization.
""" """
...@@ -20,9 +21,9 @@ simulation_app = app_launcher.app ...@@ -20,9 +21,9 @@ simulation_app = app_launcher.app
"""Rest everything follows.""" """Rest everything follows."""
import torch import torch
import unittest
import omni.usd import omni.usd
import pytest
from pxr import Sdf from pxr import Sdf
import isaaclab.envs.mdp as mdp import isaaclab.envs.mdp as mdp
...@@ -278,80 +279,73 @@ class CubeEnvCfg(ManagerBasedEnvCfg): ...@@ -278,80 +279,73 @@ class CubeEnvCfg(ManagerBasedEnvCfg):
self.sim.render_interval = self.decimation self.sim.render_interval = self.decimation
class TestScaleRandomization(unittest.TestCase): @pytest.mark.parametrize("device", ["cpu", "cuda"])
"""Test for scale randomization.""" def test_scale_randomization(device):
"""Test scale randomization for cube environment."""
""" # create a new stage
Tests omni.usd.get_context().new_stage()
"""
# set the device
def test_scale_randomization(self): env_cfg = CubeEnvCfg()
"""Test scale randomization for cube environment.""" env_cfg.sim.device = device
for device in ["cpu", "cuda"]:
with self.subTest(device=device): # setup base environment
# create a new stage env = ManagerBasedEnv(cfg=env_cfg)
omni.usd.get_context().new_stage() # setup target position commands
target_position = torch.rand(env.num_envs, 3, device=env.device) * 2
# set the device target_position[:, 2] += 2.0
env_cfg = CubeEnvCfg() # offset all targets so that they move to the world origin
env_cfg.sim.device = device target_position -= env.scene.env_origins
# setup base environment # test to make sure all assets in the scene are created
env = ManagerBasedEnv(cfg=env_cfg) all_prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube.*/.*")
# setup target position commands assert len(all_prim_paths) == (env.num_envs * 2)
target_position = torch.rand(env.num_envs, 3, device=env.device) * 2
target_position[:, 2] += 2.0 # test to make sure randomized values are truly random
# offset all targets so that they move to the world origin applied_scaling_randomization = set()
target_position -= env.scene.env_origins prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube1")
# test to make sure all assets in the scene are created # get the stage
all_prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube.*/.*") stage = omni.usd.get_context().get_stage()
self.assertEqual(len(all_prim_paths), (env.num_envs * 2))
# check if the scale values are truly random
# test to make sure randomized values are truly random for i in range(3):
applied_scaling_randomization = set() prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube1") scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
if scale_spec.default in applied_scaling_randomization:
# get the stage raise ValueError(
stage = omni.usd.get_context().get_stage() "Detected repeat in applied scale values - indication scaling randomization is not working."
)
# check if the scale values are truly random applied_scaling_randomization.add(scale_spec.default)
for i in range(3):
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i]) # test to make sure that fixed values are assigned correctly
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale") prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube2")
if scale_spec.default in applied_scaling_randomization: for i in range(3):
raise ValueError( prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
"Detected repeat in applied scale values - indication scaling randomization is not working." scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
) assert tuple(scale_spec.default) == (1.0, 1.0, 1.0)
applied_scaling_randomization.add(scale_spec.default)
# simulate physics
# test to make sure that fixed values are assigned correctly with torch.inference_mode():
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube2") for count in range(200):
for i in range(3): # reset every few steps to check nothing breaks
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i]) if count % 100 == 0:
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale") env.reset()
self.assertEqual(tuple(scale_spec.default), (1.0, 1.0, 1.0)) # step the environment
env.step(target_position)
# simulate physics
with torch.inference_mode(): env.close()
for count in range(200):
# reset every few steps to check nothing breaks
if count % 100 == 0: def test_scale_randomization_failure_replicate_physics():
env.reset() """Test scale randomization failure when replicate physics is set to True."""
# step the environment # create a new stage
env.step(target_position) omni.usd.get_context().new_stage()
# set the arguments
env.close() cfg_failure = CubeEnvCfg()
cfg_failure.scene.replicate_physics = True
def test_scale_randomization_failure_replicate_physics(self):
"""Test scale randomization failure when replicate physics is set to True.""" # run the test
# create a new stage with pytest.raises(RuntimeError):
omni.usd.get_context().new_stage() env = ManagerBasedEnv(cfg_failure)
# set the arguments env.close()
cfg_failure = CubeEnvCfg()
cfg_failure.scene.replicate_physics = True
# run the test
with self.assertRaises(RuntimeError):
env = ManagerBasedEnv(cfg_failure)
env.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment