From d080945390b243a9d27d688dc4823c3d35ccdc32 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 26 Aug 2023 11:20:22 +0100 Subject: [PATCH] daily-email: add 2023-08-24 Testing multiple implementations with contract tests --- src/content/daily-email/2023-08-24.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/content/daily-email/2023-08-24.md diff --git a/src/content/daily-email/2023-08-24.md b/src/content/daily-email/2023-08-24.md new file mode 100644 index 00000000..de35567b --- /dev/null +++ b/src/content/daily-email/2023-08-24.md @@ -0,0 +1,24 @@ +--- +title: > + Testing multiple implementations with contract tests +pubDate: 2023-08-24 +permalink: > + archive/2023/08/24/testing-multiple-implementations-with-contract-tests +tags: + - automated-testing + - test-driven-development +--- + +If you have multiple implementations of a service, as I [mentioned in yesterday's email](), you need to ensure they all provide the same functionality. + +You need to be able to run the same tests against each implementation and have them pass. + +## How do you do this? + +In PHP, I use a trait that contains the test methods and then have a test class for each implementation that uses the trait and sets up any test data. + +Then, each test class will run the methods from the contract test trait and ensure they all provide the same behaviour, regardless of how it does so - whether it communicates with an API, uses an SDK, or returns fake values. + +If one implementation doesn't return the same result as the others, its test will fail. + +If you add a new implementation, you create a new test class, use the trait and get the tests to pass.