From fbefae59c570f633a57ceb178eb8bd647f7bfdf4 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 5 Feb 2022 10:47:38 -0100 Subject: [PATCH] Non-unittest classes use a lowercase setup-method (#4833) --- moto/core/models.py | 10 +++-- tests/test_core/test_decorator_calls.py | 50 ++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/moto/core/models.py b/moto/core/models.py index 370d6e3c0..3db13879d 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -137,11 +137,15 @@ class BaseMockAWS: c for c in klass.__mro__ if c not in [unittest.TestCase, object] ] # Get a list of all userdefined methods - supermethods = itertools.chain( - *[get_direct_methods_of(c) for c in superclasses] + supermethods = list( + itertools.chain(*[get_direct_methods_of(c) for c in superclasses]) ) # Check whether the user has overridden the setUp-method - has_setup_method = "setUp" in supermethods + has_setup_method = ( + ("setUp" in supermethods and unittest.TestCase in klass.__mro__) + or "setup" in supermethods + or "setup_method" in supermethods + ) for attr in itertools.chain(direct_methods, defined_classes): if attr.startswith("_"): diff --git a/tests/test_core/test_decorator_calls.py b/tests/test_core/test_decorator_calls.py index 8b26f53d7..2f805dcc7 100644 --- a/tests/test_core/test_decorator_calls.py +++ b/tests/test_core/test_decorator_calls.py @@ -109,8 +109,9 @@ class TesterWithStaticmethod(object): @mock_s3 -class TestWithSetup(unittest.TestCase): +class TestWithSetup_UppercaseU(unittest.TestCase): def setUp(self): + # This method will be executed automatically, provided we extend the TestCase-class s3 = boto3.client("s3", region_name="us-east-1") s3.create_bucket(Bucket="mybucket") @@ -124,6 +125,53 @@ class TestWithSetup(unittest.TestCase): s3.head_bucket(Bucket="unknown_bucket") +@mock_s3 +class TestWithSetup_LowercaseU: + def setup(self, *args): + # This method will be executed automatically using pytest + s3 = boto3.client("s3", region_name="us-east-1") + s3.create_bucket(Bucket="mybucket") + + def test_should_find_bucket(self): + s3 = boto3.client("s3", region_name="us-east-1") + assert s3.head_bucket(Bucket="mybucket") is not None + + def test_should_not_find_unknown_bucket(self): + s3 = boto3.client("s3", region_name="us-east-1") + with pytest.raises(ClientError): + s3.head_bucket(Bucket="unknown_bucket") + + +@mock_s3 +class TestWithSetupMethod: + def setup_method(self, *args): + # This method will be executed automatically using pytest + s3 = boto3.client("s3", region_name="us-east-1") + s3.create_bucket(Bucket="mybucket") + + def test_should_find_bucket(self): + s3 = boto3.client("s3", region_name="us-east-1") + assert s3.head_bucket(Bucket="mybucket") is not None + + def test_should_not_find_unknown_bucket(self): + s3 = boto3.client("s3", region_name="us-east-1") + with pytest.raises(ClientError): + s3.head_bucket(Bucket="unknown_bucket") + + +@mock_s3 +class TestWithInvalidSetupMethod: + def setupmethod(self): + s3 = boto3.client("s3", region_name="us-east-1") + s3.create_bucket(Bucket="mybucket") + + def test_should_not_find_bucket(self): + # Name of setupmethod is not recognized, so it will not be executed + s3 = boto3.client("s3", region_name="us-east-1") + with pytest.raises(ClientError): + s3.head_bucket(Bucket="mybucket") + + @mock_s3 class TestWithPublicMethod(unittest.TestCase): def ensure_bucket_exists(self):