V4 was a huge release for Marten and there’s lots to talk about in the new release, but I want to start simply with just talking about Marten’s support for deleting documents. In all the examples, we’re going to use a User document type from our testing code.
Let’s say we have the identity of a User document that we want to delete in our Marten database. That code is pretty simple, it’s just this below:
internal Task DeleteByDocumentId(IDocumentSession session, Guid userId)
{
// Tell Marten the type and identity of a document to
// delete
session.Delete<User>(userId);
return session.SaveChangesAsync();
}
By default, Marten will do a “hard” delete where the actual database row in Postgresql is deleted and that’s all she wrote. Marten has long had support for “soft” deletes where the underlying rows are marked as deleted with a metadata column that tracks “is deleted” instead of actually deleting the row, so let’s opt into that by configuring the User document as soft-deleted like so:
// Configuring a new Marten document store
var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
opts.Schema.For<User>().SoftDeleted();
});
Or if you prefer, you can also use an attribute like so:
[SoftDeleted]
public class User
{
public Guid Id { get; set; }
// Other props we don't care about here
}
And I’ll show a 3rd way that’s new in Marten V4 later on.
The API to delete a User document is exactly the same, but the mechanics of what Marten is doing to the underlying Postgresql database have changed. Instead of deleting the underlying rows, Marten just marks an mt_deleted column as true. But now, think about this Linq query below where I’m searching for all the users that have the “admin” role:
using var session = store.QuerySession();
var admins = await session.Query<User>()
.Where(x => x.Roles.Contains("admin"))
.ToListAsync();
You’ll notice that I’m not doing anything to explicitly filter out the deleted users in this query, and that’s because Marten is doing that for you — and that’s the behavior you’d want most of the time. If for some reason you’d like to query for all the documents, even the ones that are marked as deleted, you can query like this:
var admins = await session.Query<User>()
.Where(x => x.Roles.Contains("admin"))
// This is Marten specific
.Where(x => x.MaybeDeleted())
.ToListAsync();
And now, if you only want to query for documents that are explicitly marked as deleted, you can do this:
var admins = await session.Query<User>()
.Where(x => x.Roles.Contains("admin"))
// This is Marten specific
.Where(x => x.IsDeleted())
.ToListAsync();
So far, so good? Now what if you change your mind about your deleted documents and you want to bring them back? Marten V4 adds the IDocumentSession.UndoDeleteWhere() method to reverse the soft deletes in the database. In the usage below, we’re going to mark every admin user as “not deleted”:
using var session = store.LightweightSession();
session.UndoDeleteWhere<User>(x => x.Roles.Contains("admin"));
await session.SaveChangesAsync();
But hold on, what if instead you really want to just wipe out the database rows with a good old fashioned “hard” delete instead? Marten V4 adds some IDocumentSession.HardDelete*****() APIs to do exactly that as shown below:
using var session = store.LightweightSession();
session
.HardDeleteWhere<User>(x => x.Roles.Contains("admin") && x.IsDeleted());
await session.SaveChangesAsync();
There are also single document versions of HardDelete() as well.
So back to the sample of querying all admin users both past and present:
var admins = await session.Query<User>()
.Where(x => x.Roles.Contains("admin"))
// This is Marten specific
.Where(x => x.MaybeDeleted())
.ToListAsync();
If you’re going to do that, it might be nice to be able to understand which users are marked as deleted, when they were marked deleted, and which users are not deleted. And it’d also be helpful if Marten would just tag the documents themselves with that metadata.
Enter the new Marten.Metadata.ISoftDeleted interface in V4. Let’s make our User document implement that interface as shown below:
public class User : ISoftDeleted
{
public Guid Id { get; set; }
// These two properties reflect Marten metadata
// and will be updated upon Delete() calls
// or when loading through Marten
public bool Deleted { get; set; }
public DateTimeOffset? DeletedAt { get; set; }
// Other props we don't care about here
}
If a document type implements the ISoftDeleted interface, the document type will automatically be treated as soft-deleted by Marten, and the Deleted and DeletedAt properties will be set at document load time by Marten.
I’m not showing it here, but you can effectively create the same behavior without the marker interface by using a fluent interface. Check out the Marten documentation for that approach.
Going into Marten I thought of the delete operation as something simple conceptually, but thousands of users means getting requests for more control over the process and that’s what we hope V4 delivers here.
Starting next week I’ll be doing some more deep dives into new Marten V4 improvements and some more involved sample usages.
Today I’m very excited to announce the official release of Marten V4.0! The Nugets just went live, and we’ve published out completely revamped project website at https://martendb.io.
This has been at least a two year journey of significant development effort by the Marten core team and quite a few contributors, preceded by several years of brainstorming within the Marten community about the improvements realized by this release. There’s plenty more to do in the Marten backlog, but I think this V4 release puts Marten on a very solid technical foundation for the long term future.
This was a massive effort, and I’d like to especially thank the other core team members Oskar Dudycz for answering so many user questions and being the champion for our event sourcing feature set, and Babu Annamalai for the newly improved website and all our grown up DevOps infrastructure. Their contributions over the years and especially on this giant release have been invaluable.
I’d also like to thank:
JT for taking on the nullability sweep and many other things
Ville Häkli might have accidentally become our best tester and helped us discover and deal with several issues along the way
Julien Perignon and his team for their patience and help with the Marten V4 shakedown cruise
Barry Hagan started the ball rolling with Marten’s new, expanded metadata collection
Raif Atef for several helpful bug reports and some related fixes
Simon Cropp for several pull requests and doing some dirty work
Kasper Damgård for a lot of feedback on Linq queries and memory usage
Adam Barclay helped us improve Marten’s multi-tenancy support and its usability
and many others who raised actionable issues, gave us feedback, and even made code contributions. Keeping in mind that I personally grew up on a farm in the middle of nowhere in the U.S., it’s a little mind-blowing to me to work on a project of this magnitude that at a quick glance included contributors from at least five continents on this release.
One of my former colleagues at Calavista likes to ask prospective candidates for senior architect roles what project they’ve done that they’re the most proud of. I answered “Marten” at the time, but I think I mean that even more now.
What Changed in this Release?
To quote the immortal philosopher Ferris Bueller:
The question isn‘t ‘what are we going to do’, the question is ‘what aren’t we going to do? ‘
We did try to write up a list of breaking changes for V4 in the migration guide, but here’s some highlights:
We generally made a huge sweep of the Marten code internals looking for every possible opportunity to reduce object allocations and dictionary lookups for low level performance improvements. The new dynamic code generation approach in Marten helped get us to that point.
We think Marten is even easier to bootstrap in new projects with improvements to the IServiceCollection.AddMarten() extensions
Marten supports System.Text.Json — but use that with some caution of course
The Linq support took a big step forward with a near rewrite and filled in some missing support for better querying through child collections as a big example. The Linq support is now much more modular and we think that will help us continue to grow that support. It’s a small thing, but the Linq parsing was even optimized a little bit for performance
Event Sourcing in Marten got a lot of big improvements that were holding up adoption by some users, especially in regards to the asynchronous projection support. The “async daemon” was completely rewritten and is now much easier to incorporate into .Net systems.
As a big user request, Marten supports much more options for tracking flexible metadata like correlation ids and even user defined headers in both document and event storage
Multi-tenancy support was improved
Soft delete support got some additional usability features
PLv8 adoption has been a stumbling block, so all the features related to PLv8 were removed to a separate add-on library called Marten.PLv8
The schema management features in Marten made some significant strides and should be able to handle more scenarios with less manual intervention — we think/hope/let’s just be positive for now
What’s Next for Marten?
Full point OSS releases inevitably bring a barrage of user reported errors, questions about migrating, possibly confusing wording in new documentation, and lots of queries about some significant planned features we just couldn’t fit into this already giant release. For that matter, we’ll probably have to quickly spin out a V5 release for .Net 6 and Npgsql 6 because there’s breaking changes coming due to those dependencies. OSS projects are never finished, only abandoned, and there’ll be a world of things to take care of in the aftermath of 4.0 — but for right now, Don’t Steal My Sunshine!.
We’re genuinely close to finally pulling the damn trigger on Marten V4. One of the last things I’m coding for the release is a new recipe for users to write very efficient web services backed by Marten database storage.
A Traditional .Net Approach
Before I get into the exact mechanics of that, let’s set the stage a little bit and draw some contrasts with a more traditional .Net web service stack. Let’s start by saying that in that traditional stack, you’re not using any kind of CQRS and the system state is persisted in the “one true model” approach using Entity Framework Core and an RDBMS like Sql Server. Now, in some web services you need to query data from the database and serve up a subset of that data into the outgoing HTTP response in a web service endpoint. The typical flow — with a focus on what’s happening under the covers — would be to:
ASP.Net Core receives an HTTP GET, finds the proper endpoint, and calls the right handler for that route. Not that it actually matters, but let’s assume the endpoint handler is an MVC Core Controller method
ASP.Net Core invokes a call to your DI container to build up the MVC controller object, and calls the right method for the route
You’ve been to .Net conferences and internalized the idea that an MVC controller shouldn’t get too big or do things besides HTTP mediation, so you’re delegating to a tool like MediatR. MediatR itself is going to go through another DI container service resolution to find the right handler for the input model, then invoke that handler
EF Core issues a query against your Sql Server database. If you’re needing to fetch data on more than just the root aggregate model, the query is going to be an outer join against all the child tables too
EF Core loops around in the database rows and creates objects for your .Net domain model classes based on its ORM mappings
You certainly don’t want to send the raw domain model on the wire because of coupling concerns, or don’t want every bit of data exposed to the client in a particular web service, so you use some kind of tool like AutoMapper to transform the internal domain model objects built up by EF Core into Data Transfer Objects (DTO) purposely designed to go over the wire.
Lastly, you return the outgoing DTO model, which is serialized to JSON and sent down the HTTP response by MVC Core
Sound pretty common? That’s also a lot of overhead. A lot of memory allocations, data mappings between structures, JSON serialization, and a lot of dictionary lookups just to get data out of the database and spit it out into the HTTP response. It’s also a non-trivial amount of code, and I’d argue that some of the tools I mentioned are high ceremony.
Now do CQRS!
I initially thought of CQRS as looking like a whole lot more work to code, and that’s not an uncommon impression when folks are first introduced to it. I’ve come to realize over time that it’s not really more work so much as it’s really just doing about the same amount of work in different places and different times in the application’s workflow.
Now let’s at least introduce CQRS into our application architecture. I’m not saying that that automatically implies using event sourcing, but let’s say that you are writing a pre-built “read side” model of the state of your system directly to a database of some sort. Now from that same web service I was describing before, you just need to fetch that persisted “read side” model from the database and spit that state right out to the HTTP response.
Now then, I’ve just yada, yada’d all the complexity of the CQRS architecture that continuously updates the read side view for you, but hey, Marten does that for you too and that can be a shortly forthcoming follow up blog post.
Finally bringing Marten V4 into play, let’s say our read side model for an issue tracking system looks like this:
public class Note
{
public string UserName { get; set; }
public DateTime Timestamp { get; set; }
public string Text { get; set; }
}
public class Issue
{
public Guid Id { get; set; }
public string Description { get; set; }
public bool Open { get; set; }
public IList<Note> Notes { get; set; }
}
Before anyone gets bent out of shape by this, it’s perfectly possible to tell Marten to serialize the persisted documents to JSON with camel or even snake casing to be more idiomatic JSON or Javascript friendly.
Now, let’s build out two controller endpoints, one that gives you an Issue payload by searching by its id, and a second endpoint that gives you all the open Issue models in a single JSON array payload. That controller — using some forthcoming functionality in a new Marten.AspNetCore Nuget — looks like this:
public class GetIssueController: ControllerBase
{
private readonly IQuerySession _session;
public GetIssueController(IQuerySession session)
{
_session = session;
}
[HttpGet("/issue/{issueId}")]
public Task Get(Guid issueId)
{
// This "streams" the raw JSON to the HttpResponse
// w/o ever having to read the full JSON string or
// deserialize/serialize within the HTTP request
return _session.Json
.WriteById<Issue>(issueId, HttpContext);
}
[HttpGet("/issue/open")]
public Task OpenIssues()
{
// This "streams" the raw JSON to the HttpResponse
// w/o ever having to read the full JSON string or
// deserialize/serialize within the HTTP request
return _session.Query<Issue>()
.Where(x => x.Open)
.WriteArray(HttpContext);
}
In the GET: /issue/{issueId} endpoint, you’ll notice the call to the new IQuerySession.Json.WriteById() extension method, and how I’m passing to it the current HttpContext. That method is:
Executing a database query against the underlying Postgresql database. And in this case, all of the data is stored in a single column in a single row, so there’s not JOINs or sparse datasets like there would be with an ORM querying an object that has child collections.
Write the raw bytes of the persisted JSON data directly to the HttpResponse.Body without ever bothering to write the whole thing to a .Net string and definitely without having to incur the overhead of JSON deserialization/serialization. That extension method also sets the HTTP content-length and content-type response headers, as well as setting the HTTP status code to 200 if the document is found, or 404 if the data is not found.
In the second HTTP endpoint for GET: /issue/open, the call to WriteArray(HttpContext) is doing something very similar, but writing the results as a JSON array.
By no means is this technique going to be applicable to every HTTP GET endpoint, but when it does, this is far, far more efficient and simpler to code than the more traditional approach that involves all the extra pieces and just has so much more memory allocations and hardware operations going on just to shoot some JSON down the wire.
For a little more context, here’s a test against the /issue/{issueId} endpoint, with a cameo from Alba to help me test the HTTP behavior:
[Fact]
public async Task stream_a_single_document_hit()
{
var issue = new Issue {Description = "It's bad", Open = true};
var store = theHost.Services.GetRequiredService<IDocumentStore>();
using (var session = store.LightweightSession())
{
session.Store(issue);
await session.SaveChangesAsync();
}
var result = await theHost.Scenario(s =>
{
s.Get.Url($"/issue/{issue.Id}");
s.StatusCodeShouldBeOk();
s.ContentTypeShouldBe("application/json");
});
var read = result.ReadAsJson<Issue>();
read.Description.ShouldBe(issue.Description);
}
Starting yesterday, all of my content about automated testing is curated under the new Automated Testing page on this site.
I kicked off a new blog series yesterday with Integration Testing: IHost Lifecycle with xUnit.Net. I started by just discussing how to manage the lifecycle of a .Net IHost inside of an xUnit.Net testing library. I used xUnit.Net because I’m much more familiar with that library, but we mostly use NUnit for our testing at MedeAnalytics, so I’m going to see how the IHost lifecycle I discussed and demonstrated last time in xUnit.NEt could work in NUnit.
To catch you up from the previous post, I have two projects:
An ASP.Net Core web service creatively named WebApplication. This web service has a small endpoint that allows you to post an array of numbers that returns a response telling you the sum and product of those numbers. The code for that controller action is shown in my previous post.
With NUnit, I chose to use the SetupFixture construct to manage and share the IHost for the test suite like this:
[SetUpFixture]
public class Application
{
// Make this lazy so you don't build it out
// when you don't need it.
private static readonly Lazy<IAlbaHost> _host;
static Application()
{
_host = new Lazy<IAlbaHost>(() => Program
.CreateHostBuilder(Array.Empty<string>())
.StartAlba());
}
public static IAlbaHost AlbaHost => _host.Value;
// I want to expose the underlying Lamar container for some later
// usage
public static IContainer Container => (IContainer)_host.Value.Services;
// Make sure that NUnit will shut down the AlbaHost when
// all the projects are finished
[OneTimeTearDown]
public void Teardown()
{
if (_host.IsValueCreated)
{
_host.Value.Dispose();
}
}
}
With the IHost instance managed by the Application static class above, I can consume the Alba host in an NUnit test like this:
public class sample_integration_fixture
{
[Test]
public async Task happy_path_arithmetic()
{
// Building the input body
var input = new Numbers
{
Values = new[] {2, 3, 4}
};
var response = await Application.AlbaHost.Scenario(x =>
{
// Alba deals with Json serialization for us
x.Post.Json(input).ToUrl("/math");
// Enforce that the HTTP Status Code is 200 Ok
x.StatusCodeShouldBeOk();
});
var output = response.ReadAsJson<Result>();
output.Sum.ShouldBe(9);
output.Product.ShouldBe(24);
}
}
And now a couple notes about what I did in Application:
I think it’s important to create the IHost lazily, so that you don’t incur the cost of spinning up the IHost when you might be running other tests in your suite that don’t need the IHost. Rapid developer feedback is important, and that’s an awfully easy optimization that could pay off.
The static Teardown() method is decorated with the `[OneTimeTearDown]` attribute to direct NUnit to call that method after all the tests are executed. I cannot stress enough how important it is to clean up resources in your test harness to ensure your ability to quickly iterate through subsequent test runs.
NUnit has a very different model for parallelization than xUnit.Net, and it’s completely “opt in”, so I think there’s less to worry about on that front with NUnit.
At this point I don’t think I have a hard opinion about xUnit.Net vs. NUnit, and I certainly wouldn’t bother switching an existing project from one to the other (even though I’ve certainly done that plenty of times in the past). I haven’t thought this one through enough, but I still think that xUnit.Net is a little bit cleaner for unit testing, but NUnit might be better for integration testing because it gives you finer grained control over fixture lifecycle and has some built in support for test timeouts and retries. At one point I had high hopes for Fixie as another alternative, and that project has become active again, but it would have a long road to challenge either of the two now mainstream tools.
What’s Next?
This series is meant to support my colleagues at MedeAnalytics, so it’s driven by what we just happen to be talking about at any given point. Tomorrow I plan to put out a little post on some Lamar-specific tricks that are helpful in integration testing. Beyond that, I think dealing with database state is the most important thing we’re missing at work, so that needs to be a priority.
I’m part of an initiative at work to analyze and ultimately improve our test automation practices. As part of that work, I’ll be blogging quite a bit about test automation starting with my brain dump on test automation last week and my most recent post on mocks and stubs last month. From here on out, I’m curating all of my posts and selected writings from other folks on my new Automated Testing page.
I’m already on record as saying that the generic host (IHost) in recent versions of .Net is one of the best things that’s ever happened to the .Net ecosystem. In my previous post I stated that I strongly prefer having the system under test running in process with the test harness for faster feedback cycles and easier debugging. The generic host builder introduced in .Net Core turns out to be a very effective way to bootstrap your system within automated test harnesses.
Before I dive into how to use the IHost in automated testing, here’s a couple issues I think you have to address in your integration testing strategy before we go willy nilly spinning up an IHost:
You ideally want to test against your code running in a realistic way, so the way code is bootstrapped and configured should be relatively close to how that code is started up in the real application.
There will inevitably need to be at least some configuration that needs to be different in testing or some services — usually accessing resources external to your system — that need to be replaced with stubs or some other kind of fake implementation.
It’s important to cleanly dispose or shutdown any IHost object you create in memory to avoid potential locks of resources like database connections, files, or ports. Failing to clean up resources in tests can easily make it harder to iterate through test fixes if you find yourself needing to manually kill processes or restart your IDE to release locked resources (been there, done that).
The IHost can be expensive to build up, and sometimes there’s going to be some serious benefit in reusing the IHost between tests to make the test suite run faster.
But the IHost is stateful, and there could easily be resources (singleton scoped services, databases, and whatnot) that could impact later test runs in the suite.
Before I jump into solutions, let’s assume that I have two projects:
WebApplication is an ASP.Net Core web service project. WebApplication uses Lamar as its underlying DI container.
A test project that references WebApplication
xUnit.Net Mechanics
I’m more comfortable with xUnit.Net, so I’m going to use that first. My typical usage is to share the IHost through xUnit.Net’s CollectionFixture mechanism (and if you think the usage of this thing is confusing, welcome to the club). First up, I’ll build out a new class I usually call AppFixture to manage the lifecycle of the IHost. The example project I’ve built here is an ASP.Net Core web service project, so I’m going to use Alba to wrap the host inside of AppFixture as shown below:
public class AppFixture : IDisposable, IAsyncLifetime
{
public IAlbaHost Host { get; private set; }
public async Task InitializeAsync()
{
// Program.CreateHostBuilder() is the code from the WebApplication
// that configures the HostBuilder for the system
Host = await Program
.CreateHostBuilder(Array.Empty<string>())
// This extension method starts up the underlying IHost,
// but Alba replaces Kestrel with a TestServer and
// wraps the IHost
.StartAlbaAsync();
}
public Task DisposeAsync()
{
return Host.StopAsync();
}
public void Dispose()
{
Host?.Dispose();
}
}
A couple things to note in that code above:
As we’ll set up next, that class above will be constructed once in memory by xUnit and shared between test fixture classes
The Dispose() and DisposeAsync() methods both dispose the IHost. By normal .Net mechanics, that will also dispose the underlying Lamar IoC container, which will in turn dispose any services created by Lamar at runtime that implement IDisposable. Disposing the IHost also stops any registered IHostedService services that your application may be using for long running tasks (for my colleagues who may be reading this, both NServiceBus and MassTransit start and stop their message listeners in an IHostedService, so that might be in use even if you don’t explicitly use that technique).
Next, we’ll set up AppFixture to be shared between our integration test classes by using the [CollectionDefinition] attribute on a marker class:
[CollectionDefinition("Integration")]
public class AppFixtureCollection : ICollectionFixture<AppFixture>
{
}
Lastly, I like to build out a base class for integration tests like this one:
[Collection("Integration")]
public abstract class IntegrationContext
{
protected IntegrationContext(AppFixture fixture)
{
theHost = fixture.Host;
// I am using Lamar as the underlying DI container
// and want some Lamar specific things later on
// in the tests
Container = (IContainer)fixture.Host.Services;
}
public IAlbaHost theHost { get; }
public IContainer Container { get; }
}
The [Collection] attribute is meaningful here because that makes xUnit.Net run all the tests that are contained in test fixture classes that inherit from IntegrationContext in a single thread so we don’t have to worry about concurrent test runs.*
And finally to bring this all together, let’s say that WebApplication has this simplistic web service code to do some arithmetic:
public class Result
{
public int Sum { get; set; }
public int Product { get; set; }
}
public class Numbers
{
public int[] Values { get; set; }
}
public class ArithmeticController : ControllerBase
{
[HttpPost("/math")]
public Result DoMath([FromBody] Numbers input)
{
var product = 1;
foreach (var value in input.Values)
{
product *= value;
}
return new Result
{
Sum = input.Values.Sum(),
Product = product
};
}
}
In the next code block, let’s finally see a test fixture class that uses the new IntegrationContext as a base class and tests the HTTP endpoint shown in the block above.
public class ArithmeticApiTests : IntegrationContext
{
public ArithmeticApiTests(AppFixture fixture) : base(fixture)
{
}
[Fact]
public async Task post_to_a_secured_endpoint_with_jwt_from_extension()
{
// Building the input body
var input = new Numbers
{
Values = new[] {2, 3, 4}
};
var response = await theHost.Scenario(x =>
{
// Alba deals with Json serialization for us
x.Post.Json(input).ToUrl("/math");
// Enforce that the HTTP Status Code is 200 Ok
x.StatusCodeShouldBeOk();
});
var output = response.ReadAsJson<Result>();
output.Sum.ShouldBe(9);
output.Product.ShouldBe(24);
}
}
Alright, at this point we’ve got a way to shared the system’s IHost in tests for better efficiency, and we’re making sure that all the resources in the IHost are cleaned up when the test suite is done. We’re using the WebApplication’s exact configuration for the IHost, but we still might need to alter that in testing. And there’s also the issue of needing to roll back state in our system between tests. I’ll pick up those subjects in my next couple posts, as well as using NUnit instead of xUnit.Net because that’s what the majority of code at my work uses for testing.
* It would be nice to be able to run parallel tests using our shared IHost, but that can often be problematic because of shared state, so I generally bypass test parallelization in integrated tests. The subject of parallelizing integration tests is worthy of a later blog post of some thoughts I haven’t quite elucidated yet.
I’m strictly talking about automated testing in this post. I’m more or less leading an effort at work to improve our test automation and Test Driven Development practices at work, so I’ll be trying to blog quite a bit about related topics in the next couple months. After reviewing quite a bit of in flight code, I think I’ll try to revisit some of my old blog posts on testability design from the CodeBetter days and update those old lessons from the early days of TDD to what we’re building now.
My company builds and maintains several long running software systems with a healthy back log of feature requests, performance improvements, and stories to retire technical debt. All that is to say that we’re constantly adding to or improving existing code — which implies we’re always running some non-zero risk of creating regression defects. To keep everybody’s stress levels down, we’re taking incremental steps toward a true continuous delivery model where we can smoothly and consistently build and deploy fully tested features while being confident that we aren’t introducing regression defects.
As you’d likely guess, we’re very interested in improving our automated testing practices as a safety net to enable continuous delivery while also improving our quality in general. That leads to the next question, what kind of automated testing should we be doing? Followed by, is there automated testing we’re doing today that isn’t delivering enough bang for the buck?
To that point, let’s take a look at the classic idea of the testing pyramid at some point, as shown below:
The thinking behind the testing pyramid is that there’s a certain, healthy mix of different sorts of automated tests that efficiently lead to better results. I say a “mix” here because unit tests, though relatively cheap compared to other tests, cannot detect many defects that only come out during integration between components, code modules, or systems. From a quick search, I found worlds of memes along the lines of this one:
No integration tests, but all the unit tests pass!
To address exactly what kind of automated tests we should be writing, here’s a stream of consciousness brain dump that I later organized with a patina of organization:
On End to End User Interface Tests
Any kind of test that uses a tool like Selenium to do end to end, black-box testing is going to run slowly. These tests are also frequently be unstable because of asynchronous timing issues in modern browser applications. There’s an unhealthy tendency in many shops who adopt Selenium as a test automation solution to use it as their golden hammer to the exclusion of other testing techniques that can be much more efficient in certain circumstances. To put it bluntly, it’s very difficult to successfully author and maintain test automation suites based on Selenium against complicated applications. In my experience in shops that have attempted large scale Selenium usage, I do not believe that the benefits of those tests have ever outweighed the costs.
I would still recommend using some small number of end to end tests with a tool like Selenium, but those tests should be focused on proving out integration mechanisms between a user interface and backing server side code. For example, I’ve been working on a new integration of Open Id Connect (OIDC) authentication into our web services and web applications. I’ve used Playwright to automate browser testing to prove out the interactions and redirects between the OIDC service and our applications or services.
I also find Cypress.io interesting, but more for doing integration testing of our Angular applications by themselves with a dummy backend. For true end to end testing of .Net-backed web applications, I think I’m interested in replacing Selenium from here on out with Playwright as I think and hope it just does much more for you to make performant and reliable automated tests compared to Selenium.
Driving a browser should not be used to automate functional testing of business logic or data services or any kind of data analysis that could possibly be tested without using the full browser.
If you insist on trying to do a lot of browser automation testing, you better invest in collecting diagnostic information in test runs that can be used by developers to debug test failures. Ideally, I like to have the application’s log output correlated to the test run somehow. I’ve worked with teams that were able to pipe the console.log() tracing from the JavaScript code running in the browser to the test results and that was extremely helpful. Taking screenshots as part of the test can certainly help. As another ideal, I very strongly recommend that any kind of browser automation tests be executable by developers on their local machine on demand for easier debugging. More on this later.
Again, if you absolutely have to write Selenium/Playwright/Cypress tests despite all of my warnings, I strongly recommend you write those tests in the same programming language as the real application. That statement is going to be controversial if any real test automation engineers stumble into this post, but I think it’s important to make it as easy as possible for developers to collaborate with test automation engineers. Moreover, I despise the kind of shadow data access layers you can get from test automation code doing their own thing to write to and read from the underlying data store of the system under test. I think it’s less likely to get that kind of insidious, hidden code duplication if the test code is written in the same programming language and even uses the system’s own data access code to set up or verify database state as part of the automated tests.
Choosing Solitary/Unit Tests or Sociable/Integration Tests
I missed out on this when it was first published, but I think I like the nomenclature of solitary vs sociable tests better than thinking about unit vs integration tests. I also encourage folks to think of that as a continuum rather than a hard categorization. Moreover, I recommend that you switch between solitary and sociable tests even within the same test library where one or the other is more effective.
I would recommend organizing tests by functional area first, and only consider separating out integration tests into a separate testing project when it’s advantageous to use a “fast test, slow test” division for more efficient development.
We have formal requirements for test coverage metrics in our continuous integration builds, so I’d definitely make sure that any integration tests count toward that coverage number.
I think an emphasis on always writing classical unit tests can easily create a strong coupling between the production code and your testing code. That can and will reduce your ability to evolve your code, add new functionality, or do performance optimizations without rewriting your tests.
In many cases, integration tests that start at a natural sub-system facade or a logical controller/conductor entry point will do much better for you as a regression safety net to allow you to refactor your code to allow for new behavior or do important performance optimizations.
Case in point, I relied strictly on fine grained unit tests in my early work in StructureMap, and I definitely felt the negative consequences of that approach (I gave a talk about it in 2008 that’s still relevant). With later releases of StructureMap and now with Lamar, I lean much more heavily on integrated acceptance tests (let’s go ahead and call it Behavior Driven Development) that test from the entry point of the library down and focus on user-centric scenarios. I feel like that testing approach has led to much better results — both in the ease of adding new features, detecting regression defects in automated builds, and allowing me to evolve the functionality of the library.
On the other hand, integration tests can be harder to troubleshoot when they fail because you have more ground to cover. They also run slower of course. If you find that your feedback cycles feel too slow to efficiently run the tests continuously or especially if you find yourself doing long, marathon sessions in your debugger, stop and consider introducing more fine-grained tests first.
Running the Tests Locally vs Remotely
To the previous point, I think it’s critical that developers should be able to easily spin up and run automated integration tests on demand on their local development boxes. Tests will fail, and being able to easily troubleshoot a failing test is a prerequisite for successful test automation. If you can run an integration test locally, you’re much more likely to be able to iterate and try potential fixes quickly. There’s also the very real possibility of attaching a debugger to the testing process.
I think that our current technology set makes it much easier to do integration testing than it was when I was first getting started and the strict Michael Feathers definition of a unit test was in vogue. Just speaking from my own experience, the current .Net 5 generation is very easy to spin up and down in process for automated testing. Docker has been a great way to stand up development environments using Sql Server, Postgresql, Rabbit MQ, and other infrastructural tools.
As a follow up to the previous section, it’s also advantageous for automated tests to be able to run in process with the test harness code. For instance, I’d much rather do Alba testing of HTTP endpoints in .Net 5 where I’m able to quickly spin up an actual web service in memory and shut it down from my testing project. As opposed to the old, full .Net framework where you’d have to run the web service project in IIS or IISExpress first, then use HttpClient to address the service from your unit tests. The first, .Net 5/Alba approach is a much faster iteration and feedback cycle to support a Test Driven Development workflow than the 2nd approach.
Likewise, when given a choice between tests that can be run locally versus tests that can only be executed in a remote server location, give me the local tests every time. When and if you hit a scenario where you really need to run tests remotely *cough* serverless *cough*, that’s the one and only exception I can think of to my “never deploy from your local development box” rule. If depending on remote execution of tests, I’d at least want the ability to send my local development branch to the remote server at will. Just having a CI server build out pull request branches might get you there of course, but then you might be dependent upon being about to run that test suite in parallel with other CI builds. That’s not a show stopper, but it might make you have to invest more in your build automation to spin up isolated environments on demand.
Apollo Testing!
There’s plenty of debate over what the actual ration of UI tests to integration tests to unit tests should be, and plenty of folks have different metaphors than a “pyramid” to describe what they thing the ratio should be. I happen to like the integration test heavy ratio described in The Testing Trophy and Testing Classifications with the graphic below:
So from now on, I’m calling our intended testing approach the Apollo Testing Method!
What is the purpose of testing?
I’m just barely old enough that I started my official software development career in old fashioned waterfall models. In those days we did some unit testing with ad hoc testing tools to troubleshoot new code, but it wasn’t anywhere close to what developers do today with Test Driven Development and xUnit tools. As developers, we mostly ran the complete application on our development boxes and stepped through things manually to check out new code locally before throwing things over the wall to QA at the end of the project.
Regardless of whatever ad hoc, local testing developers did of their code locally, the only official testing that actually counted was the purely manual testing done by our testers in the testing environment that was supposed to exactly mimic the production environment (it never quite did, but that’s a story for another day). The QA team strictly used black-box testing with some direct access to the underlying database.
That old black-box testing approach at the end of the project was a much slower feedback cycle than we’re accustomed to today after the advent of Agile Software Development. The killer problem was that the testing feedback cycles were too slow to consider evolutionary design approaches because of the real fear of regression failures. It was also harder as a developer to address defects found in the testing cycle because you were frequently needing to work with code you hadn’t touched in many months that certainly wasn’t fresh in your mind. That frequently led to marathon debugging sessions while a helpful project manager came by your cubicle to cheerfully ask for any updates several times a day and crank up the pressure. As a developer you were also completely at the mercy of QA for information about what was really happening in the system when they found bugs.
In my mind, the most important element of Agile software development overall was the emphasis on improving feedback cycles. Faster feedback allowed
Testing is not about proving that our code works perfectly so much as a way to find and remove enough problems from the code that it can be deployed to production. I think this is an important approach because it allows us to use faster, finer grained testing approaches like isolated unit testing or intermediate level white-box integration tests that are generally faster running and cheaper to build than classic black-box, end to end tests.
What’s Next?
My organization has started an effort to introduce much more integration testing into our development processes as a way of improving quality and throughput. To help out on that, I’m going to attempt to write a series of blog posts going into specific areas about tools and techniques, but for right now I’m just jotting down this stream of consciousness brain dump to get started.
Based on what I think we need to establish at work, I’m thinking to cover:
.Net IHost bootstrapping and lifecycle within xUnit.Net or NUnit. I’m much more familiar with xUnit.Net from recent development, but we mostly use NUnit at work so I’ll be trying to cover that base as well.
HTTP API testing, which will inevitably feature Alba
Dealing with databases in tests, and that’s gonna have to cover both RDBMS databases and probably Mongo Db for now
Message handler testing with MassTransit and NServiceBus (we use both in different products)
Another brain dump on doing end to end testing after a meeting today about the CI of one of our big systems.
How should automated testing be integrated into the development cycle, and who should be responsible for these tests, and why is the obvious answer a very close collaboration between testers, developers, and even business experts
This will be a bigger stretch for me, but maybe get into how to do some semi-integration testing of an Angular front end with NgRX.
After talking through some of our issues with test automation at work, I think I’d like to blog about some of the positive things we did with Storyteller. I’ve been increasingly frustrated with xUnit.Net (and don’t think NUnit would be much better) for integration testing, so I’ve got quite a bit of notes about what an alternative tool optimized for integration tests could look like I wouldn’t mind publishing.
EDIT 8/25: Make sure you’re using Oakton 3.2.2 if you try to reproduce this sample, because of course I found and fixed an Oakton bug in this functionality in the process of writing the post:(
I think that what I’m calling an “environment check” here is a very valuable, and under utilized technique in software development today. I had a good reason to stop and create one today to troubleshoot an issue I stubbed my toe on, so here’s a blog post about environment checks.
One of the things on my plate at work right now is prototyping out the usage of Identity Server 5 as our new identity provider across our enterprise. Today I was working with a spike that needed to run a pair of ASP.Net Core applications:
A web application that used the Duende.BFF library and JWT bearer tokens as its server side authentication mechanism.
The actual identity server application
All I was trying to do was to run both applications and try to log into secure portions of the web application. No luck though, I was getting weird looking exceptions about missing configuration. I eventually debugged through the ASP.Net Core OIDC authentication innards, and found out that my issue was that the web application was configured with the wrong authority Url to the identity server service.
That turned out to be a very simple problem to fix, but the exception message was completely unrelated to the actual error, and that’s the kind of problem that’s unfortunately likely to come up again as this turns into a real solution that’s separately deployed in several different environments later (local development, CI, testing, staging, production, you know the drill).
As a preventative measure — or at least a diagnostic tool — I stopped and added an environment check into the application just to assert that yes, it can successfully reach the configured OIDC authority at the configured Url we expect it to be. Mechanically, I added Oakton as the command line runner for the application. After that, I utilized Oakton’s facility for adding and executing environment checks to a .Net Core or .Net 5+ application.
Inside the Startup.ConfigureServices() method for the main web application, I have this code to configure the server-side authentication (sorry, I know this is a lot of code):
services.AddBff()
.AddServerSideSessions();
// This is specific to this application. This is just
// a cheap way of doing strong typed configuration
// without using the ugly IOptions<T> stuff
var settings = Configuration.Get<OidcSettings>();
// configure server-side authentication and session management
services.AddAuthentication(options =>
{
options.DefaultScheme = "cookie";
options.DefaultChallengeScheme = "oidc";
options.DefaultSignOutScheme = "oidc";
})
.AddCookie("cookie", options =>
{
// host prefixed cookie name
options.Cookie.Name = settings.CookieName;
// strict SameSite handling
options.Cookie.SameSite = SameSiteMode.Strict;
})
.AddOpenIdConnect("oidc", options =>
{
// This is the root Url for the OIDC authority
options.Authority = settings.Authority;
// confidential client using code flow + PKCE + query response mode
options.ClientId = settings.ClientId;
options.ClientSecret = settings.ClientSecret;
options.ResponseType = "code";
options.ResponseMode = "query";
options.UsePkce = true;
options.MapInboundClaims = true;
options.GetClaimsFromUserInfoEndpoint = true;
// save access and refresh token to enable automatic lifetime management
options.SaveTokens = true;
// request scopes
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("api");
// request refresh token
options.Scope.Add("offline_access");
});
The main thing I want you to note in the code above is that the Url for the OIDC identity service is bound from the raw configuration to the OidcSettings.Authority property. Now that we know where the authority Url is, formulating an environment check is this code, again within the Startup.ConfigureServices() method:
// This extension method registers an environment check with Oakton
services.CheckEnvironment<HttpClient>($"Is the expected OIDC authority at {settings.Authority} running", async (client, token) =>
{
var document = await client.GetDiscoveryDocumentAsync(settings.Authority, cancellationToken: token);
if (document == null)
{
throw new Exception("Unable to load the OIDC discovery document. Is the OIDC authority server running?");
}
else if (document.IsError)
{
throw document.Exception;
}
});
Now, if the authority Url is misconfigured, or the OIDC identity service is not running, I can quickly diagnose that issue by running this command from the main web application directory:
dotnet run -- check-env
If I were to run this command with the identity service is down, I’ll get this lovely output:
Stop, dogs, stop! The light is red!
Hopefully, I can tell from the description and the exception message that the identity service that is supposed to be at https://localhost:5010 is not responsive. In this case it was because I had purposely turned it off to show you the failure, so let’s turn the identity service on and try again with dotnet run -- check-env:
Go, dogs, go! It’s green ahead!
As part of turning this work over to real developers to turn this work into real, production worthy code, we’ll document the dotnet run -- check-env tooling for detecting errors. More effectively though, with Oakton the command line call will return a non-zero error code if the environment checks fail, so it’s useful to run this command in automated deployments as a “fail fast” check on the environment that can point to specifically where the application is misconfigured or external dependencies are unreachable.
As another alternative, Oakton can run these environment checks for you at application startup time by using the -c or --check flag when the application executable is executed.
Lamar is a modern IoC container library that is optimized for usage within .Net Core / .Net 5/6 applications and natively implements the .Net DI abstractions (AddTransient/Singleton/Scoped() etc). Lamar was specifically built to be a much higher performance version of the old StructureMap library with similar usage for relatively easy migration.
Lamar v6.0.0 went live today. It’s not a very large release at all, but I took this opportunity to tuck some previously public types into being internal to clean up your Intellisense and eliminated some obsolete public members. I don’t anticipate any changes in your code for the mass majority of Lamar users upgrading.
The highlights of this release are:
Lamar’s documentation website got a big facelift as it moved from an old Bootstrap theme with my old stdocs tool to using Vitepress and mdsnippets. I also did a sweep to remove old StructureMap nomenclature and replace that with the newer Lamar nomenclature that very purposely mimics the .Net DI abstraction terminology.
New functionality to reliably override service registrations at container creation time. This was specifically meant for automated integration testing where you may want to override specific service registrations. This hasn’t been a straightforward thing in the past because the generic host builder applies service registrations from Startup classes last no matter what, and that defeated many efforts to override services in testing.
A couple bug fixes, but I think this is the only big one.
I stopped and modernized the devops a bit to replace the old Rake build with Bullseye & SimpleExec. I also moved the CI from AppVeyor to GitHub actions. I wouldn’t say that either move was a big deal, but it is nice having the CI information right in the GitHub website.
Alba is a small library to facilitate easier integration testing of web services built on the ASP.Net Core platform. It’s more or less a wrapper around the ASP.Net TestServer, but does a lot to make testing much easier than writing low level code against TestServer. You would use Alba from within xUnit.Net or NUnit testing projects.
I outlined a proposal for Alba V5 a couple weeks back, and today I was able to publish the new Nuget. The big changes are:
The old SystemUnderTest type was renamed AlbaHost to make the terminology more inline with ASP.Net Core
The Json serialization is done through the configured input and output formatters within your ASP.Net Core application — meaning that Alba works just fine regardless of whether your application uses Newtonsoft.Json or System.Text.Json for its Json serialization.
We’re working toward standing up a new OIDC infrastructure built around Identity Server 5, with a couple gigantic legacy monolith applications and potentially dozens of newer microservices needing to use this new identity server for authentication. We’ll have to have a good story for running our big web applications that will have this new identity server dependency at development time, but for right now I just want to focus on an automated testing strategy for our newer ASP.Net Core web services using the Alba library.
First off, Alba is a helper library for integration testing HTTP API endpoints in .Net Core systems. Alba wraps the ASP.Net Core TestServer while providing quite a bit of convenient helpers for setting up and verifying HTTP calls against your ASP.Net Core services. We will be shortly introducing Alba into my organization at MedeAnalytics as a way of doing much more integration testing at the API layer (think the middle layer of any kind of testing pyramid concept).
In my previous post I laid out some plans and proposals for a quickly forthcoming Alba v5 release, with the biggest improvement being a new model for being able to stub out OIDC authentication for APIs that are secured by JWT bearer tokens (I think I win programming bingo for that sentence!).
Before I show code, I should say that all of this code is in the v5 branch of Alba on GitHub, but not yet released as it’s very heavily in flight.
To start, I’m assuming that you have a web service project, then a testing library for that web service project. In your web application, bearer token authentication is set up something like this inside your Startup.ConfigureServices() method:
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
// A real application would pull all this information from configuration
// of course, but I'm hardcoding it in testing
options.Audience = "jwtsample";
options.ClaimsIssuer = "myapp";
// don't worry about this, our JwtSecurityStub is gonna switch it off in
// tests
options.Authority = "https://localhost:5001";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("some really big key that should work"))
};
});
And of course, you also have these lines of code in your Startup.Configure() method to add in ASP.Net Core middleware for authentication and authorization:
app.UseAuthentication();
app.UseAuthorization();
With these lines of setup code, you will not be able to hit any secured HTTP endpoint in your web service unless there is a valid JWT token in the Authorization header of the incoming HTTP request. Moreover, with this configuration your service would need to make calls to the configured bearer token authority (http://localhost:5001 above). It’s going to be awkward and probably very brittle to depend on having the identity server spun up and running locally when our developers try to run API tests. It would obviously be helpful if there was a quick way to stub out the bearer token authentication in testing to automatically supply known claims so our developers can focus on developing their individual service’s functionality.
That’s where Alba v5 comes in with its new JwtSecurityStub extension that will:
Disable any validation interactions with an external OIDC authority
Automatically add a valid JWT token to any request being sent through Alba
Give developers fine-grained control over the claims attached to any specific request if there is logic that will vary by claim values
To demonstrate this new Alba functionality, let’s assume that you have a testing project that has a direct reference to the web service project. The direct project reference is important because you’ll want to spin up the “system under test” in a test fixture like this:
public class web_api_authentication : IDisposable
{
private readonly IAlbaHost theHost;
public web_api_authentication()
{
// This is calling your real web service's configuration
var hostBuilder = Program.CreateHostBuilder(new string[0]);
// This is a new Alba v5 extension that can "stub" out
// JWT token authentication
var jwtSecurityStub = new JwtSecurityStub()
.With("foo", "bar")
.With(JwtRegisteredClaimNames.Email, "guy@company.com");
// AlbaHost was "SystemUnderTest" in previous versions of
// Alba
theHost = new AlbaHost(hostBuilder, jwtSecurityStub);
}
I was using xUnit.Net in this sample, but Alba is agnostic about the actual testing library and we’ll use both NUnit and xUnit.Net at work.
In the code above I’ve bootstrapped the web service with Alba and attached the JwtSecurityStub. I’ve also established some baseline claims that will be added to every JWT token on all Alba scenario requests. The AlbaHost extends the IHost interface you’re already used to in .Net Core, but adds the important Scenario() method that you can use to run HTTP requests all the way through your entire application stack like this test :
[Fact]
public async Task post_to_a_secured_endpoint_with_jwt_from_extension()
{
// Building the input body
var input = new Numbers
{
Values = new[] {2, 3, 4}
};
var response = await theHost.Scenario(x =>
{
// Alba deals with Json serialization for us
x.Post.Json(input).ToUrl("/math");
// Enforce that the HTTP Status Code is 200 Ok
x.StatusCodeShouldBeOk();
});
var output = response.ResponseBody.ReadAsJson<Result>();
output.Sum.ShouldBe(9);
output.Product.ShouldBe(24);
}
You’ll notice that I did absolutely nothing in regards to JWT set up or claims or anything. That’s because the JwtSecurityStub is taking care of everything for you. It’s:
Reaching into your application’s bootstrapping to pluck out the right signing key so that it builds JWT token strings that can be validated with the right signature
Turns off any external token validation using an external OIDC authority
Places a unique, unexpired JWT token on each request that matches the issuer and authority configuration of your application
Now, to further control the claims used on any individual scenario request, you can use this new method in Scenario tests:
[Fact]
public async Task can_modify_claims_per_scenario()
{
var input = new Numbers
{
Values = new[] {2, 3, 4}
};
var response = await theHost.Scenario(x =>
{
// This is a custom claim that would only be used for the
// JWT token in this individual test
x.WithClaim(new Claim("color", "green"));
x.Post.Json(input).ToUrl("/math");
x.StatusCodeShouldBeOk();
});
var principal = response.Context.User;
principal.ShouldNotBeNull();
principal.Claims.Single(x => x.Type == "color")
.Value.ShouldBe("green");
}
I’ve got plenty of more ground to cover in how we’ll develop locally with our new identity server strategy, but I’m feeling pretty good about having a decent API testing strategy. All of this code is just barely written, so any feedback you might have would be very timely. Thanks for reading about some brand new code!