Skip to content

Commit 09cf285

Browse files
authored
tests: add testing w/o 'grpc' installed (googleapis#289)
Closes googleapis#288.
1 parent f4e776e commit 09cf285

16 files changed

+124
-12
lines changed

noxfile.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
nox.options.sessions = [
3434
"unit",
3535
"unit_grpc_gcp",
36+
"unit_wo_grpc",
3637
"cover",
3738
"pytype",
3839
"mypy",
@@ -78,7 +79,7 @@ def blacken(session):
7879
session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS)
7980

8081

81-
def default(session):
82+
def default(session, install_grpc=True):
8283
"""Default unit test session.
8384
8485
This is intended to be run **without** an interpreter set, so
@@ -92,7 +93,10 @@ def default(session):
9293

9394
# Install all test dependencies, then install this package in-place.
9495
session.install("mock", "pytest", "pytest-cov")
95-
session.install("-e", ".[grpc]", "-c", constraints_path)
96+
if install_grpc:
97+
session.install("-e", ".[grpc]", "-c", constraints_path)
98+
else:
99+
session.install("-e", ".", "-c", constraints_path)
96100

97101
pytest_args = [
98102
"python",
@@ -140,6 +144,12 @@ def unit_grpc_gcp(session):
140144
default(session)
141145

142146

147+
@nox.session(python=["3.6", "3.10"])
148+
def unit_wo_grpc(session):
149+
"""Run the unit test suite w/o grpcio installed"""
150+
default(session, install_grpc=False)
151+
152+
143153
@nox.session(python="3.6")
144154
def lint_setup_py(session):
145155
"""Verify that setup.py is valid (including RST check)."""

tests/asyncio/gapic/test_config_async.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
16+
import pytest
17+
18+
try:
19+
import grpc # noqa: F401
20+
except ImportError:
21+
pytest.skip("No GRPC", allow_module_level=True)
22+
1523
from google.api_core import exceptions
1624
from google.api_core.gapic_v1 import config_async
1725

tests/asyncio/gapic/test_method_async.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
import datetime
1616

17-
from grpc import aio
1817
import mock
1918
import pytest
2019

20+
try:
21+
from grpc import aio
22+
except ImportError:
23+
pytest.skip("No GRPC", allow_module_level=True)
24+
2125
from google.api_core import exceptions
2226
from google.api_core import gapic_v1
2327
from google.api_core import grpc_helpers_async

tests/asyncio/operations_v1/test_operations_async_client.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from grpc import aio
1615
import mock
1716
import pytest
1817

19-
from google.api_core import grpc_helpers_async, operations_v1, page_iterator_async
18+
try:
19+
from grpc import aio
20+
except ImportError:
21+
pytest.skip("No GRPC", allow_module_level=True)
22+
23+
from google.api_core import grpc_helpers_async
24+
from google.api_core import operations_v1
25+
from google.api_core import page_iterator_async
2026
from google.longrunning import operations_pb2
2127
from google.protobuf import empty_pb2
2228

tests/asyncio/test_grpc_helpers_async.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import grpc
16-
from grpc import aio
1715
import mock
18-
import pytest
16+
import pytest # noqa: I202
17+
18+
try:
19+
import grpc
20+
from grpc import aio
21+
except ImportError:
22+
grpc = aio = None
23+
24+
25+
if grpc is None:
26+
pytest.skip("No GRPC", allow_module_level=True)
27+
1928

2029
from google.api_core import exceptions
2130
from google.api_core import grpc_helpers_async

tests/asyncio/test_operation_async.py

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import mock
1717
import pytest
1818

19+
try:
20+
import grpc # noqa: F401
21+
except ImportError:
22+
pytest.skip("No GRPC", allow_module_level=True)
23+
1924
from google.api_core import exceptions
2025
from google.api_core import operation_async
2126
from google.api_core import operations_v1

tests/unit/gapic/test_client_info.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
17+
try:
18+
import grpc # noqa: F401
19+
except ImportError:
20+
pytest.skip("No GRPC", allow_module_level=True)
21+
1522

1623
from google.api_core.gapic_v1 import client_info
1724

tests/unit/gapic/test_config.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
17+
try:
18+
import grpc # noqa: F401
19+
except ImportError:
20+
pytest.skip("No GRPC", allow_module_level=True)
21+
1522
from google.api_core import exceptions
1623
from google.api_core.gapic_v1 import config
1724

tests/unit/gapic/test_method.py

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
import datetime
1616

1717
import mock
18+
import pytest
19+
20+
try:
21+
import grpc # noqa: F401
22+
except ImportError:
23+
pytest.skip("No GRPC", allow_module_level=True)
24+
1825

1926
from google.api_core import exceptions
2027
from google.api_core import retry

tests/unit/gapic/test_routing_header.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
17+
try:
18+
import grpc # noqa: F401
19+
except ImportError:
20+
pytest.skip("No GRPC", allow_module_level=True)
21+
1522

1623
from google.api_core.gapic_v1 import routing_header
1724

tests/unit/operations_v1/test_operations_client.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pytest
16+
17+
try:
18+
import grpc # noqa: F401
19+
except ImportError:
20+
pytest.skip("No GRPC", allow_module_level=True)
21+
1522
from google.api_core import grpc_helpers
1623
from google.api_core import operations_v1
1724
from google.api_core import page_iterator

tests/unit/test_bidi.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
import queue
1818
import threading
1919

20-
import grpc
2120
import mock
2221
import pytest
2322

23+
try:
24+
import grpc
25+
except ImportError:
26+
pytest.skip("No GRPC", allow_module_level=True)
27+
2428
from google.api_core import bidi
2529
from google.api_core import exceptions
2630

tests/unit/test_client_info.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@
1313
# limitations under the License.
1414

1515

16+
try:
17+
import grpc
18+
except ImportError:
19+
grpc = None
20+
1621
from google.api_core import client_info
1722

1823

1924
def test_constructor_defaults():
2025
info = client_info.ClientInfo()
2126

2227
assert info.python_version is not None
23-
assert info.grpc_version is not None
28+
29+
if grpc is not None:
30+
assert info.grpc_version is not None
31+
else:
32+
assert info.grpc_version is None
33+
2434
assert info.api_core_version is not None
2535
assert info.gapic_version is None
2636
assert info.client_library_version is None

tests/unit/test_exceptions.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
import http.client
1616
import json
1717

18-
import grpc
1918
import mock
19+
import pytest
2020
import requests
2121

22+
try:
23+
import grpc
24+
except ImportError:
25+
grpc = None
26+
2227
from google.api_core import exceptions
2328

2429

@@ -151,6 +156,7 @@ def test_from_http_response_json_unicode_content():
151156
assert exception.errors == ["1", "2"]
152157

153158

159+
@pytest.mark.skipif(grpc is None, reason="No grpc")
154160
def test_from_grpc_status():
155161
message = "message"
156162
exception = exceptions.from_grpc_status(grpc.StatusCode.OUT_OF_RANGE, message)
@@ -162,6 +168,7 @@ def test_from_grpc_status():
162168
assert exception.errors == []
163169

164170

171+
@pytest.mark.skipif(grpc is None, reason="No grpc")
165172
def test_from_grpc_status_as_int():
166173
message = "message"
167174
exception = exceptions.from_grpc_status(11, message)
@@ -173,6 +180,7 @@ def test_from_grpc_status_as_int():
173180
assert exception.errors == []
174181

175182

183+
@pytest.mark.skipif(grpc is None, reason="No grpc")
176184
def test_from_grpc_status_with_errors_and_response():
177185
message = "message"
178186
response = mock.sentinel.response
@@ -187,13 +195,15 @@ def test_from_grpc_status_with_errors_and_response():
187195
assert exception.response == response
188196

189197

198+
@pytest.mark.skipif(grpc is None, reason="No grpc")
190199
def test_from_grpc_status_unknown_code():
191200
message = "message"
192201
exception = exceptions.from_grpc_status(grpc.StatusCode.OK, message)
193202
assert exception.grpc_status_code == grpc.StatusCode.OK
194203
assert exception.message == message
195204

196205

206+
@pytest.mark.skipif(grpc is None, reason="No grpc")
197207
def test_from_grpc_error():
198208
message = "message"
199209
error = mock.create_autospec(grpc.Call, instance=True)
@@ -211,6 +221,7 @@ def test_from_grpc_error():
211221
assert exception.response == error
212222

213223

224+
@pytest.mark.skipif(grpc is None, reason="No grpc")
214225
def test_from_grpc_error_non_call():
215226
message = "message"
216227
error = mock.create_autospec(grpc.RpcError, instance=True)

tests/unit/test_grpc_helpers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import grpc
1615
import mock
1716
import pytest
1817

18+
try:
19+
import grpc
20+
except ImportError:
21+
pytest.skip("No GRPC", allow_module_level=True)
22+
1923
from google.api_core import exceptions
2024
from google.api_core import grpc_helpers
2125
import google.auth.credentials

tests/unit/test_operation.py

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515

1616
import mock
17+
import pytest
18+
19+
try:
20+
import grpc # noqa: F401
21+
except ImportError:
22+
pytest.skip("No GRPC", allow_module_level=True)
1723

1824
from google.api_core import exceptions
1925
from google.api_core import operation

0 commit comments

Comments
 (0)