Skip to main content

Posts

Showing posts with the label Testing

Using HTTP Files in VS Code with REST Client

I regularly switch between multiple IDEs, mostly VS Code and Visual Studio but sometimes also Rider or Cursor. One of the Visual Studio features I miss when using VS Code is the support for HTTP files. What if I told you that there is a way to use HTTP files in VS Code? Enter the REST Client extension for Visual Studio Code, a lightweight, powerful tool that lets you test APIs without ever leaving your editor. What is the REST client extension? Similar to the HTTP support in Visual Studio, the REST Client extension allows you to send HTTP requests and view responses directly in VS Code. It's minimal, scriptable, and integrates seamlessly into your existing workflow. It offers the same functionality as in Visual Studio and more:   .http and .rest file extensions support Syntax highlight (Request and Response)         Auto completion for method, url, header, custom/system variables, mime types and so on Comments (line ...

Testing your MCP server with Visual Studio HTTP Files

If you're building or testing Model Context Protocol (MCP) servers and you need a quick way to verify your endpoints are working correctly. Visual Studio's HTTP files ( .http ) provide an elegant, code-based approach to API testing What are Visual Studio HTTP files? HTTP files are plain text files with a .http or .rest extension that let you define and execute HTTP requests directly in your editor. They're supported in Visual Studio, Visual Studio Code (with the REST Client extension), and JetBrains IDEs. Think of them as executable documentation for your API. Why use HTTP files for MCP testing? The Model Context Protocol defines a JSON-RPC 2.0 interface for AI model interactions. Testing these endpoints traditionally meant using tools like Postman or curl commands, but HTTP files offer several advantages: Version control friendly : Store your test requests alongside your code Easy sharing : Team members can run the same tests instantly Fast iteration...

Use request chaining in HTTP files

By default every request inside your HTTP file is independent from any other request. But what if you want to use the output of one request as the input of another request? This is exactly what you can achieve using request variables. Creating resources with dependencies As an example I created a new ProjectController that we will use to: Create a new project Use the returned project id to create and assign a new task to this project I updated the http file with a new request to create the project. Notice that I included a createProject variable to name the request. I can now use this createProject variable in other requests. In our example I extract the project id value from the response: The following table describes the syntax in more details: Element Description requestVarName ( login in this case ) Request Variable which is being referenced. response|request Whether the value will be...

Use shared variables in HTTP files

Yesterday I explained how you can introduce environment specific variables when using .http files in Visual Studio. But now we need to repeat these variables for every environment even when they stay the same. In this post I show how to avoid this by introducing the special $shared environment. The $shared environment Visual Studio 2022 version 17.12 introduced the $shared environment, which is perfect for variables that should be available across all environments: You can now use these shared variables no matter which environment you have selected: More information Use .http files in Visual Studio 2022 | Microsoft Learn

Use environment specific variables in HTTP files

HTTP files provide a convenient way to test your API’s inside Visual Studio. In this post we'll look at a specific feature; the usage of environment-specific variables that let you seamlessly switch between environments without modifying your request files. Getting started with .http files Before diving into environment variables, let's understand the basics. When you create an ASP.NET Core project in Visual Studio 2022, you'll often find a .http file already in your solution. Here's a simple example: Variables are defined with @variableName = value and referenced using {{variableName}} . The ### delimiter separates multiple requests in a single file. Creating environment files The real power comes when you externalize these variables into environment files. Visual Studio supports two types of environment files: 1. http-client.env.json (Shared) This file contains environment configurations that are shared across your team and typically committed to sourc...

Migrating from XUnit v2 to v3–Troubleshooting

The XUnit team decided to do a major overhaul of the XUnit libraries and created completely new V3 packages. So don't expect backwards compatibility but a significant architectural shift that brings improved performance, better isolation, and modernized APIs to .NET testing. While the migration requires some work, the benefits should make it worthwhile for most projects. In this post I’ll share some of the migration problems I encountered and how to fix them. XUnit.Abstractions is not recognized This is an easy one. The XUnit.Abstractions has become an internal namespace and should no longer be referenced. Just remove any using Xunit.Abstractions statement from you code. No v3 version of Serilog.Sinks.XUnit After switching to the v3 version of the Xunit packages, I noticed that the old XUnit v2 version was still used somewhere causing the following compiler error: The type 'FactAttribute' exists in both 'xunit.core, Version=2.4.2.0, Culture=neutral, Publ...

Migrating from XUnit v2 to v3 – Getting started

The XUnit team decided to do a major overhaul of the XUnit libraries and created completely new V3 packages. So don't expect backwards compatibility but a significant architectural shift that brings improved performance, better isolation, and modernized APIs to .NET testing. While the migration requires some work, the benefits should make it worthwhile for most projects. Yesterday I talked about some of the features that I like in the new version. Today I want to walk you through the basic steps needed to migrate an existing V2 project to V3. Understanding the architectural changes Before diving into the migration steps, it's crucial to understand the fundamental changes in xUnit v3 that impact how you'll structure and run your tests From Libraries to Executables The most significant change in v3 is that test projects are now stand-alone executables rather than libraries that require external runners. This architectural shift solves several problems that plagued ...

Part VIII – Evaluations(continued)

I promised to continue my blog post from yesterday about Microsoft.Extensions.AI.Evaluation. Today we have a look at caching the responses and reporting. This post is part of a blog series. Other posts so far: Part I – An introduction to Microsoft.Extensions.AI Part II – ASP.NET Core integration Part III –Tool calling Part IV – Telemetry integration Part V – Chat history Part VI – Structured output Part VII -  MCP integration Part VIII – Evaluations Part VIII – Evaluations (continued) The example I showed yesterday was a simple example of how you can integrate LLM validation into your tests and check the relevance of the LLM response. However this is only one of the many metrics you typically want to check. A more realistic test scenario will evaluate a large range of metrics and as tests can be run quite frequently caching the responses of our LLM models will save us both money and time (as tests can run faster). Let’s update our prev...

Microsoft.Extensions.AI–Part VIII–Evaluations

Back from holiday with charged batteries, we continue our journey exploring the Microsoft.Extensions.AI library. Today we have a look at evaluating AI models. This post is part of a blog series. Other posts so far: Part I – An introduction to Microsoft.Extensions.AI Part II – ASP.NET Core integration Part III –Tool calling Part IV – Telemetry integration Part V – Chat history Part VI – Structured output Part VII -  MCP integration Part VIII – Evaluations What is Microsoft.Extensions.AI.Evaluation? Microsoft.Extensions.AI.Evaluation is a set of libraries with one common goal; simplifying the process of evaluating the quality and accuracy of responses generated by AI models. Measuring the quality of your AI apps is challenging, you need to evaluate metrics like: Relevance: How effective is the response for a given prompt? Truthfulness: Is the response factually correct? Coherence: Is the response logically structured and consiste...

Fixing integration test issues with Microsoft.AspNetCore.Mvc.Testing in .NET 9

When upgrading an ASP.NET Core application to .NET 9, I encountered the following error in my integration tests: System.InvalidOperationException: No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via StartupAssemblyKey in the web host configuration. I had updated my custom TestHost to switch from using a Startup.cs file to directly using the Program.cs file and the Minimal API approach. Therefore I added a partial Program.cs and updated the WebApplicationFactory class to use the Program.cs instead (more about this change in this post ). Here is the updated code: But this code didn’t work and resulted in the error message above. While giving the code a second look, I noticed that I was still referring to the Startup.cs that I didn't remove yet. I updated the code to use my Program.cs file instead: Doing that resulted in another error: A public method named ...

Monitor your A/B test in .NET

I’ m currently working on a new feature in one of our microservices. As this new feature could have large performance impact, we did some performance benchmarks up front through BenchMarkDotNet . The results looked promising but we were not 100% confident that these results are representative for real life usage. Therefore we decided to implement A/B testing. Yesterday I showed how to implement A/B testing in .NET using .NET Feature Management . Today I want to continue on the same topic and show you how we added telemetry. Measuring and analyzing results The most critical aspect of A/B testing is measuring results. You need to track relevant metrics for each variation to determine which performs better. The most simple way to do this is to fall back to the built-in logging in .NET Core : Although this is a good starting point, we can simplify and improve this by taking advantage of the built-in telemetry through OpenTelemetry and/or Application Insights. Using OpenTelemetry...

A/B testing in .NET using Microsoft Feature Management

I’ m currently working on a new feature in one of our microservices. As this new feature could have a large performance impact, we did some performance benchmarks up front through BenchMarkDotNet . The results looked promising but we were not 100% confident that these results are representative for real life usage. Therefore we decided to implement A/B testing. In this post, we'll explore what A/B testing is, why it matters, and how to implement it effectively using .NET Feature Management . What is A/B testing? A/B testing, also known as split testing, is a method of comparing two versions of a web page, application feature, or user experience to determine which one performs better. In its simplest form, you show version A to half your users and version B to the other half, then measure which version achieves your desired outcome more effectively. The beauty of A/B testing lies in its scientific approach. Rather than making changes based on opinions or assumptions, you...

Detecting breaking changes in your OpenAPI metadata

For the last 2 days I have been struggling with a breaking change I had in my ASP.NET Core web api that caused the consuming application to fail. I had a header parameter that was optional but became required after changing the nullability of my project to enabled . Although I found the issue and was able to fix it quite fast, I was not happy with my current process and was wondering how I could prevent this from happening again. This brought me to a final solution where I introduced some extra tests that compared the OpenAPI metadata between different implementations. Let me show you how I did it… Generate OpenAPI documents at build time To compare 2 OpenAPI metadata documents we first need to get them. For the already released version of the API, I can download the document through the OpenAPI endpoint ( /openapi/v1.json by default). But what about the new version of the API that is still in development? I solve this by generating the OpenAPI document at build time. This m...

Repeating a test multiple times in C#

In JUnit you have the @RepeatedTest annotation. This annotation allows you to run a single test method multiple times with different execution contexts. Unlike simply calling a method in a loop, each repetition is treated as a separate test execution with its own lifecycle. Although it can be useful to discover and investigate race conditions, I never had a good reason to start using this kind of functionality. But with the introduction of AI workloads inside my applications, times have changed. As AI output is less deterministic, it now starts to make sense to run the same test multiple times as the AI output could differ from test run to test run. Let me show you how to do this using NUnit and XUnit... NUnit: Built-in Repeat Attribute NUnit provides the most elegant solution with its built-in [Repeat] attribute: XUnit: Using Theory and Custom Attributes XUnit doesn't provide an out-of-the-box equivalent to the @RepeatedTest annotation but you can build your own ...

Property based testing - Updating FsCheck to version 3.x

f you have never heard about Property based testing, I would recommend to check my blog series about it first. But if you are too lazy to go through all these posts, here is a short definition: Property-based testing is a powerful testing methodology used in software development to verify that a system behaves correctly across a wide range of inputs. Instead of writing individual test cases with specific inputs and expected outputs, property-based testing defines properties —general rules that should always hold true for a given system. It serves as an alternative to example based testing where we focus on a set of example cases to validate the behavior of our code. There are a lot of libraries out there that help you write Property Based Tests. In .NET I mainly use FSCheck and CSCheck . As I’m doing more and more Python, I also start using Property Based Tests there through Hypothesis . In this post I’ll focus on FSCheck and how to update to the latest version as some breakin...

Why you should clean up your test directories

Today I lost a lot of time investigating a stupid(aren’t they all?) issue with some failing tests on the build server. The strange thing was that when I ran the same tests locally, they always succeeded...what was going wrong? Just for completeness, here is the test task configuration I was using: Nothing special I would think. It was only when diving deeper into the build output that I discovered what was going wrong. Here is the output that explains the problem: 2025-05-20T13:19:22.3855459Z vstest.console.exe 2025-05-20T13:19:22.3855564Z "D:\b\3\_work\210\s\IAM.Core.Tests\bin\Release\ net6.0\IAM.Core.Tests.dll " 2025-05-20T13:19:22.3855657Z "D:\b\3\_work\210\s\IAM.Core.Tests\bin\Release\ net8.0\IAM.Core.Tests.dll " 2025-05-20T13:19:22.3855761Z "D:\b\3\_work\210\s\Mestbank.Core.Tests\bin\Release\net6.0\Mestbank.Core.Tests.dll" 2025-05-20T13:19:22.3855857Z "D:\b\3\_work\210\s\Mestbank.Core.Tests\bin\Release\net8.0\Mestbank.Core.Tests....

C# - Set environment in unit tests

When monitoring our RabbitMQ usage, I noticed some strange behavior. After drilling a little bit deeper in our monitoring data, I discovered that the integration tests for one of our projects were running against our production environment! Whoops!! Luckily the impact was limited but that doesn't mean we don't have to fix it. The fix Let me show you how we did it… The unit test code was using the Microsoft.AspNetCore.Mvc.Testing library to create an in-memory test host to test an API. Microsoft.AspNetCore.Mvc.Testing is part of the ASP.NET Core ecosystem and aims to simplify integration testing by providing essential tools and setup. Here is the original code: There is nothing wrong with the code itself and it makes it very easy to write and end-to-end integration test against our web app: One of the api endpoints puts messages on a RabbitMQ queue to process them later. A test instance exists but as I mentioned at the beginning of this post, instead of using th...

XUnit–Improve type safety

While doing a code review, I discovered a feature in XUnit I didn't know it existed. Let me share what I discovered.  I've been used to specify data for my parameterised tests either using the [InlineData] attribute or through the [MemberData] or [ClassData] attributes. When using [MemberData] or [ClassData] , XUnit expects that you return an IEnumerable<object[]> as far I as I know. Here is an example: If I try to use a typed alternative, it results in a compiler error: However it turns out that there is a type safe alternative available through TheoryData<> . The TheoryData<> types provide a series of abstractions around the IEnumerable<object[]> required by theory tests. It consists of a TheoryData base class, and a number of generic derived classes TheoryData<> . It can be used in combination with both the [MemberData] or [ClassData] attributes while enforcing type safety. Here is our original example rewritten to use The...

Model based testing in C#

In my continuous journey to design and write better code I try and experiment with multiple testing techniques. A while ago I wrote a whole series of blog posts about Property based testing, an addition to the traditional example-based testing. A property based test is meant to be fairly succinct and verify that simple properties hold true for all possible inputs given a set of preconditions. Libraries like FSCheck and CSCheck by randomizing the inputs for a particular operation. But what if we need to test a more complicated scenario where it is not so easy to identify the separate properties of our system? This is where model based testing can help us. Instead of randomizing the input for one operation, we execute an arbitrary combination of operations against our system and compare it to a simplified model. Let me show you how to do this using CsCheck . I have created a small Counter with two operations: Increase() and Decrease(): We now define a “simplified” model ba...

Property based testing in C#–CsCheck

Almost a year ago I wrote a series of blog posts on how to use property-based tests in C#. Part 1 – Introduction Part 2 – An example Part 3 – Finding edge cases Part 4 – Writing your own generators Part 5 – Locking input In this series I used FsC https://fscheck.github.io/FsCheck/ heck as the library of my choice. Although originally created for F#, it also works for C# as I have demonstrated. However as it was originally created for F#, it sometimes feels strange when using FsCheck in C#. If you prefer a more idiomatic alternative, you can have a look at CsCheck , also inspired by QuickCheck but specifically created for C#. CsCheck offers no specific integration but can be used with any testing framework(XUnit, NUnit, MSTest, …). Here is a small example: CsCheck does it really well in the shrinking challenge and offers support for multiple types of tests including concurrency testing . This is a feature I really like as concurrency related issues ...