Pytest With Eric

Learn to write production level Python Unit Tests with Pytest

Staying up-to-date on pytest CLI commands can be painful.

When running tests via the console you have to remember to log the output, runtime environment variables, set timeouts, coverage, parallel executions, requirements and many others.

Programmers are good at logic, not remembering things.

That’s why we made config files. They let you specify the settings once and move on with writing your tests.

So how do you specify config in Pytest?

Read more »

We think that computers can do tasks “in parallel”. As much as we’d like, that’s not actually the case.

The modern computer is actually sequential, although at a very high clock speed unperceivable to humans.

Perhaps supercomputers or quantum computers are capable of true synchronous execution.

Almost all code is executed sequentially. However, there are optimisations we can do to efficiently use computing resources (CPU/RAM).

Async stands for Asynchronous and refers to the efficient allocation of resources.

For example, while one task is pending the system executes another task and then comes back to the pending one.

While it’s easy to write async functions, how do you Unit Test them?

Read more »

Imagine waking up to find your daily Unit and Integration Tests have taken ages to run or are stuck in a loop?

This happens more often than you think and for any reason — slow networks, external dependencies, resource allocation issues.

Whether you’re developing code locally or running Unit Tests as part of a CI/CD pipeline, it’s important to keep tests lightweight and fast.

An interesting concept to take note of is timeout . This means exactly what the name says — the code times out if it takes longer than x units of time.

Remember your test suite needs to be fast, timeouts are a last resort, not an expected failure mode.

Good coding practices rarely result in a timeout. Keeping that in mind, if you still wish to use timeouts, pytest timeout is an interesting feature.

Read more »

Have you had to ship code without fully functional Unit Tests?

In an ideal world, you have all the time in the world to write thorough Unit and Integration tests.

But the real world wants features and bug fixes, like yesterday. This presents a challenge.

Fortunately, can “skip” or “fail” tests that are related to an upcoming feature or bug-fix and not yet ready.

Or perhaps you don’t want to run some tests every single time (e.g. connection to an external DB or API).

Read more »

As a good developer, how do you ensure your code always works as expected?

Perhaps your boss asks, “have you tested all conditions and use cases?”

One of the most beautiful bits of programming is its deterministic nature. We tell a machine what to do, and it does the same thing—every single time.

But let’s be realistic, sometimes the real world (particularly Users) use your application in ways that you never could have predicted. Expecting it to work just fine.

How do you engineer or account for this?

Read more »

You may have noticed that a lot of companies have recently gone serverless with their micro-services.

Using AWS Lambda, Google Cloud Functions, Azure or other alternatives.

The benefits and ease of complexity these technologies offer are immense — no servers to manage to auto-scaling up to 10k requests/second with AWS API Gateway.

I don’t have to convince you of the benefits of serverless but just in case here’s an interesting article to help you decide if your company should go serverless.

While serverless is brilliant, it’s quite challenging for developers to fully test and validate our code in the Cloud Ecosystem locally.

Read more »

When writing Python code, you’ve probably used Environment Variables, at some point.

Whether in serverless lambda functions or stateful deployed apps.

Environment Variables are variables whose state is passed to the code or script at runtime.

Or variables whose state doesn’t change for execution (e.g. related to the system, user or network).

Although the best practices point to maintaining stateless code at all times, this is often not always possible.

Imagine deploying your code to a server and finding out it doesn’t work.

Read more »

So you’re a backend developer or data engineer and probably stumbled across this article when looking to speed up your Unit Tests.

TDD (Test driven development) is the practice of writing unit tests alongside the actual source code.

Why? Because it makes you think of edge cases and functionality you generally don’t think of when writing source code.

That’s great. But what about running these tests? Maybe 100s of tests?

Read more »

Have you heard of monkeypatch but have no idea what it means?

Or maybe you’ve even seen it in your company’s code base and wondered why your colleagues use it.

Rather than go through complex documentation or a bunch of Stack Overflow posts, let’s understand what is monkeypatching at a high level and when and how we can use it to improve Unit Testing.

Read more »

Are you a python backend developer looking to improve your TDD (Test Driven Development) skills?

Or perhaps you heard or seen a colleague usepytest conftest in a PR and don’t understand why or how they used it?

Well, you’ve come to the right place.

Read more »