I think we shouldn't test for printed output in our tests, as it doesn't allow you to easily change and/or add new logs.
also, we could use a library such as moto to avoid actually API calls during unit tests, so anybody can run unit tests without configuring AWS credentials. Additionally, we can always run the same tests as "integration tests" on a real AWS account.
we could speed up all the tests related to delays by patching time.sleep. This is not super critical now, but it'd allow us to add more tests and test much longer intervals without waiting minutes to unit tests to complete.
For example, this:
@ignore_warnings
def test_get_delay(self):
with patch('sys.stdout', new=StringIO()) as fakeOutput:
response = handler('foo', 'bar')
assert (
'Injecting 400 of delay with a rate of 1' in fakeOutput.getvalue().strip()
)
self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")
could simply become this:
def test_get_delay(self):
with patch('time.sleep', return_value=None) as patched_time_sleep:
response = handler('foo', 'bar')
patched_time_sleep. assert_called()
self.assertEqual(str(response), "{'statusCode': 200, 'body': 'Hello from Lambda!'}")
And we could mock SSM like this:
from moto import mock_ssm
@mock_ssm
class TestExceptionMethods(unittest.TestCase):
...
A few considerations about tests:
I think we shouldn't test for printed output in our tests, as it doesn't allow you to easily change and/or add new logs.
also, we could use a library such as
moto
to avoid actually API calls during unit tests, so anybody can run unit tests without configuring AWS credentials. Additionally, we can always run the same tests as "integration tests" on a real AWS account.we could speed up all the tests related to delays by patching
time.sleep
. This is not super critical now, but it'd allow us to add more tests and test much longer intervals without waiting minutes to unit tests to complete.For example, this:
could simply become this:
And we could mock SSM like this: