Skip to content

Commit 85364f0

Browse files
committed
Update tests for Pytest support
1 parent 3a91c14 commit 85364f0

File tree

5 files changed

+122
-70
lines changed

5 files changed

+122
-70
lines changed

scripts/ci/run-tests

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def process_args(args):
3434
test_args = ""
3535
if args.with_cov:
3636
test_args += (
37-
f"--with-xunit --cover-erase --with-coverage "
38-
f"--cover-package {PACKAGE} --cover-xml -v "
37+
f"--cov={PACKAGE} --cov-report xml -v "
3938
)
4039
dirs = " ".join(args.test_dirs)
4140

@@ -53,8 +52,8 @@ if __name__ == "__main__":
5352
parser.add_argument(
5453
"-r",
5554
"--test-runner",
56-
default="nosetests",
57-
help="Test runner to execute tests. Defaults to nose.",
55+
default="pytest",
56+
help="Test runner to execute tests. Defaults to pytest.",
5857
)
5958
parser.add_argument(
6059
"-c",

tests/functional/docs/test_smoke.py

+58-32
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,71 @@
1919
from boto3.docs.service import ServiceDocumenter
2020

2121

22-
def test_docs_generated():
23-
"""Verify we can generate the appropriate docs for all services"""
22+
@pytest.fixture
23+
def botocore_session():
24+
return botocore.session.get_session()
25+
26+
@pytest.fixture
27+
def boto3_session():
28+
return boto3.Session(region_name='us-east-1')
29+
30+
def all_services():
2431
botocore_session = botocore.session.get_session()
2532
session = boto3.Session(region_name='us-east-1')
2633
for service_name in session.get_available_services():
27-
generated_docs = ServiceDocumenter(
28-
service_name, session=session).document_service()
29-
generated_docs = generated_docs.decode('utf-8')
30-
client = boto3.client(service_name, 'us-east-1')
34+
yield service_name
35+
36+
37+
@pytest.fixture
38+
def available_resources():
39+
session = boto3.Session(region_name='us-east-1')
40+
return session.get_available_resources()
41+
42+
43+
@pytest.mark.parametrize('service_name', all_services())
44+
def test_documentation(
45+
boto3_session, botocore_session, available_resources, service_name
46+
):
47+
generated_docs = ServiceDocumenter(
48+
service_name, session=boto3_session).document_service()
49+
generated_docs = generated_docs.decode('utf-8')
50+
client = boto3.client(service_name, 'us-east-1')
51+
52+
# Check that all of the services have the appropriate title
53+
_assert_has_title(generated_docs, client)
54+
55+
56+
# Check that all services have the client documented.
57+
_assert_has_client_documentation(generated_docs, service_name, client)
3158

32-
# Check that all of the services have the appropriate title
33-
yield (_assert_has_title, generated_docs, client)
3459

35-
# Check that all services have the client documented.
36-
yield (_assert_has_client_documentation, generated_docs, service_name,
37-
client)
60+
#If the service has resources, make sure the service resource
61+
#is at least documented.
62+
if service_name in available_resources:
63+
64+
resource = boto3.resource(service_name, 'us-east-1')
65+
_assert_has_resource_documentation(
66+
generated_docs, service_name, resource
67+
)
3868

39-
# If the client can paginate, make sure the paginators are documented.
40-
try:
41-
paginator_model = botocore_session.get_paginator_model(
69+
# If the client can paginate, make sure the paginators are documented.
70+
try:
71+
paginator_model = botocore_session.get_paginator_model(
4272
service_name)
43-
yield (_assert_has_paginator_documentation, generated_docs,
44-
service_name, client,
45-
sorted(paginator_model._paginator_config))
46-
except DataNotFoundError:
47-
pass
48-
49-
# If the client has waiters, make sure the waiters are documented
50-
if client.waiter_names:
51-
waiter_model = botocore_session.get_waiter_model(service_name)
52-
yield (_assert_has_waiter_documentation, generated_docs,
53-
service_name, client, waiter_model)
54-
55-
# If the service has resources, make sure the service resource
56-
# is at least documented.
57-
if service_name in session.get_available_resources():
58-
resource = boto3.resource(service_name, 'us-east-1')
59-
yield (_assert_has_resource_documentation, generated_docs,
60-
service_name, resource)
73+
_assert_has_paginator_documentation(
74+
generated_docs, service_name, client,
75+
sorted(paginator_model._paginator_config)
76+
)
77+
except DataNotFoundError:
78+
pass
79+
80+
81+
# If the client has waiters, make sure the waiters are documented.
82+
if client.waiter_names:
83+
waiter_model = botocore_session.get_waiter_model(service_name)
84+
_assert_has_waiter_documentation(
85+
generated_docs, service_name, client, waiter_model
86+
)
6187

6288

6389
def _assert_contains_lines_in_order(lines, contents):

tests/functional/test_smoke.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,29 @@ def create_session():
2323
return session
2424

2525

26-
def test_can_create_all_resources():
27-
"""Verify we can create all existing resources."""
26+
def _all_resources():
2827
session = create_session()
2928
for service_name in session.get_available_resources():
30-
yield _test_create_resource, session, service_name
29+
yield session, service_name
30+
31+
32+
def _all_clients():
33+
session = create_session()
34+
for service_name in session.get_available_services():
35+
yield session, service_name
36+
37+
38+
def _all_api_version_args():
39+
botocore_session = botocore.session.get_session()
40+
boto3_session = create_session()
41+
for service_name in boto3_session.get_available_resources():
42+
yield (service_name, botocore_session, boto3_session)
43+
44+
45+
@pytest.mark.parametrize('resource_args', _all_resources())
46+
def test_can_create_all_resources(all_resources):
47+
"""Verify we can create all existing resources."""
48+
_test_create_resource(*resource_args)
3149

3250

3351
def _test_create_resource(session, service_name):
@@ -37,23 +55,21 @@ def _test_create_resource(session, service_name):
3755
assert hasattr(resource, 'meta')
3856

3957

40-
def test_can_create_all_clients():
41-
session = create_session()
42-
for service_name in session.get_available_services():
43-
yield _test_create_client, session, service_name
58+
@pytest.mark.parametrize('client_args', _all_clients())
59+
def test_can_create_all_resources(client_args):
60+
"""Verify we can create all existing clients."""
61+
_test_create_client(*client_args)
4462

4563

4664
def _test_create_client(session, service_name):
4765
client = session.client(service_name)
4866
assert hasattr(client, 'meta')
4967

5068

51-
def test_api_versions_synced_with_botocore():
52-
botocore_session = botocore.session.get_session()
53-
boto3_session = create_session()
54-
for service_name in boto3_session.get_available_resources():
55-
yield (_assert_same_api_versions, service_name,
56-
botocore_session, boto3_session)
69+
@pytest.mark.parametrize('api_version_args', _all_api_version_args())
70+
def test_api_versions_synced_with_botocore(api_version_args):
71+
"""Verify both boto3 and botocore clients stay in sync."""
72+
_assert_same_api_versions(*api_version_args)
5773

5874

5975
def _assert_same_api_versions(service_name, botocore_session, boto3_session):

tests/integration/test_collections.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313

14-
import boto3.session
14+
import pytest
1515

16+
import boto3.session
1617
from boto3.resources.collection import CollectionManager
18+
from boto3.resources.base import ServiceResource
1719

1820

1921
# A map of services to regions that cannot use us-west-2
@@ -24,33 +26,37 @@
2426

2527
# A list of collections to ignore. They require parameters
2628
# or are very slow to run.
27-
BLACKLIST = {
29+
BLOCKLIST = {
2830
'ec2': ['images'],
2931
'iam': ['signing_certificates'],
3032
'sqs': ['dead_letter_source_queues']
3133
}
3234

3335

34-
def test_all_collections():
35-
# This generator yields test functions for every collection
36-
# on every available resource, except those which have
37-
# been blacklisted.
36+
def all_collections():
37+
# This generator yields every collection on every available resource,
38+
# except those which have been blocklisted.
3839
session = boto3.session.Session()
3940
for service_name in session.get_available_resources():
4041
resource = session.resource(
4142
service_name,
4243
region_name=REGION_MAP.get(service_name, 'us-west-2'))
4344

4445
for key in dir(resource):
45-
if key in BLACKLIST.get(service_name, []):
46+
if key in BLOCKLIST.get(service_name, []):
4647
continue
4748

4849
value = getattr(resource, key)
4950
if isinstance(value, CollectionManager):
50-
yield _test_collection, service_name, key, value
51+
yield value
52+
5153

52-
def _test_collection(service_name, collection_name, collection):
54+
@pytest.mark.parametrize("collection", all_collections())
55+
def test_all_collections(collection):
56+
"""Test all collections work on every available resource."""
5357
# Create a list of the first page of items. This tests that
5458
# a remote request can be made, the response parsed, and that
5559
# resources are successfully created.
56-
list(collection.limit(1))
60+
collection_list = list(collection.limit(1))
61+
assert len(collection_list) < 2
62+
assert all([isinstance(res, ServiceResource) for res in collection_list])

tests/unit/resources/test_collection_smoke.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ def _shape_has_pagination_param(shape):
6363
return False
6464

6565

66-
def test_all_collections_have_paginators_if_needed():
67-
# If a collection relies on an operation that is paginated, it
68-
# will require a paginator to iterate through all of the resources
69-
# with the all() method. If there is no paginator, it will only
70-
# make it through the first page of results. So we need to make sure
71-
# if a collection looks like it uses a paginated operation then there
72-
# should be a paginator applied to it.
66+
def _collection_test_args():
7367
botocore_session = botocore.session.get_session()
7468
session = Session(botocore_session=botocore_session)
7569
loader = botocore_session.get_component('data_loader')
@@ -91,13 +85,24 @@ def test_all_collections_have_paginators_if_needed():
9185
# Iterate over all of the collections for each resource model
9286
# and ensure that the collection has a paginator if it needs one.
9387
for collection_model in resource_model.collections:
94-
yield (
95-
_assert_collection_has_paginator_if_needed, client,
96-
service_name, resource_name, collection_model)
88+
yield (client, service_name, resource_name, collection_model)
9789

90+
@pytest.mark.parametrize(
91+
'collection_args',
92+
_collection_test_args()
93+
)
94+
def test_all_collections_have_paginators_if_needed(collection_args):
95+
# If a collection relies on an operation that is paginated, it
96+
# will require a paginator to iterate through all of the resources
97+
# with the all() method. If there is no paginator, it will only
98+
# make it through the first page of results. So we need to make sure
99+
# if a collection looks like it uses a paginated operation then there
100+
# should be a paginator applied to it.
101+
_assert_collection_has_paginator_if_needed(*collection_args)
98102

99103
def _assert_collection_has_paginator_if_needed(
100-
client, service_name, resource_name, collection_model):
104+
client, service_name, resource_name, collection_model
105+
):
101106
underlying_operation_name = collection_model.request.operation
102107
# See if the operation can be paginated from the client.
103108
can_paginate_operation = client.can_paginate(

0 commit comments

Comments
 (0)