Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Databases and Testing Libraries

Databases Libraries linked in this lesson:

Real Python Database Resources

Testing Libraries

Real Python Testing Resources:

00:00 In the previous lesson, I covered DevOps and development environments. This penultimate lesson is on databases and testing libraries.

00:09 Most code touches data somehow, and databases are common long-term stores that you can use. There are several kinds. The most common are called relational databases.

00:19 These use interlinked tables that are kind of like spreadsheets containing rows and columns. The most common way to interface with relational databases is a separate language called SQL, which you can embed inside your Python scripts.

00:33 Relational databases aren’t the only thing out there, though. There are document stores and other kinds. As these kinds of databases don’t use SQL, they sometimes are called NoSQL databases.

00:45 Often, the stuff you’re storing in a database maps to objects in your code. For example, for a contact book, you might have a person object that maps to a person table in the database.

00:55 There’s a family of tools called Object-Relational Mapping Tools, or ORMs, that allow you to interact with a database by mapping objects to tables with little or no SQL required.

01:09 The libraries on this slide allow you to connect directly to specific databases. SQLite is probably the most used database in the world. Even if you’ve never heard of it, you’ve used it as a lot of the apps on your phone use this under the covers.

01:23 The Python standard library includes an interface to SQLite. Two popular open-source databases are MySQL and Postgres. There are third-party Python libraries that can connect to these. If you’re looking for a higher-level coding mechanism, SQLAlchemy is a general database library that has three different ways of interacting with a database, including an ORM.

01:47 It works with many different databases as well, which means you don’t have to figure out the specifics for any given database.

01:54 I know I mentioned Django way back in the web frameworks topics, but one of its strengths is that it ships with an ORM. You don’t typically use Django unless you’re doing web stuff, but if you expect to write code that interacts with a database and needs a web front end, Django is the place to go.

02:10 Granted, I’m biased. I did write a book on the subject after all. One of the more popular document store databases out there is MongoDB. These two libraries both provide ways to interact with it.

02:25 There are plenty of tutorials on databases that you can find in this topic listing, or if you want a step-by-step approach, you can try this learning path.

02:36 Did you know you can write code to test your code? As odd as this might sound, it’s a powerful technique that helps ensure the quality of your software, especially when you make a change.

02:46 Re-running your tests ensures you haven’t broken anything, creating a regressive defect. Automated testing is considered a best practice when coding professionally, and is often mandatory with minimum coverage measurements when working on larger teams. Writing tests is good, but knowing how to write your code so it is testable is also necessary.

03:05 For example, if you’re using something like Selenium to test code in the browser, making sure all the parts of your HTML have unique IDs makes it easier to write tests.

03:15 The standard library includes a testing framework called unittest, and a second one where you write tests in the pydoc comments in your code.

03:25 The unittest library can be a little verbose. pytest is a popular third-party library that allows you to write more tests with fewer lines of code.

03:36 Once you’ve got your tests, you may need to run them in multiple environments. The tox library allows you to do this, creating virtual environments for each of your setups.

03:46 tox uses configuration files for its setup and it can get quite complicated. nox is an alternative that uses Python code instead of configuration files. Why learn yet another thing when you already know how to write Python?

04:01 This is a nox script from one of my libraries. This decorator tells nox which versions of Python to run a test against. Here, I’m testing against everything from Python 3.9 through 3.13.

04:13 That means five different virtual environments will get set up with dependencies installed in each, and tests run against each setup.

04:22 The install method allows you to install dependencies. In this case, it’s a library called waelstow. You can have as many calls to install as you need, and then you use the run method to execute your tests.

04:34 In my case, I’m calling another script, but this could simply call Python’s unittest module directly or invoke pytest if that’s your jam. This is a list of tutorials on testing topics, and if you want the step-by-step approach, this learning path can help.

04:53 In the last lesson, I’ll briefly summarize the course, tell you a bit about where Python might not be the right choice, and point you at even more resources to continue your journey.

Become a Member to join the conversation.