Unit Test & Deployment

Beginner Updated January 20, 2025

✅ Unit Testing in .NET Core

Testing is first-class in .NET Core using tools like xUnit or MSTest.

Create Test Project:

dotnet new xunit -n MyApp.Tests
dotnet add reference ../MyApp/MyApp.csproj

Sample Test:

public class GreetingServiceTests
{
    [Fact]
    public void Greet_ReturnsExpectedMessage()
    {
        var service = new GreetingService();
        Assert.Equal("Hello, Alice!", service.Greet("Alice"));
    }
}

🚀 Publishing and Deployment

You can publish your app as a self-contained executable:

dotnet publish -c Release -r win-x64 --self-contained true

This will bundle the .NET runtime with your app, so it runs even on machines without .NET installed.

.NET Core apps can be easily containerized with Docker:

FROM mcr.microsoft.com/dotnet/aspnet:7.0
COPY ./publish /app
WORKDIR /app
ENTRYPOINT ["dotnet", "MyApp.dll"]