dotnet core

The how and why of Integration Tests .Net core 5 XUnit

The purpose of this post is to provide information and answer questions about integration tests. It covers topics such as the importance of integration tests, how to create and set up an XUnit project, how to recreate and seed a database, how to run integration tests in order, and how to mock calls to external APIs so that the integration tests do not rely on the availability of external systems.

Integration Tests

Integration tests are a type of software testing that verifies the interaction and communication between different components in a system. They are designed to test the integration of different parts of a system.

Why integration tests are important?

Integration tests are important because they test how different parts of a system work together and verify that components in the application, such as different classes or modules, work together as expected. This is different from unit tests, which test individual components in isolation.

Integration tests are particularly important when you are building a complex application with many different components that need to work together. By writing integration tests, you can verify that the different parts of the application are communicating and interacting correctly and that the application as a whole is working as intended.

The test pyramid

The test pyramid refers to the idea that, when writing tests, it is generally more efficient and effective to have a larger number of low-level unit tests and a smaller number of high-level end-to-end tests. This is because low-level unit tests are typically easier to write, work with, and maintain than high-level end-to-end tests. In addition, low-level unit tests are usually faster to execute than higher-level intergrain or end-to-end tests, which may require setting up a server or the entire system to run.

Integration tests, which are placed in the middle of the pyramid, cover some of the same areas as unit tests and some of the same areas as end-to-end tests. They are generally used to test the integration of different components or systems covering both the happy and unhappy paths. Unit tests, on the other hand, include tests for niche cases that integration tests may not cover.

Continue reading
dotnet core

Efficiently Modifying Lists: To Remove or Create a New One? .Net Core 5

When you have a big list and you need to remove items from that list.

Is it better to remove the items from that list or is it better to create a new list?

When you remove an item from a list, the list will shift all the elements after the removed item to the left to fill the gap. For example, if you have a list [1, 2, 3, 4, 5] and you remove the item at index 2 (which is the value 3), the list will become [1, 2, 4, 5]. The element at index 3 (which was previously the value 4) will now be at index 2, and so on.

This operation can be expensive, especially if the list is large and you are removing multiple items. It is usually more efficient to create a new list and add only the items that you want to keep, rather than removing items from an existing list.

On the other hand, creating a new list involves allocating a new block of memory and copying the elements that you want to keep into the new list. This can also be a time-consuming operation, but it is usually faster than removing items from a list, especially if you are only removing a small number of items.

Let’s take an example. I am reading a 10 MB Excel file that has 46 columns and 54,000 rows.

In the first method, I am passing the List of items and apply some filters. Based on those filters I am creating two lists. One list with valid records and another one with invalid records

Continue reading