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

Safe snake charming (or "How to test your Pytho...

Safe snake charming (or "How to test your Python code")

In this talk we will see some tips and tools that will help us in the process of testing our Python code.

We will start with a general introduction to testing to emphasize why it's important to have a good testing suite and some tips to achieve it, along with presenting useful concepts like mocking. Then, we will review some of the most widely used libraries like unittest, pytest or tox, and how we can get the more out of them.

Despite the main focus of the talk will be Python tools, there will also be a general introduction to testing concepts and practices that may be useful for developers no matter the language, so you can enjoy the talk even if you don't have knowledge of that language.

Alex Gascón

November 15, 2018
Tweet

More Decks by Alex Gascón

Other Decks in Programming

Transcript

  1. Index 2 1. Why testing is important? 2. Types of

    tests 3. Structuring tests 4. Fake it ‘til you make it 5. Code examples
  2. Why testing is important? 3 “Either you test your code

    to look for bugs the it contains, or you let your users do it for you”
  3. Isolating your tests 11 Your test Another test Environment variables

    Internet Order of execution Date and time 42
  4. Fake it ‘til you make it 12 Fake your codebase

    Dummy Data that you will never use (but that you need e.g. to fill a parameter list) Fake Has a working implementation, but takes some shortcut Stub Does what you tell them to do Mock Does what you tell them to do (and then they let you know if they did it)
  5. Fake it ‘til you make it 15 Fake your environment

    Environment variables Set them during the test (set up), don’t use the ones in your system Datetime The result of your test shouldn’t depend on the day in which you execute it (e.g. if it uses a credit card that will expire). Stub the date-time
  6. Fake it ‘til you make it 16 Fake your data

    Fixtures Before running the test, load the data that will be used from a static file/source
  7. Fake it ‘til you make it 17 Fake the internet

    Fake requests and responses Don’t do external requests, fake them instead
  8. 18