Pytest With Eric

Learn to write production level Python Unit Tests with Pytest

APIs form the backbone of your backend operations.

Every software you interact with, from mobile to web apps, uses APIs to bridge the gap between frontend and backend.

Given it’s immense importance, testing your APIs is as crucial as building them. After all, if your API doesn’t work as expected, your entire application may be rendered useless.

Not to mention upsetting your users and clients.

FastAPI is an amazing framework for building APIs in Python.

But how do you test your FastAPI APIs? Do you test each endpoint? What about the payloads and responses?

How do you make sure your API returns helpful responses in the case of client or server errors? Should you test against real data or mock it?

Read more »

Testing is an important part of the software development lifecycle (SDLC).

It helps ensure that your application is reliable, stable, and less prone to bugs (nobody is 100% immune).

Pytest is a popular testing framework to help you write and run tests for Python.

Testing helps identify bugs and errors in the software before its released to end users.

This helps prevent costly and damaging consequences.

Read more »

Testing is a critical component of software development, and Pytest is one of the most popular testing frameworks out there.

It provides a powerful and flexible testing environment, allowing you to write concise, maintainable and easy-to-understand tests.

One of the beautiful things about Pytest is that it can be easily extended using plugins, which provide additional functionality and make it even easier to write and maintain your unit tests.

Read more »

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 »

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 likely used environment variables to pass configuration or other data to your code at runtime.

Specifying environment variables in Python is easy, but when it comes to testing, things can get a bit tricky.

You need to understand variable preference, how to override them, and how to ensure your tests are isolated from the local runtime environment.

So how do you define environment variables in Pytest? Should you use a .env file, fixtures, specify them for each test?

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 »

So you’re tasked with building a service that talks to external REST API.

You use the Requests or similar library, whip up some GET or POST methods and voila! All done.

Easy peasy, right?

Well not so. If you’re doing it for a hobby or quick side project then yes.

But as a professional developer, you know that you’ve to account for all edge cases.

The challenge when dealing with external APIs is that the behaviour is outside your control.

Schema or payload changes, new error codes, and updated speed caps are some problems that may plague you.

Read more »