Skip to content

Repository files navigation

.NET Unit Testing with NUnit, xUnit & MSTest

A comprehensive reference repository for unit testing in .NET Core using NUnit, xUnit, and MSTest — with Moq for mocking dependencies. The repo contains two sample projects of increasing complexity, each fully tested across all three frameworks so you can compare patterns and syntax side by side.


📁 Projects Overview

🔷 Sparky — Core Testing Fundamentals

A simple class library that serves as a sandbox for learning and comparing testing frameworks. Each framework has its own dedicated test project.

Project Framework
Sparky Source library (Customer, BankAccount, etc.)
SparkyMSTest MSTest test project
SparkyNUnitTest NUnit test project
SparkyXUnit xUnit test project

Concepts covered:

  • Basic assertions and test structure ([Test], [Fact], [TestMethod])
  • Parameterized tests ([TestCase], [Theory] + [InlineData])
  • Exception testing
  • Collection assertions
  • Setup and teardown lifecycle ([SetUp], constructor-based)
  • Mocking with Moq — Setup, Returns, Verify
  • Fibonacci series and grading calculator examples
  • BankAccount deposit/withdrawal tests

🔶 Bongo — Real-World N-Tier Web Application

A full ASP.NET Core MVC booking application structured with a proper N-tier architecture, tested at every layer.

Project Purpose
Bongo.Core.Tests Business logic / service layer tests
Bongo.DataAccess.Tests Data access layer tests with EF Core in-memory DB
Bongo.Models.Tests Model validation tests
Bongo.Web.Tests / Bongo.Tests.Web Controller / MVC action tests

Concepts covered:

  • Mocking repositories and services with Moq
  • EF Core in-memory database for integration-style tests
  • Controller unit testing (action results, view names, model state)
  • Testing exceptions and return codes
  • Organizing tests by layer in an N-tier architecture
  • [TestCase] / [InlineData] for data-driven tests across layers

🧰 Technology Stack

  • .NET 6 / .NET Core
  • NUnit 3[Test], [TestCase], [SetUp], [OneTimeSetUp]
  • xUnit[Fact], [Theory], [InlineData], constructor-based setup
  • MSTest[TestMethod], [DataRow], [TestInitialize]
  • Moq — mocking library for interfaces and dependencies
  • ASP.NET Core MVC (Bongo web app)
  • Entity Framework Core with in-memory provider

🚀 Getting Started

Prerequisites

  • .NET 6 SDK or later
  • Visual Studio 2022, VS Code (with C# Dev Kit), or JetBrains Rider

Clone & Run

git clone https://github.com/you22efAtya/NUnit-XUnit.git
cd NUnit-XUnit

Open Sparky.sln in your IDE, or run all tests from the terminal:

dotnet test

To run tests for a specific framework only:

# NUnit only
dotnet test SparkyNUnitTest/

# xUnit only
dotnet test SparkyXUnit/

# MSTest only
dotnet test SparkyMSTest/

# Bongo — all layers
dotnet test Bongo.Core.Tests/
dotnet test Bongo.DataAccess.Tests/
dotnet test Bongo.Models.Tests/
dotnet test Bongo.Web.Tests/

🔍 Framework Comparison at a Glance

Feature MSTest NUnit xUnit
Test attribute [TestMethod] [Test] [Fact]
Parameterized [DataRow] [TestCase] [Theory] + [InlineData]
Setup [TestInitialize] [SetUp] Constructor
Teardown [TestCleanup] [TearDown] IDisposable
Class setup [ClassInitialize] [OneTimeSetUp] IClassFixture<T>
Assert library Assert Assert / Is Assert

📐 Test Structure — AAA Pattern

All tests in this repository follow the Arrange → Act → Assert pattern:

[Test]
public void Customer_WithSilverLoyaltyPoints_ShouldGetDiscount()
{
    // Arrange
    var customer = new Customer { LoyaltyPoints = 100 };

    // Act
    var discount = customer.GetDiscount();

    // Assert
    Assert.That(discount, Is.EqualTo(10));
}

About

A comprehensive reference repository for unit testing in .NET Core using NUnit, xUnit, and MSTest — with Moq for mocking dependencies. The repo contains two sample projects of increasing complexity, each fully tested across all three frameworks so you can compare patterns and syntax side by side.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages