Using doctests from within unittests in pytest

Photo by David Clode on Unsplash

Hello everyone. I’ve discovered for myself lately a need to run at the same time doctests alongside with pytest unit tests.

Let’s assume one’s have such structure of a project:

A structure of a project.

That person needs to cover by unit tests app package and at the same time check doctests of functions, classes, etc. by means of pytest’s invocation. How can it be achieved? We can put a file named, for instance, test_package_doctests.py (or any file named according to pytest.ini rules defined by an owner of a project) to the tests directory with the following content.

Content of test_package_doctests.py

It’s a hacky way to discover both unit tests & doctests by mimicking run of DocTestSuit inside an ordinary unit test.

Here’s an example of the output, it’s pretty smooth:

pytest output.

Simple as that. I hope this article helped you.
P.S. it’s more simple to implement such behaviour when used standard unittest module see this.
P.P.S. a full implementation can be found here.

Software Engineer, LL.B., MSc in CS

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DevOps In 2021 — Tools And Trends That You Need To Be Aware Of!

AWS ECS Blue/Green Deployment with CodePipeline, using AWS CLI

Reading the Source Code of Microsoft.NET.Sdk, Writing the Creative Extension of Compiling

Transform your SDLC with Continuous Integration & Continuous Deployment

Transform your SDLC with Continuous Integration & Continuous Deployment

Serverless Testing, Part 2: SOLID Architecture for Lambda

Create aliases for git bash

Automatically pulling and visualizing data with n8n 📈

Badgerloop in California: Day 4

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vladyslav Krylasov

Vladyslav Krylasov

Software Engineer, LL.B., MSc in CS

More from Medium

Installing Spark/PySpark on Mac-OS Monterey with Anaconda/MiniConda/VS Code

Apache Sqoop with Postgresql

Setup Apache Hadoop on local Ubuntu System

How to reverse parametrize in PyTest fixtures

Indirect parametrize