Add __main__ guards to jit tests (#154725)

This PR is part of a series attempting to re-submit https://github.com/pytorch/pytorch/pull/134592 as smaller PRs.

In jit tests:

- Add and use a common raise_on_run_directly method for when a user runs a test file directly which should not be run this way. Print the file which the user should have run.
- Raise a RuntimeError on tests which have been disabled (not run)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/154725
Approved by: https://github.com/Skylion007
This commit is contained in:
Anthony Barbier
2025-06-04 14:44:04 +00:00
committed by PyTorch MergeBot
parent 3f34d26040
commit 1a55fb0ee8
79 changed files with 456 additions and 518 deletions

View File

@ -13,17 +13,10 @@ from torch.testing import FileCheck
# Make the helper files in test/ importable
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(pytorch_test_dir)
from torch.testing._internal.common_utils import raise_on_run_directly
from torch.testing._internal.jit_utils import JitTestCase, RUN_CUDA
if __name__ == "__main__":
raise RuntimeError(
"This test file is not meant to be run directly, use:\n\n"
"\tpython test/test_jit.py TESTNAME\n\n"
"instead."
)
class TestBuiltins(JitTestCase):
"""
Tests for TorchScript support of Python builtin functions.
@ -299,3 +292,7 @@ class TestTensorBuiltins(JitTestCase):
self.assertEqual(
test_func(script_funs[i], x, tensor), test_func(funs[i], x, tensor)
)
if __name__ == "__main__":
raise_on_run_directly("test/test_jit.py")