Skip to content

Commit c6d4a62

Browse files
authored
test: use unittest.mock (#1320)
* test: use unittest.mock * test: use unittest.mock * install asyncmock for python_version < '3.8' * fix showcase alternative templates test * fix fragment tests
1 parent dacbd40 commit c6d4a62

20 files changed

Lines changed: 67 additions & 24 deletions

File tree

packages/gapic-generator/gapic/ads-templates/noxfile.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ALL_PYTHON = [
1818
def unit(session):
1919
"""Run the unit test suite."""
2020

21-
session.install('coverage', 'mock', 'pytest', 'pytest-cov')
21+
session.install('coverage', 'pytest', 'pytest-cov')
2222
session.install('-e', '.')
2323

2424
session.run(

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
{% block content %}
44

55
import os
6-
import mock
6+
# try/except added for compatibility with python < 3.8
7+
try:
8+
from unittest import mock
9+
from unittest.mock import AsyncMock
10+
except ImportError:
11+
import mock
712

813
import grpc
914
from grpc.experimental import aio

packages/gapic-generator/gapic/templates/noxfile.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ nox.sessions = [
4444
def unit(session):
4545
"""Run the unit test suite."""
4646

47-
session.install('coverage', 'pytest', 'pytest-cov', 'asyncmock', 'pytest-asyncio')
47+
session.install('coverage', 'pytest', 'pytest-cov', 'pytest-asyncio', 'asyncmock; python_version < "3.8"')
4848
session.install('-e', '.')
4949

5050
session.run(

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
{% import "tests/unit/gapic/%name_%version/%sub/test_macros.j2" as test_macros %}
55

66
import os
7-
import mock
7+
# try/except added for compatibility with python < 3.8
8+
try:
9+
from unittest import mock
10+
from unittest.mock import AsyncMock
11+
except ImportError:
12+
import mock
813

914
import grpc
1015
from grpc.experimental import aio

packages/gapic-generator/noxfile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def fragment(session, use_ads_templates=False):
138138
"pytest",
139139
"pytest-cov",
140140
"pytest-xdist",
141-
"asyncmock",
141+
"asyncmock; python_version < '3.8'",
142142
"pytest-asyncio",
143143
"grpcio-tools",
144144
)
@@ -246,7 +246,7 @@ def showcase(
246246
"""Run the Showcase test suite."""
247247

248248
with showcase_library(session, templates=templates, other_opts=other_opts):
249-
session.install("mock", "pytest", "pytest-asyncio")
249+
session.install("pytest", "pytest-asyncio")
250250
session.run(
251251
"py.test",
252252
"--quiet",
@@ -265,7 +265,7 @@ def showcase_mtls(
265265
"""Run the Showcase mtls test suite."""
266266

267267
with showcase_library(session, templates=templates, other_opts=other_opts):
268-
session.install("mock", "pytest", "pytest-asyncio")
268+
session.install("pytest", "pytest-asyncio")
269269
session.run(
270270
"py.test",
271271
"--quiet",
@@ -303,7 +303,7 @@ def run_showcase_unit_tests(session, fail_under=100):
303303
"pytest",
304304
"pytest-cov",
305305
"pytest-xdist",
306-
"asyncmock",
306+
"asyncmock; python_version < '3.8'",
307307
"pytest-asyncio",
308308
)
309309

@@ -417,7 +417,7 @@ def snippetgen(session):
417417
# Install gapic-generator-python
418418
session.install("-e", ".")
419419

420-
session.install("grpcio-tools", "mock", "pytest", "pytest-asyncio")
420+
session.install("grpcio-tools", "pytest", "pytest-asyncio")
421421

422422
session.run("py.test", "-vv", "tests/snippetgen")
423423

packages/gapic-generator/tests/integration/goldens/asset/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
def unit(session):
5656
"""Run the unit test suite."""
5757

58-
session.install('coverage', 'pytest', 'pytest-cov', 'asyncmock', 'pytest-asyncio')
58+
session.install('coverage', 'pytest', 'pytest-cov', 'pytest-asyncio', 'asyncmock; python_version < "3.8"')
5959
session.install('-e', '.')
6060

6161
session.run(

packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
# limitations under the License.
1515
#
1616
import os
17-
import mock
17+
# try/except added for compatibility with python < 3.8
18+
try:
19+
from unittest import mock
20+
from unittest.mock import AsyncMock
21+
except ImportError:
22+
import mock
1823

1924
import grpc
2025
from grpc.experimental import aio

packages/gapic-generator/tests/integration/goldens/credentials/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
def unit(session):
5656
"""Run the unit test suite."""
5757

58-
session.install('coverage', 'pytest', 'pytest-cov', 'asyncmock', 'pytest-asyncio')
58+
session.install('coverage', 'pytest', 'pytest-cov', 'pytest-asyncio', 'asyncmock; python_version < "3.8"')
5959
session.install('-e', '.')
6060

6161
session.run(

packages/gapic-generator/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
# limitations under the License.
1515
#
1616
import os
17-
import mock
17+
# try/except added for compatibility with python < 3.8
18+
try:
19+
from unittest import mock
20+
from unittest.mock import AsyncMock
21+
except ImportError:
22+
import mock
1823

1924
import grpc
2025
from grpc.experimental import aio

packages/gapic-generator/tests/integration/goldens/eventarc/noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
def unit(session):
5656
"""Run the unit test suite."""
5757

58-
session.install('coverage', 'pytest', 'pytest-cov', 'asyncmock', 'pytest-asyncio')
58+
session.install('coverage', 'pytest', 'pytest-cov', 'pytest-asyncio', 'asyncmock; python_version < "3.8"')
5959
session.install('-e', '.')
6060

6161
session.run(

0 commit comments

Comments
 (0)