Applying moto on a classmethod fails

429 views Asked by At

Applying a moto mock to the test class as a whole will have no effect on classmethods like the python unittests setupClass method.

@mock_ssm
class SomeClassTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        boto3.client("ssm").put_parameter(Name="some-name",
                                          Value="some-value")

will result in

botocore.exceptions.NoCredentialsError: Unable to locate credentials
1

There are 1 answers

0
Vincent Claes On BEST ANSWER

One way to applying moto instances to the class as a whole including the classmethod, is to explicitly instantiate and terminate the moto service

class SomeClassTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.mock_ssm = mock_ssm()
        cls.mock_ssm.start()
        boto3.client("ssm").put_parameter(Name="some-name",
                                          Value="some-value")
    
    @classmethod
    def tearDownClass(cls) -> None:
        cls.mock_ssm.stop()