Non-unittest classes use a lowercase setup-method (#4833)

This commit is contained in:
Bert Blommers 2022-02-05 10:47:38 -01:00 committed by GitHub
parent bb6fb1200f
commit fbefae59c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 4 deletions

View File

@ -137,11 +137,15 @@ class BaseMockAWS:
c for c in klass.__mro__ if c not in [unittest.TestCase, object] c for c in klass.__mro__ if c not in [unittest.TestCase, object]
] ]
# Get a list of all userdefined methods # Get a list of all userdefined methods
supermethods = itertools.chain( supermethods = list(
*[get_direct_methods_of(c) for c in superclasses] itertools.chain(*[get_direct_methods_of(c) for c in superclasses])
) )
# Check whether the user has overridden the setUp-method # 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): for attr in itertools.chain(direct_methods, defined_classes):
if attr.startswith("_"): if attr.startswith("_"):

View File

@ -109,8 +109,9 @@ class TesterWithStaticmethod(object):
@mock_s3 @mock_s3
class TestWithSetup(unittest.TestCase): class TestWithSetup_UppercaseU(unittest.TestCase):
def setUp(self): 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 = boto3.client("s3", region_name="us-east-1")
s3.create_bucket(Bucket="mybucket") s3.create_bucket(Bucket="mybucket")
@ -124,6 +125,53 @@ class TestWithSetup(unittest.TestCase):
s3.head_bucket(Bucket="unknown_bucket") 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 @mock_s3
class TestWithPublicMethod(unittest.TestCase): class TestWithPublicMethod(unittest.TestCase):
def ensure_bucket_exists(self): def ensure_bucket_exists(self):