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

Database Testing with pytest

Dan Clark
October 05, 2018

Database Testing with pytest

Testing relational database assests such as stored procedures, functions, and views can be awkward. However, Python can come to the rescue with pytest. We’ll dive into an example or two so that you too can leverage Python to test your own obtuse database structures.

Dan Clark

October 05, 2018
Tweet

Other Decks in Technology

Transcript

  1. Database Testing with Dan Clark • Code can be found

    at https://github.com/danclark5/pytesting_db • Companion Article is at https://bruisedthumb.com/post/2018-07-29
  2. Tables, Views, Stored Procedures, and Functions. Oh My! “Immersed in

    Numbers” by Chris Khamken is licensed under CC BY 2.0
  3. A few caveats We’re starting with an empty database Build

    it up, tear it down! Database transactions for the win!
  4. Some helpful pytest options • -s: sets the capture method

    to "no". • -m: runs tests that are marked with a certain string. • --pdb: runs the debugger when an error is encountered • --lf: runs only the tests that failed on the last attempt. • -v: make pytest verbose
  5. Let’s make a test • Keep it simple Socrates •

    Make sure that tests can connect to the database. • Confirms pytests is working.
  6. Benefits and Drawbacks • Can be migrated to another product

    • Fixtures are reusable • Tests and fixtures can be grouped together • We can leverage Python’s vast module ecosystem • Using assert instead of specialized functions • Command line based. However Pycharm to the rescue • Code coverage is useless • No AssertEqualsTable (tSQLt)
  7. • Code can be found at https://github.com/danclark5/pytesting_db • Companion Article

    is at https://bruisedthumb.com/post/2018-07-29 Twitter: @danclark Thanks!!!!!!!!!!!