Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gagnez en confiance dans vos tests - adapter co...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for Mathieu Cans Mathieu Cans
January 07, 2025
15

Gagnez en confiance dans vos tests - adapter contract testing

Vos tests sont au vert mais vous avez un bug en prod ?
Vous n'avez pas suffisamment confiance dans vos tests pour déployer en production ?
Vos tests vous ralentissent quand vous refactorez ?
L'intégration avec d'autres services sont source de bugs ?

Ces limitations peuvent venir de l’utilisation des mocks dans les tests. Mais comment les dépasser ?

Grâce à au contract testing et à l’architecture hexagonale, dans cette formation à forte composante pratique, vous apprendrez à tester l’intégration avec les services externes.
Cette approche permet d’obtenir des simulateurs à moindre coût qui se comportent exactement comme les services externes.
Cela se révèle être un formidable outil pour tester l’application dans son ensemble de manière plus explicite avec plus de confiance qu’en utilisant les mocks.

Avatar for Mathieu Cans

Mathieu Cans

January 07, 2025

Transcript

  1. Spiral journey What did you say no to in order

    to attend this training ? According to you, what could Adapter Contract Testing be ?
  2. Spiral journey What did you say no to in order

    to attend this training ? According to you, what could Adapter Contract Testing be ? From your experience, how contract testing may help you in your daily work ?
  3. Spiral journey What did you say no to in order

    to attend this training ? According to you, what could Adapter Contract Testing be ? From your experience, how contract testing may help you in your daily work ? What is the big question you bring to this trining?
  4. Agenda • Hexagonal architecture • Adapter contract testing principle •

    TP1: contract test a database • State based vs interaction based testing • TP2 : contract test a read only provider • Le problème avec les mock • TP3 : use simulators to test & write domain application • Contract testing in the CI • Examples and REX • Contract testing in real life • Conclusion
  5. Pré-requis Cloner le projet github https://github.com/mathieucans/snail-race-kata Télécharger les images docker

    suivantes : docker run --rm -p 27017:27017 --name mongo_contract_testing -d mongo docker run --rm -d -p8000:8000 --name snail-race-server mathieucans/snail-race-server:latest
  6. Hexagonal Architecture Infrastructure Code Domain model use case use case

    Emailer Interface Repository Interface Primary port Secondary Port Email Service Data store MVC Controller MVC Controller Emailer Impl. Repository Impl. Is about domain and infrastructure
  7. Adapter contract testing disclamer • Adapter contract testing is about

    : • domain & infrastructure • Hexagonal architecture • It’s not about : • µ-services • PACT (https://docs.pact.io/)
  8. Test Real adapter Simulator Contract = running the same test

    Domain Interface Including anti-corruption layer External Service
  9. TP1.1 : Contract test a data base Cloner le projet

    github https://github.com/mathieucans/snail-race-kata Ouvrir le répertoire du langage de votre choix dans votre IDE Par exemple snail-race-bet/typescript/ pour travailler en typescript ⚠ Attention : ne pas utiliser le répertoire snail-race-server qui ne fait pas parti des exercices
  10. TP1.2 Use contract test to write simulators Adapter Domaine (Port)

    Infrastructure Contract Test Fake Domaine (Port)
  11. TP1.2 : Use contract test to write simulators BetRepositoryMongoDb BetRepository

    (Port) BetRepository Contract Mongo BetRepositoryFake
  12. XUnit Jest / Jasmine Golang Tests location Abstract class Inside

    a function List of test methods Entrypoints Concrete class Spec fi le calling the function TestMain function
 running the list of methods How to use same test for both adapter and simulator
  13. BetRepository Contract BetRepository MongoDb BetRepository Fake Only setup BetRepositoy (port)

    MongoDB BetRepository MongoDBTest BetRepository FakeTest All tests here Inherits Inherits
  14. JS

  15. Examples of adapters • Repository • Ex PersonRepository • File

    read/write wrapper • Message queue • External service wrapper • Ex Pdf extractor, email service …
  16. Types of external dependencies Read - Registries - Pdf extraction

    - Weather Write - Emailing - Payments - Job launching Read - Write - Data stores - Identity pools - File systems
  17. TP2 Adapter contract testing • Contract test a read only

    provider RaceResultProvider (Port) Contract Test RaceResultProviderHttp snail- race-server RaceResultProviderFake
  18. Use contract test to discover the api •Explore the endpoint

    with a unit test •Use the json response to create infrastructure types •Write a contract test that uses the desired port •Unit test the mapping (infra->domain) with pure unit tests •
  19. Use contract test to discover the api •Explore the endpoint

    with a unit test •Use the json response to create infrastructure types •Write a contract test that uses the desired port •Unit test the mapping (infra->domain) with pure unit tests •
  20. Anticorruption layer testing • Static function transform(infraData): DomainData • Pure

    unit tests • Keep the anti-corruption layer in the infra
  21. Test SUT Stub Spy Test SUT Fake / Simulator Other

    classes Setup Setup Veri fi cation Veri fi cation Testing with mocks Testing with fakes
  22. Test SUT Fake / Simulator Other classes Setup Veri fi

    cation Domain Interface Read depencency
  23. Test SUT Fake / Simulator Other classes Setup Veri fi

    cation Domain Interface Write depencency
  24. Advantages with adapter and contract tests • The real deal

    • Easy to setup • Catches external bugs • Fast • No “mock bugs” • Easier to interact with the external dependency • Easy to change technology • Easy to change the contract
  25. Domain rules The application can register bets • registering a

    bet is done by providing • gambler name • the 3 numbers of snails on the podium The application lists the winners according to last race ran. To win a bet : • The podium match exactly the race result • The bet has been registered at least 3 seconds before the race timestamp • A bet is valid only for the next race
  26. • Where is implemented the selection of the snails on

    the Podium ? RaceResultProvider (Port) SnailRaces SnailRace Podium Snail * 1 3 RaceResultProvider (Port) SnailRaces SnailRace Snail * 1 10 VS Anti-Corruption vs domain layer opportunities
  27. • How do we ensure that we select the last

    race ? RaceResultProvider (Port) SnailRaces SnailRace Podium Snail * 1 3 VS RaceResultProvider (Port) SnailRace Podium Snail 1 3 Anti-Corruption vs domain layer opportunities
  28. Anti-Corruption vs domain layer opportunities • Other examples : •

    What about snail number unicity in the podium ? • What about races with less than 3 snails ? • What about equal positions?
  29. Annoyances with adapter tests • Availability • Speed • External

    changes? - and frequency • Cost of call • Accessible locally?
  30. Contract tests in the CI • Put in a separate

    pipeline (possibly di ff erent triggers/periodicity) • Put in an optional pipeline/step • Use as uptime-bot • Extract as a library • Make them stable! • No CI
  31. Use contract test for production metrics • Use exactly same

    test to generate metrics on production • Useful when traffic still small • Better diagnostic • OPS can monitor them
  32. Example: gmail reply detection Gmail has no native way to

    check if an email has been replied to It can be implemented through threads Need to expose this to the domain 1. Discovered how thread works through contract tests 2. Use these tests to write a simulator like gmail 3. Very productive and con fi dent in implementing the domain logic
  33. Example : api change Both simulators and contract test shave

    been written to consume orange mails The application was tested with simulators Need to change end point used to read mails in the adapter (QOS issue) 1. Write the adapter with the new end point 2. Check the behavior still works as the old one and the simulator 3. Integrate the new end point safely without change neither domain code nor domain tests
  34. Example : bu Both simulators and contract test shave been

    written to consume orange mails The application was tested with simulators Need to change end point used to read mails in the adapter (QOS issue) 1. Write the adapter with the new end point 2. Check the behavior still works as the old one and the simulator 3. Integrate the new end point safely without change neither domain code nor domain tests
  35. Example: remote fi le system (OneDrive) • Very slow, in

    particular for listing nested folders • There were around 15 methods on it • Having the fake made it very easy to implement a good caching system • As a result it was easy to implement both a persistent and an ephemeral caching system Storage OneDrive Slow
  36. Example: public company registry • Getting info about companies •

    Problem: not really. Read-only but stable • Solution: keep the exact assertions • In retrospect: They changed sometimes, but not often. Our app Data.gouv
  37. Example: data.entreprise.gouv • Getting (more) info about companies • Problem:

    Heteregenous data • Solution: Try ingesting big amounts of data as a test • Result: We discovered rare problems in dev as opposed to production Our app Data.gouv Heteregenous 
 data
  38. Example: Android system • A js-app running on android, it

    has a chatty api with a native app. • Problem: complex and wrong mocking • Solution: build a simulator (without contract testing) • Gain: Reliable tests of the isolated js-app js app native app Complex 
 interactions
  39. Example: Run tests on DB production • Warning : after

    the training • Running contract test in isolation as much as possible
  40. Exercice: write contract test for your project • List the

    contract tests you’d write for one of your dependencies (only names) • Start implementing some tests on the real dependency, as if you were starting from scratch • Improve the interface (i.e. extract or refactor) • In particular no infra concerns should transpire • Transform it to a contract test
  41. Quelques références • Contract Test - Martin Fowler • Testing

    Without Mocks - James Shore • Architecture Hexagonale : gestion de la complexité logicielle et de la dette technique - Julien Topçu
  42. Con fi ance dans les 
 tests du domaine Tests

    de l’infra pertinents Migration de dépendance facile Refactorabilité 
 ++ 
 Contract
 Testing