Posts tagged ‘python’


How to Mock Environment Variables in Python’s unittest

Our environment - mother gaia

Sometimes tests need to change environment variables. This is fairly straightforward in tests using Python’s unittest, thanks to os.environ quacking like a dict, and the unittest.mock.patch.dict decorator/context manager.

Read more...

How to Mock Environment Variables in pytest

Our environment - planet earth

Sometimes tests need to change environment variables. This is fairly straightforward in pytest, thanks to os.environ quacking like a dict, and the unittest.mock.patch.dict decorator/context manager.

Read more...

How to Unit Test a Django Management Command

Here comes the test train!

This post is an adapted extract from my book Speed Up Your Django Tests, available now.

Read more...

A Guide to Python Lambda Functions

It’s a Lamb, Duh

This is a cross-post from the Scout APM blog, where I occasionally write. I also maintain the Scout Python integration.

Read more...

Better Python Decorators with wrapt

Bee likes decorated flowers.

This is a cross-post from the Scout APM blog, where I occasionally write. I also maintain the Scout Python integration, which uses wrapt for its Python decorators.

Read more...

Why does Python raise ModuleNotFoundError when modifying Django's INSTALLED_APPS?

Pegasus, chained

Imagine we are installing the third party package django-cors-headers, which I maintain. Step one in its installation process is to install the package, so we run the command:

Read more...

How to Check if Python's Output Buffering Is Enabled

Buffering

Update (2020-10-12): Since Python 3.9, sys.stderr is line-buffered, which helps mitigate some of the problems with output not appearing.

Read more...

Why does Python log a SyntaxWarning saying "list indices must be integers or slices"?

Sssssssspam

Take this code, which we want to return a list of two breakfast orders:

Read more...

Why does Python log a SyntaxWarning saying "assertion is always true"?

Wiggle wiggle

Take this code:

Read more...

Why does Python log a SyntaxWarning saying "object is not subscriptable"?

The snake is out of the box now

Take this code:

Read more...

All tags: