Update: The examples in this post were written in 2021 using .NET 5 and IdentityServer4, both of which are now out of support. Alba is still actively maintained and the testing approach shown here applies to current versions of ASP.NET Core, though newer versions of Alba use AlbaHost in place of the SystemUnderTest class in the code below. See the Alba documentation for current setup, and Duende IdentityServer for IdentityServer4’s successor.
Setting Up the Projects
I started two new .Net projects:
- A new web service that hosts IdentityServer4 and is configured to use user membership information from our client’s existing Marten/Postgresql database.
- A new xUnit.Net project to hold integration tests against the new IdentityServer4 web service.
Building the Test Harness With Alba and xUnit.Net
Let’s dive right into how I set up Alba and xUnit.Net as an automated test harness for our new IdentityServer4 service. If you start a new ASP.Net project with one of the built-in project templates, you’ll get a Program file that’s the main entry point for the application and a Startup class that has most of the system’s bootstrapping configuration. The templates will generate this method that’s used to configure the IHostBuilder for the application:

For more information on the role of the IHostBuilder within your application, see .NET Generic Host in ASP.NET Core.
That’s important, because that gives us the ability to stand up the application exactly as it’s configured in an automated test harness. Switching to the new xUnit.Net test project, I referenced my new web service project that will host IdentityServer4. Because spinning up your ASP.Net system can be relatively expensive, I only want to do that once and share the IHost between tests. That’s a perfect usage for xUnit.Net’s shared context support.
First, I make what will be the shared test fixture context class for the integration tests shown below:

The Alba SystemUnderTest wrapper is responsible for building the actual IHost object for your system, and does so using the in memory TestServer in place of Kestrel.
Just as a convenience, I like to create a base class for integration tests I tend to call Integration Context.

We’re using Lamar as the underlying IoC container in this application, and I wanted to use Lamar-specific IoC diagnostics in the tests, so I expose the main Lamar container off the base class as just a convenience.
Testing the Discovery Endpoint
To finally turn to the tests, the very first thing to try with IdentityServer4 was just to hit the descriptive discovery endpoint just to see if the application was bootstrapping correctly and IdentityServer4 was functional at all. I started a new test class with this declaration:

And then a new test just to exercise the discovery endpoint:

The test above is pretty crude. All it does is try to hit the /.well-known/openid-configuration url in the application and see that it returns a 200 OK HTTP status code.
I tend to run tests while I’m coding by using keyboard shortcuts. Most IDEs support some kind of “re-run the last test” keyboard shortcut. Using that, my preferred workflow is to run the test once, then assuming that the test is failing the first time, work in a tight cycle of making changes and constantly re-running the test(s). This turned out to be invaluable as it took me a couple iterations of code changes to correctly re-create the old IdentityServer3 configuration into the new IdentityServer4 configuration.
Testing Simple Authentication
Moving on to doing a simple authentication, I wrote a test like this one to exercise the system with known credentials:

Why Iterating Through Tests Paid Off
Now, this test took me several iterations to work through until I found exactly the right way to configure IdentityServer4 and adjusted our custom Marten backing identity store (IResourceOwnerPasswordValidator and IProfileService in IdentityServer4 world) until the tests pass. I found it extremely valuable to be able to debug right into the failing tests as I worked, and even needed to take advantage of JetBrains Rider’s capability to debug through external code to understand how IdentityServer4 itself worked. I’m sure that I was able to get through this work much faster by iterating through tests as opposed to just trying to run the application and driving it through something like Postman or through the connected user interface.