Exploring testing AspNet web applications
Tags: aspnet, csharp, testing
Published: 2023-10-17 08:38:28

## Introduction

Testing lies at the heart of ensuring the functionality, reliability, and security of any software application, and ASP.NET web applications are no exception. In this series of articles, we delve into the intricate world of testing ASP.NET web applications using C#. From unit tests that scrutinize individual components to end-to-end tests ensuring the seamless operation of the entire application, we explore various testing approaches and methodologies.

## Understanding the Goal

Imagine you have an ASP.NET web application, and you're faced with the challenge of testing it comprehensively. How do you ensure its logic is flawless, its data remains coherent, and its user interactions are seamless? This series aims to answer these questions and more, providing insights into different aspects of testing, including:

Automated Tests at Different Scales: We'll discuss unit tests, integration tests, and end-to-end tests. Each type has its unique purpose, allowing us to validate different layers of our application's functionality.

Testing Code with External Dependencies: Real-world applications often rely on external services and databases. We'll explore strategies to test code that interacts with these dependencies, ensuring our tests remain robust and independent.

Manual Testing: While automated tests are invaluable, some scenarios require the human touch. We'll explore when and how manual testing fits into the overall testing strategy.

## Setting the Context: The Application

In the articles to follow, our focus will be on testing the application logic and database access of an ASP.NET web application. This application comprises:

An interactive website provides the user interface through which users interact with the application. The website communicates with a backend via a mostly json API. I'd like to say REST, but let's be real, most of the time it's more like RPC.

A backend in Modern C# with ASP.NET: The application logic is predominantly written in C#, employing services organized using ASP.NET dependency injection. However some logic resides in SQL Views and Stored Procedures. A bad choice? Maybe, but we don't always get to choose.

Database Storage: Data is stored in SQL Server using EntityFramework 7. While EntityFramework simplifies database access, it presents challenges in terms of testing, especially when it comes to mocking.

Application Settings: Any sufficiently complex application probably has a few settings stored in the database. Even a TODO application might have different types of tasks with different workflows defined in the database.

## Key Considerations

Testing is not one-size-fits-all; it involves making various decisions and trade-offs based on project requirements and goals. A project may have several test suites with different trade-offs. In this series, we explore different testing styles and goals, considering factors such as:

* *Speed:* It is important, especially in Test-Driven Development (TDD) scenarios, to have tests that run fast, giving feedback quickly. Such tests, must probably be written to focus on individual components, validating their behavior without relying on external dependencies. The trade-off is how far you are willing to go to mock external dependencies. These tests are often called "Unit tests" because they test parts in isolation.

* *System coverage:* Testing parts in isolation while mocking their dependencies only gets you so far. To ensure comprehensive coverage, we delve into integration tests that assess how these components interact when combined. Integration tests provide a more holistic view of the application, examining the integration points where potential issues might arise. True system coverage may require communicating with services outside of the application, such as a real database. The trade-off lie in how far you go to provide outside services equivalent to the production environment, or how far you go to replace them with mocks and/or fakes.

* *Reliability and Stability:* Beyond the functionalities, a robust application must be reliable and stable under various conditions. Load testing, stress testing, and performance testing become crucial. These tests assess how the application behaves under different loads and stress levels, ensuring it can handle real-world usage scenarios without compromising on speed and responsiveness.

* *Monitoring:* No matter how completely we have tested the application logic and reliability, things will get messed up eventually. Monitoring probably ought to be sufficient to debug and fix the problems easily. The trade-offs to be explored include how much detail you want to collect and store.

* *Maintainability:* As the project evolves, tests need to evolve too. Writing maintainable tests involves considering their longevity and ease of modification. Test suites should be adaptable, allowing developers to refactor code confidently without the fear of breaking existing functionalities. Balancing thorough testing with the ability to make changes to the codebase is essential for long-term project health.

In this series, we'll explore these key considerations in depth, offering insights into crafting test suites that strike the right balance between speed, coverage, reliability, and maintainability. By understanding these fundamental principles, you'll be better equipped to make informed decisions tailored to your project's unique requirements.

## Conclusion

Testing ASP.NET web applications involves a delicate balance of various testing approaches, each serving a specific purpose. In the articles that follow, we will explore various approaches. Some may be good, some may be crazy. This is exploration, not a thesis. The goal is not to explain the one true way, but to learn.