Pytest With Eric

Learn to write production level Python Unit Tests with Pytest

Asynchronous task processing has become a cornerstone for building efficient and scalable applications in today’s fast-paced world.

Users perform actions and expect results near real-time. This can be efficiently handled with distributed computing and asynchronous task processing.

At the heart of this asynchronous revolution lies Celery, a powerful and popular distributed task queue framework for Python.

Celery empowers you to offload time-consuming tasks to the background, allowing applications to remain responsive and performant while handling resource-intensive operations.

While Celery simplifies the implementation of asynchronous workflows, testing them can be challenging.

Read more »

As a software developer, you’re no stranger to the importance of effective testing strategies, especially Test-Driven Development (TDD).

As projects become complex, so do the testing requirements, leading to repetitive and time-consuming test case creation. Is there a way to simplify test generation?

Fortunately, a game-changing solution exists!

Read more »

Have you explored passing command line arguments in Python? If you have, you’re likely familiar with the convenience it offers.

The capability to define and modify runtime behavior through command line arguments is a potent tool, and this is the reason, why it is widely used in the design of various libraries.

However, a question arises: How can you extend this versatility to your unit tests when using Pytest? How can you dynamically adapt the behavior of your tests during their execution?

Read more »

There’s no doubt that Pytest fixtures are incredibly useful and help you write clean and maintainable tests.

But what if you want to do something more dynamic?

Maybe set up a database connection or pass different data inputs to each test case?

Setting up and tearing down identical fixtures with very minor changes leads to code repetition and maintenance nightmare.

Maybe you want to parameterize your fixtures?

Read more »

As a Python developer striving for accurate and efficient testing, you will likely encounter scenarios where verifying floating-point values or approximate comparisons presents challenges.

In many real-world applications, especially with scientific computing, simulations, high-performance computing, financial calculations, and data analysis, you’ll often deal with floating-point numbers.

These numbers are represented in computers using a finite number of binary digits, which can lead to rounding errors and precision limitations.

So what do you and how do you test these floating-point values?

Read more »

You’ve written code and Unit tests, and want to make sure it works. You simply run the pytest command in your terminal to run them the tests. Boom! some tests fail.

How do you debug it?

To debug, it’s sometimes helpful to run one test, run tests in a specific module or class, or run tests based on a marker.

But how do you run just a single test?

Read more »

In our fast-paced world, every millisecond matters and user experience is paramount.

The importance of faster code faster cannot be overstated.

Beyond correct functioning, it’s imperative to ensure that it operates efficiently and consistently across varying workloads.

This is where performance testing and benchmarking step in to uncover bottlenecks, inefficiencies, and regressions.

Read more »

Writing repeat code is likely one of the things you least enjoy. At least it’s the case for me.

In a world striving for time, cost and resource efficiency less is more.

How can we apply this same philosophy to Unit Testing?

We often need to reuse resources (like input data, database connections, shared objects and so on) across our Unit Tests.

Read more »

As a Python developer, you’re likely familiar with Pytest, the popular Unit Testing framework.

It’s a powerful tool to test your Python programs using easy and concise syntax, with a plethora of built-in functionality and Pytest plugins to enhance your testing experience.

Most developers use the CLI to run tests. But it’s actually possible (and easier) to run tests with just a single mouse click. You might be wondering, “Really? But how?”.

If you’re using VS Code then you can set it up in just a few minutes. Saving you countless hours in iterative development and testing time.

Read more »

Software testing is critical to your development process, ensuring your code works as expected.

As Python continues to gain popularity as a backend and scripting language, choosing the right testing framework becomes increasingly important.

Two prominent options in the Python ecosystem are Unittest and Pytest. Both frameworks provide powerful capabilities for writing and executing tests but differ in approach, features, and community support.

In this article, we delve into the comparison between Unittest vs Pytest, shedding light on their strengths, weaknesses, and distinctive features.

Read more »

While developing software, keeping track of events is crucial.

Logging helps you understand the execution flow of your code, to help catch bugs when they happen.

Although Pytest is great testing framework, it doesn’t automatically display the output of print statements or logs, which can be a problem when trying to debug failing tests or understanding flow.

So how do you go about logging and viewing events during Testing?

Can you override the default logging behavior set in the source code, just for testing?

What if you want to output logs to a file instead of the console?

Read more »

As software engineers, handling errors is an important part of code development.

How often do users behave unexpectedly? More often than not. In most respect, people, systems and the Universe are random.

Maybe your code expects user input or takes data from a received packet and transforms it, perhaps performing complex calculations.

Network delays, even reordering or corrupted data. Whatever be it, it’s good to plan for the worst.

Read more »

Javascript Object Notation (JSON) is arguably one of the most popular data exchange formats over the web.

Web services use serialisation to convert data from low-level data structures to JSON format that allows receiving services to easily deserialise it.

When writing Unit tests the need for testing JSON input and outputs is one of high importance.

Test data, API Responses and sometimes even config files are defined in JSON which makes it necessary to understand how to read and write to it, using Pytest.

Read more »

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