Skip to content

rysjones/XUnitPresentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

73 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

XUnit Presentation - Comprehensive Unit Testing Guide

πŸ“š Documentation

Comprehensive guide covering:

  • What is unit testing and its benefits
  • Introduction to XUnit framework
  • Why choose XUnit over other frameworks
  • XUnit core concepts (Facts, Theories, Fixtures)
  • Introduction to Moq mocking framework
  • Moq core concepts and advanced features
  • Practical examples and best practices
  • Advanced testing scenarios

Quick reference for:

  • XUnit attributes and assertions
  • Moq setup and verification syntax
  • Test organization patterns
  • Command line test execution
  • Best practices summary

🎯 Topics Covered

Core Concepts

  • What is unit testing? - Understanding the fundamentals and benefits
  • What is XUnit and why should we use it? - Modern testing framework advantages
  • How to mock external dependencies using Moq? - Isolation and control in testing

Advanced Topics

  • Async/await testing patterns
  • Test data management and builders
  • Custom assertions and exceptions
  • Test fixtures and collections
  • Parameterized testing with Theory
  • Test organization and lifecycle management
  • Mocking strategies and verification patterns

πŸ“ Project Structure

πŸ§ͺ UnitTest - XUnit Demonstration Project

Core XUnit features and patterns:

Services

  • MathService.cs - Basic arithmetic operations for testing fundamentals
  • AsyncMathService.cs - Async operations testing examples
  • CacheService.cs - Stateful service for fixture demonstrations
  • CounterService.cs - Shared state testing with class fixtures
  • OrderService.cs - Complex service with multiple dependencies (Moq examples)

Tests

  • SumUnitTests.cs - Basic [Fact] testing
  • DivisionUnitTests.cs - [Theory] with [InlineData] examples
  • MathUnitTests.cs - Constructor/Dispose lifecycle, custom assertions
  • CounterUnitTests.cs - Class fixtures, test ordering with priorities
  • CacheKeyOneUnitTests.cs & CacheManyKeysUnitTests.cs - Collection fixtures
  • AsyncMathServiceTests.cs - NEW Comprehensive async testing patterns
  • OrderServiceTests.cs - NEW Integration testing with Moq
  • AdvancedXUnitFeaturesTests.cs - NEW Advanced XUnit features showcase

Advanced Features

  • CustomAssert.cs - Custom assertion creation
  • PriorityOrderer.cs - Custom test execution ordering
  • Test fixtures and collections for shared setup
  • Trait-based test categorization

🎭 MoqDemo - Moq Mocking Framework Demonstration

Core Application (DemoConsoleApp)

  • PayloadService.cs - Service layer with database operations
  • KeyVaultManager.cs - Azure Key Vault integration service
  • SQLiteDataAccess.cs - Data access layer
  • Supporting infrastructure (wrappers, configurations)

Test Projects (DemoConsoleApp.UnitTests)

  • PayloadServiceTests.cs - IMPROVED Better structured service testing
  • KeyVaultManagerTests.cs - Basic mocking examples
  • DataAccessTests.cs - Data layer testing with mocks
  • ImprovedKeyVaultManagerTests.cs - NEW Advanced mocking patterns
  • ImprovedDataAccessTests.cs - NEW Comprehensive data access testing
  • ComprehensiveMoqExamples.cs - NEW Complete Moq feature demonstration

Moq Features Demonstrated

  • Basic mock setup and verification
  • Argument matching (It.IsAny, It.Is, It.IsInRange, It.IsRegex)
  • Return values and dynamic behaviors
  • Exception handling and async method mocking
  • Callbacks and parameter capture
  • Property mocking and verification
  • Sequential behavior setup
  • Strict mocks and protected method mocking
  • Verification patterns and timing constraints

πŸš€ Getting Started

Prerequisites

  • .NET 6.0 or later
  • Visual Studio 2022 / VS Code / JetBrains Rider

Running the Tests

Command Line

# Navigate to a test project
cd UnitTest
# or
cd MoqDemo

# Run all tests
dotnet test

# Run with detailed output
dotnet test --logger "console;verbosity=detailed"

# Run specific test categories
dotnet test --filter "Category=Unit"
dotnet test --filter "Category=Cache"

# Run specific test class
dotnet test --filter "FullyQualifiedName~MathUnitTests"

Visual Studio

  1. Open the solution file (.sln)
  2. Build the solution
  3. Use Test Explorer to run tests
  4. Right-click on specific tests to run/debug

VS Code

  1. Install the .NET Core Test Explorer extension
  2. Open the workspace folder
  3. Use the Test Explorer panel or Command Palette

Test Categories and Filters

UnitTest Project

# Run cache-related tests
dotnet test --filter "Category=Cache"

# Run high-priority tests
dotnet test --filter "Priority=High"

# Run specific test classes
dotnet test --filter "FullyQualifiedName~AsyncMathServiceTests"

MoqDemo Project

# Run improved tests
dotnet test --filter "FullyQualifiedName~Improved"

# Run comprehensive examples
dotnet test --filter "FullyQualifiedName~Comprehensive"

πŸ› οΈ Key Improvements Made

Enhanced Test Coverage

  • Added async/await testing patterns
  • Comprehensive Moq usage examples
  • Advanced XUnit features demonstration
  • Real-world service integration testing

Better Test Organization

  • Test builders for complex object creation
  • Improved setup and teardown patterns
  • Clear separation of concerns in test classes
  • Consistent naming conventions

Practical Examples

  • Database integration testing with in-memory databases
  • External service mocking (Key Vault, Email, Payment processing)
  • Error handling and exception testing
  • Performance and timeout testing scenarios

Documentation Improvements

  • Step-by-step learning progression
  • Code samples with detailed explanations
  • Best practices and anti-patterns
  • Quick reference for daily use

πŸ“ Learning Path

  1. Start with UnitTestingGuide.md for comprehensive understanding
  2. Explore basic examples in UnitTest/Tests/SumUnitTests.cs
  3. Progress to parameterized testing in DivisionUnitTests.cs
  4. Learn test lifecycle in MathUnitTests.cs
  5. Understand fixtures in cache tests
  6. Master async testing in AsyncMathServiceTests.cs
  7. Practice mocking with MoqDemo examples
  8. Reference XUnit-Moq-QuickReference.md for syntax help

πŸŽ“ Advanced Concepts Demonstrated

XUnit Advanced Features

  • Custom test orderers and priority attributes
  • Collection definitions and shared fixtures
  • Theory data sources (MemberData, ClassData)
  • Custom assertions and exception types
  • Test output and debugging techniques
  • Conditional test execution and skipping

Moq Advanced Patterns

  • Argument matchers and custom conditions
  • Callback functions and parameter capture
  • Sequential behavior and state-based testing
  • Protected method mocking
  • Strict vs. loose mock behaviors
  • Verification strategies and timing

Testing Best Practices

  • Arrange-Act-Assert pattern consistency
  • Test isolation and independence
  • Meaningful test naming conventions
  • Appropriate use of test doubles
  • Error scenario coverage
  • Performance and boundary testing

🀝 Contributing

Feel free to add more examples, improve documentation, or suggest better testing patterns. The goal is to create a comprehensive resource for learning unit testing with XUnit and Moq.

βœ… Final Status

Current Test Results

  • UnitTest Project: 63 tests passed, 1 skipped βœ…
  • MoqDemo Project: 58 tests passed βœ…
  • Total Coverage: 121 successful tests

Key Accomplishments

  • βœ… Created comprehensive documentation and guides
  • βœ… Implemented modern mock-based testing with dependency injection
  • βœ… Demonstrated all major XUnit and Moq features
  • βœ… Applied best practices for testable code architecture
  • βœ… Fixed all compilation and test failures
  • βœ… Established patterns for maintainable test suites

Test Categories Successfully Implemented

  • Basic XUnit Features: Facts, Theories, Assertions
  • Advanced XUnit: Fixtures, Collections, Custom Assertions, Test Ordering
  • Async Testing: Async/await patterns and testing
  • Moq Fundamentals: Basic mocking and verification
  • Advanced Moq: Callbacks, Sequences, Argument Matching, Protected Methods
  • Architecture Patterns: Dependency injection, Interface-based design
  • Real-world Scenarios: Service layer testing, Data access mocking, Configuration management

This repository now serves as a comprehensive example of modern .NET unit testing best practices.

πŸ“œ License

This project is for educational purposes. Feel free to use the examples and patterns in your own projects.

About

XUnit Presentation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages