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

Testing your "TESTS"

Testing your "TESTS"

Proposal for PyDelhi Conference 2017

48fdf38336211f8dbf454afcc6575806?s=128

Aniket Maithani

January 25, 2017
Tweet

Transcript

  1. Testing Your ”TESTS” - ANIKET MAITHANI

  2. $: whoami o Aniket Maithani o Codes in Python o

    Works @Corseco Technologies o Foodie o -----”MAI--_THA--_NI” o @2aniketmaithani o https://github.com/aniketmaithani
  3. TESTING 101 Test(s) in Software scenario refers to the investigating

    the internal “health” of the code about the quality of the product or service. As the number of possible tests for even simple software components is practically infinite, all software testing uses some strategy to select tests that are feasible for the available time and resources But testing cannot identify all the defects within software at all times
  4. “Software testing proves the existence of bugs not their absence.”

    – CHUCK NORRIS
  5. Types of Testing TESTS UI MODULE FLOW Functional Testing Unit

    Testing Integration Testing AUTOMATED TESTING MANUAL TESTING
  6. Popular Python Testing Modules vNose vUnittest vDoctest vPyTest vTox

  7. PyTest • Easy To Use • Popular • Concise •

    Easy Assertions • Plugins • POWER OF FIXTURES!
  8. INSTALLATION

  9. The objective of Unit testing is to isolate a unit

    and validate its correctness. Unit Testing
  10. Let’s Get Started

  11. Basic Python Program

  12. Now Run `py.test`

  13. How Py-Test discovers your tests?? Ø Filename should start from

    test_foo_bar_etc.py Ø Function name should be test_foo_bar_etc() Ø Configuration defined in setup.cfg or setup.ini settings file
  14. Example

  15. Let’s Dive a Bit More Deep…….. q SETUP & TEARDOWN

    q FIXTURES
  16. SETUP & TEARDOWN Ø When a setUp() method is defined,

    the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test. Ø You can create a setUp and tearDown for a bunch of tests and define them in a parent class - so it would be easy for you to support such tests and update common preparations and clean ups.
  17. EXAMPLE Ø SetUp() Ø TearDown()

  18. Fixtures Ø The purpose of test fixtures is to provide

    a fixed baseline upon which tests can reliably and repeatedly execute. Ø fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. Ø fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures. Ø fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across class, module or whole test session scopes.
  19. Dependency Injection & Fixtures Example

  20. Running The Test Again

  21. More About Fixture Ø Fixtures in Django [Example] Ø Tests

    for Fixtures Ø Fixing the ”Fixtures”
  22. MOCK v mock is a library for testing in Python.

    It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. Installation is simple :
  23. Examples - v Mocking Request v Mocking Objects v Mocking

    Functions
  24. MonkeyPatch / Patching [Examples and Details]

  25. Py.Test & PDB Ø Combine the power of PyTest and

    PDB for real time test assertions Ø Easy to Use Ø Powerful Ø Interactive Ø Real Time Analysis
  26. Resources/Links http://doc.pytest.org/en/latest/contents.html https://docs.python.org/2/library/unittest.html www.voidspace.org.uk/python/mock/examples.html www.drdobbs.com/testing/unit-testing-with-python/240165163 https://docs.python-guide.org/en/latest/writing/tests/

  27. That’s all FOLKS!