Skip to content

Commit

Permalink
Test that TensorFlow is not imported on startup (#904)
Browse files Browse the repository at this point in the history
* test tf not imported on startup

* add require_tf

* better comment

* Update test_import.py

* add pytest ./tests/test_tf*
  • Loading branch information
lhoestq authored Jul 7, 2022
1 parent 9a034ec commit 06fdf94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ jobs:
pip install --upgrade pip
pip install .[testing,tensorflow]
- run: pytest --cov -Werror::FutureWarning -sv ./tests/test_tf*

- run: pytest --cov -Werror::FutureWarning -sv ./tests/test_keras*

- name: Upload coverage reports to Codecov with GitHub Action
Expand Down
26 changes: 26 additions & 0 deletions tests/test_tf_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
import unittest

from huggingface_hub.file_download import is_tf_available


def require_tf(test_case):
"""
Decorator marking a test that requires TensorFlow.
These tests are skipped when TensorFlow is not installed.
"""
if not is_tf_available():
return unittest.skip("test requires Tensorflow")(test_case)
else:
return test_case


@require_tf
def test_import_huggingface_hub_doesnt_import_tensorfow():
# `import huggingface_hub` is not necessary since huggingface_hub is already imported at the top of this file,
# but let's keep it here anyway just in case
import huggingface_hub # noqa

assert "tensorflow" not in sys.modules

0 comments on commit 06fdf94

Please # to comment.