Skip to content
Advertisement

Using unittests and moto to mock AWS

I am used to pytest approach for unit testing, without using classes. Today I wanted to give a try to unittest and I wanted to encapsulate my tests inside a TestCase.

Then consider this sample test class:

JavaScript

Why is not the parameter placed in setUpClass visible from the test? I could imagine that by using the @moto.mock_ssm decorator there it would all have been done in the mocked context.

I can, however, place the parameter within test_something as just:

JavaScript

And then it (obviously) works. Why not with my first approach? I do not want to be populating the fake ssm parameter for each test that will rely on it. What is the best way of doing so here?

The reason why I am asking this is because the class I want to test requires this parameter when it is initialised.

Advertisement

Answer

The mock-decorators are scoped for that particular function, so data created inside that function will only be available there.

Instead, you can use the class-decorator:

JavaScript

Note that I’ve switched to setUp, instead of setUpClass. Because setUpClass is a classmethod, it is executed before the decorator is applied, and it will try to execute this method against AWS itself.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement