Skip to content

Commit

Permalink
Add decorator to skip tests and log a warning if URL is unaccessible (#…
Browse files Browse the repository at this point in the history
…2104) (#2163)

* Add decorator to skip tests and log a warning if URL is unaccessible

* More descriptive error message

Co-authored-by: Joe Cummings <jrcummings@fb.com>
  • Loading branch information
Nayef211 and joecummings authored Apr 26, 2023
1 parent 5735a72 commit 9a9c181
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions test/torchtext_unittest/common/torchtext_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
import shutil
import subprocess
import tempfile
from urllib.error import HTTPError

import torch # noqa: F401
from torch.testing._internal.common_utils import TestCase

logger = logging.getLogger(__name__)


def third_party_download(test_func):
def inner(*args, **kwargs):
try:
return test_func(*args, **kwargs)
except HTTPError as e:
logger.warning(f"Cannot access URL in {test_func.__name__}. Error message {e}")

return inner


class TorchtextTestCase(TestCase):
def setUp(self) -> None:
logging.basicConfig(format=("%(asctime)s - %(levelname)s - " "%(name)s - %(message)s"), level=logging.INFO)
Expand Down
3 changes: 2 additions & 1 deletion test/torchtext_unittest/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import torch
import torchtext.data

from .common.torchtext_test_case import TorchtextTestCase
from .common.torchtext_test_case import TorchtextTestCase, third_party_download


class TestDataUtils(TorchtextTestCase):
Expand Down Expand Up @@ -64,6 +64,7 @@ def test_vectors_get_vecs(self) -> None:
self.assertEqual(token_one_vec.shape[0], vec.dim)
self.assertEqual(vec[tokens[0].lower()], token_one_vec)

@third_party_download
def test_download_charngram_vectors(self) -> None:
# Build a vocab and get vectors twice to test caching.
for _ in range(2):
Expand Down

0 comments on commit 9a9c181

Please sign in to comment.