xUnit BeforeAfterTestAttribute: How to Run Code Before And After Test

Sometimes we need to run a piece of custom code before and after a test and set of tests. One example of this might be when we want to change the thread culture. In this case we change it before test start to run and restore it to its original after test is finished. One thing we can do is to use BeforeAfterTestAttribute to do it. So in this post I’m going to show you how you can use this attribute and we’ll see some example that demonstrate the specific of how it can be used.

Creating the BeforeAfterTestAttribute Attribute

First thing we need to do it to create an attribute that inherit from BeforeAfterTestAttribute. Then we need to over ride the Before and After method and place the code that we need to run there. Here’s a very simple example.

This is a simple example with only a bare bone of what’s needed to be done. Here’s a more practical example from Brad Wilson’s xunit sample repository.

In above example, we use it to change the culture before test and restore it back to normal after it. Notice also that before and after method has a parameter of type MethodInfo. We can use this parameter to ask information about the running method of possibly change how the method behave.

Now that we saw how we can build an attribute that run code for our before and after test, let’s see how we can use it.

Using the Custom BeforeAfterTestAttribute  Atrribute

To run a piece of code before and after a test, the only thing we need to do is to decorate the class or method with the attribute we just built.

Here’s what happens after we decorate our method and run the test.

It also worth to note that we could run this attribute on a single method or an entire class with sets of methods.

Summary

In this post I explained what BeforeAfterTestAttribute is and how it can help us. We saw how we can use it to run a piece of code before and after a test got executed.

Share...
 

Hamid Mosalla

Hi, I'm Hamid ("Arman") a software engineer and tech lead with more than 11 years of experience building applications with C# and .NET. My background spans backend development, APIs, application security, and modernizing existing systems. Much of that work comes down to understanding how a system behaves and making changes without creating problems elsewhere. These days, my focus also includes AI engineering: deploying open-weight models, building agentic applications, and connecting models to tools and business data through the Model Context Protocol (MCP). I’m interested in the practical questions behind RAG, local inference, and agent workflows: how to give a model useful context, control what it can do, and evaluate whether it’s doing the job well. On this blog, I share technical walkthroughs and things I learn while working through those questions. Expect a mix of .NET, software design, and applied AI, with code examples and enough detail to try things yourself.

 

One thought on “xUnit BeforeAfterTestAttribute: How to Run Code Before And After Test”

Leave a Reply

Your email address will not be published. Required fields are marked *