Skip to content

test_script_module

Hosted unit tests for the pyrevit.script module.

Run from Revit via the DevTools "Script Module Tests" button (zero-doc context) or the "Run All Tests" aggregator. The tests assert the command-runtime contract: logger/output access, bundle file resolution, data file helpers, and environment variable round trips.

Classes

ScriptInfoTests

Bases: TestCase

Command metadata exposed to running scripts.

Methods:

setUp()

Resolve the running command info object.

Command resolution matches bundle directories against the known command subclasses, so the components module must be imported first; in engines that never loaded it, subclasses() is empty and get_info() resolves to None.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def setUp(self):
    """Resolve the running command info object.

    Command resolution matches bundle directories against the known
    command subclasses, so the components module must be imported
    first; in engines that never loaded it, __subclasses__() is empty
    and get_info() resolves to None.
    """
    import pyrevit.extensions.components  # noqa: F401 pylint: disable=unused-import
    from pyrevit import script

    self.info = script.get_info()
test_info_available()

Command info resolves for the running command.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_info_available(self):
    """Command info resolves for the running command."""
    self.assertIsNotNone(self.info)
    self.assertTrue(self.info.name)
test_unique_name_format()

Unique command name is lowercase with no whitespace.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_unique_name_format(self):
    """Unique command name is lowercase with no whitespace."""
    unique_name = self.info.unique_name
    self.assertTrue(unique_name)
    self.assertEqual(unique_name.lower(), unique_name)
    self.assertNotIn(" ", unique_name)
test_script_file_exists()

Resolved script file exists inside the bundle.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_script_file_exists(self):
    """Resolved script file exists inside the bundle."""
    self.assertIsNotNone(self.info.script_file)
    self.assertTrue(os.path.isfile(self.info.script_file))

ScriptLoggerTests

Bases: TestCase

Logger wrapper exposes the extended level methods.

Methods:

setUp()

Grab the pyRevit logger wrapper.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def setUp(self):
    """Grab the pyRevit logger wrapper."""
    from pyrevit import script

    self.logger = script.get_logger()
test_standard_levels()

Standard logging levels are callable.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_standard_levels(self):
    """Standard logging levels are callable."""
    for level in ("debug", "info", "warning", "error", "critical"):
        self.assertTrue(callable(getattr(self.logger, level)))
test_extended_levels()

pyRevit-specific levels are callable.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_extended_levels(self):
    """pyRevit-specific levels are callable."""
    for level in ("success", "deprecate", "dev_log"):
        self.assertTrue(callable(getattr(self.logger, level)))

BundleFileTests

Bases: TestCase

Bundle resource resolution relative to the command bundle.

Methods:

setUp()

Grab the script module helpers.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def setUp(self):
    """Grab the script module helpers."""
    from pyrevit import script

    self.script = script
test_get_bundle_file_missing_returns_nonexistent_path()

Missing resources resolve to a joined path that does not exist.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_get_bundle_file_missing_returns_nonexistent_path(self):
    """Missing resources resolve to a joined path that does not exist."""
    filepath = self.script.get_bundle_file("definitely_missing_resource.xyz")
    self.assertIsInstance(filepath, str)
    self.assertFalse(os.path.isfile(filepath))
test_get_bundle_files_missing_raises()

Missing sub-paths raise an OS error from the directory listing.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_get_bundle_files_missing_raises(self):
    """Missing sub-paths raise an OS error from the directory listing."""
    self.assertRaises(
        EnvironmentError,
        self.script.get_bundle_files,
        "definitely_missing_folder/",
    )
test_get_bundle_file_existing()

Bundle metadata file resolves to an existing path.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_get_bundle_file_existing(self):
    """Bundle metadata file resolves to an existing path."""
    filepath = self.script.get_bundle_file("bundle.yaml")
    self.assertIsNotNone(filepath)
    self.assertTrue(os.path.isfile(filepath))
test_bundle_directory_exists()

The resolved bundle directory exists on disk.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_bundle_directory_exists(self):
    """The resolved bundle directory exists on disk."""
    bundle_dir = os.path.dirname(os.path.abspath(__file__))
    self.assertTrue(os.path.isdir(bundle_dir))

DataFileTests

Bases: TestCase

Data file helpers create, read, and remove slot files.

Methods:

setUp()

Grab the script module helpers and reset the probe path.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def setUp(self):
    """Grab the script module helpers and reset the probe path."""
    from pyrevit import script

    self.script = script
    self.filepath = None
tearDown()

Remove any data file created by the test.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def tearDown(self):
    """Remove any data file created by the test."""
    if self.filepath and os.path.isfile(self.filepath):
        self.script.remove_data_file(self.filepath)
test_instance_data_file_round_trip()

Instance data files persist content between calls in a session.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_instance_data_file_round_trip(self):
    """Instance data files persist content between calls in a session."""
    filepath = self.script.get_instance_data_file("unittest_probe")
    self.filepath = filepath
    with open(filepath, "w") as data_file:
        data_file.write("pyrevit")

    self.assertTrue(os.path.isfile(filepath))
    with open(filepath, "r") as data_file:
        self.assertEqual("pyrevit", data_file.read())

EnvVarTests

Bases: TestCase

Environment variable helpers round trip values.

Methods:

test_set_get_envvar()

Stored env var values are returned by the getter.

Source code in pyrevitlib/pyrevit/unittests/test_script_module.py
def test_set_get_envvar(self):
    """Stored env var values are returned by the getter."""
    from pyrevit import script

    script.set_envvar("PYREVIT_UNITTEST_PROBE", "probe_value")
    self.assertEqual("probe_value", script.get_envvar("PYREVIT_UNITTEST_PROBE"))