Pytest With Eric

Learn to write production level Python Unit Tests with Pytest

When writing unit tests, it’s hard to consider all possible edge cases and validate that your code works correctly.

This is sometimes caught in production and a quick and speedy patch needs to be deployed. Only for a new bug to emerge later.

There will always be cases you didn’t consider, making this an ongoing maintenance job. Unit testing solves only some of these issues.

Read more »

There are several frameworks for building robust and scalable Rest APIs with Python.

FastAPI, Flask and Django are the most popular, reliable and easy to use.

However, building APIs is incomplete without thorough testing.

Unit tests and Integration tests are necessary to ensure your API works for client use cases.

Most of these frameworks come with inbuilt Unittest integration, but writing and maintaining test classes can be cumbersome.

Read more »

Do you find yourself with lots of boilerplate code when writing unit tests?

Monotonous code like database setup, teardown, API clients or test data can be painful to replicate across 10s or 100s of unit tests.

When writing tests, it’s often necessary to set up some initial state before running the actual test code.

This setup can be time-consuming to write, especially when there are multiple tests that require the same steps.

Read more »

Continuous Integration (CI) is an essential practice in software development. It ensures you release small and quick.

Unit and Integration Testing form a vital piece of this CI/CD Pipeline. After all, what good is untested code?

But, does the thought of setting up and maintaining a CI/CD server like Jenkins, Ansible or Code Commit cause you stress?

Read more »

Complex software is made up of several moving parts - Rest APIs, Databases, Cloud services, etc.

However, when writing unit tests, you’re often interested in testing a specific part of your code, not the entire system.

So how do you test your code when it depends on external services? Do you call the REST API or connect to the database in your unit tests?

Read more »

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 »
0%