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

Integration Tests with Super Powers

Integration Tests with Super Powers

You are maybe like me: I never learned at school how to write tests. My teachers gave me at first a broad overview of computer history. Then, they explained me some basic design patterns. And to finish, I often had to write more or less basic programs, to validate and demonstrate my skills. Not the kind of code I would be really proud of today: the procrastinator monkey living in my head at this time was more thinking about planning my summer holidays, rather than writing Ninja code!

And to make things worse, my studies focused on network and system engineering. Not software architecture. Funny story, because I decided to become programmer a couple of years later…

What I realize now is that I don’t have as much time as before to learn. And in a world driven by business, where time is money, and where tradeoffs are the rule, there is rarely enough money to write both shiny new features and a complete test suite.

People who practice Test-Driven Development know how complicated it can be to write proper tests. TDD is often discouraging at first: the learning curve is steep. But this problem also exists in the testing world in general. Because writing good tests is hard, many beginners get headaches trying to reach this goal. How to convince project managers to have more time for writing tests in these conditions…

But “le jeu en vaut la chandelle” as we say in French ("the juice is worth the squeeze"). Well tested applications are not only easier to maintain and extend. They also have in general a better API. That’s what we will see in this talk, by focusing on how to write integration tests. Our journey will begin with a presentation of different testing strategies. We will then jump to the practical part, using Pytest, interface testing , dependency injections and stubs, amongst many others. And because we want to add nice buzzwords on our resume after EuroPython, we will finish this talk by automating the whole with Docker Compose.

[Talk given at EuroPython 2018]

Alexandre Figura

July 27, 2018
Tweet

More Decks by Alexandre Figura

Other Decks in Technology

Transcript

  1. Integration Tests
 with Super Powers And even more... Alexandre Figura

    · Site Reliability Engineer @ SysEleven GmbH
  2. A Brillant Masterpiece Golden Snake Awards Nominated for Best Speaker

    Alexandre Figura ⭐⭐⭐⭐⭐ The Most Exciting Talk of the Year ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐ The Python Magazine Scottish Insiders Irresistibly Entertaining ⭐⭐⭐⭐⭐ Edinburgh Times
  3. Who Am I? 3 • Use Python with since 2014.

    • # Live in Berlin since 2016 % • Always eat 2 desserts at lunch • Work with cool folks at SysEleven GmbH,
 best company in town.
  4. 5 A Well-Kept Secret There is one And only one

    Reason why we write tests. We want our Pull-Requests to get accepted. And incidentally, to make it easier to maintain our code in the future.
  5. 7 Mocks Advantages - Easy to write. Drawbacks - Dependant

    on implementation, - Make refactoring harder, - Hide errors and behavioral changes. Conclusion Never use them* *Unless you have a very good reason...
  6. 10 Basic Concepts Integration Tests are built around two ideas:

    1. Dependency Injection, 2. Interface Testing. It sounds complicated. But it isn't.
  7. 11 Summary Advantages: - Tests not dependant anymore on implementation.

    - Easy to change dependency later on. - Make refactoring easier. Drawbacks: - Require way more work at first.
  8. 13 To the Cloud and Beyond Use Case We want

    to upload a static website To the Cloud Using OpenStack Object Store's API. Links: - Officiel API, - Fake Implementation, - Interface Tests, - Parametrizing with Pytest. Container Object Object Object Object Object Store
  9. 15 Mock Injection Too much work to write Fake dependencies?

    - Write a Mock-like implementation, - Inject it as a dependency. Useful for simulating complex systems. Advantages: - Easy as mocks, - Tests remaining independent of implementation
 (makes refactoring easier).
  10. 16 Minimalist Interface Testing Use Case: I make Holiday movies

    Recorded on Blu-ray And need to convert them to MKV. Problem: A Blu-Ray is 30-50GB large. Solution: Only check for syntax errors. Links: - Syntax Errors Testing, - Fake Implementations, - Dependency Injection.
  11. 18 Standard Workflow Tests independent of running platform. Working example

    available on Github. Docker Compose Application Container Container 2 Container 3 Source Code Tox +
 Pytest Container 4
  12. 20 Summary Mocks make refactoring harder. Interface Testing is more

    flexible, but involves more work. Dependency Injection will improve your code's API. Always ask yourself (or around)
 if you need 100% test coverage.