Testing Microservices: Testing Microservices: Testing Microservices: fast and with confidence fast and with confidence fast and with confidence Stephan Jaensch Stephan Jaensch Stephan Jaensch @s_jaensch @s_jaensch @s_jaensch 1
2
Two years ago... Two years ago... 3
The Testing Pyramid The Testing Pyramid
The Testing Pyramid The Testing Pyramid
Why end to end tests? Why end to end tests? Source: https://www.slideshare.net/danveloper/microservices-the-right-way 6
Flakes Flakes 7
Improving speed and reducing Improving speed and reducing flakiness flakiness
8
Contract testing Contract testing Contract Testing is writing tests to ensure that the explicit and implicit contracts of your microservices work as advertised. Bob Reselman 9
A service call at Yelp A service call at Yelp 10
Anatomy of a client library request Anatomy of a client library request Construct Unmarshal request response Validate Marshal data response Validate Adapt HTTP request response HTTP request HTTP response REMOTE SERVICE 11
Inject mock in client library Inject mock in client library Construct Unmarshal request response Validate Marshal data response Validate Adapt HTTP Mock request response HTTP request HTTP response REMOTE SERVICE 12
Patching the client library Patching the client library class TestExampleHappyhourClient (): @pytest.fixture(autouse=True) def setup_teardown (self): with patch_clientlib() as mock_client: yield def test_get_hours_simple (): hours = get_hours_simple() assert hours == { 'SUN': {'time_start': 0, 'time_end': 0}, 'MON': {'time_start': 0, 'time_end': 0}, ... } 13
Providing a mock response Providing a mock response class TestExampleHappyhourClient (): @pytest.fixture(autouse=True) def setup_teardown (self): with patch_clientlib() as mock_client: mock_client.hours.getHours\ .set_return_value_and_status_code( return_value={ 'FRI': {'time_start': 21, 'time_end': 23}, 'SAT': {'time_start': 21, 'time_end': 23}, 'SUN': {'time_start': 21, 'time_end': 23}, }, status_code=200, ) yield 14
Mocks with errors Mocks with errors jsonschema.exceptions.ValidationError: 'id' is a required property 15
Conclusion Conclusion End to end tests don't scale infinitely Contract tests are supposed to help We can leverage existing infrastructure to do consumer driven contract testing (sort of) We're relying on the fact that the service adheres to the specification 16
17
Questions? Questions? sjaensch@yelp.com @s_jaensch 18
Recommend
More recommend