Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TDD Demo App

A professional C# demonstration of Test-Driven Development (TDD) principles using .NET 6.0 and xUnit testing framework.

Project Overview

This project demonstrates best practices in Test-Driven Development by implementing a text analysis library with comprehensive unit tests. It serves as an educational reference for developers learning TDD methodologies.

Features

  • Text Analysis: Word count functionality for analyzing text content
  • Comprehensive Testing: Full unit test coverage with xUnit
  • Modern .NET: Built on .NET 6.0 with latest language features
  • Professional Structure: Follows C# coding conventions and best practices
  • CI/CD Pipeline: Azure Pipelines integration for automated builds and tests
  • Documentation: XML documentation comments for IntelliSense support

Project Structure

UnitTestingDemo/
├── TDDDemoApp/                    # Main application project
│   ├── TextAnalyzer.cs           # Core text analysis logic
│   ├── Program.cs                # Console application entry point
│   └── TDDDemoApp.csproj        # Project configuration
├── TDDDemoAppTests/              # Unit test project
│   ├── TextAnalyzerTests.cs     # Comprehensive test suite
│   └── TDDDemoAppTests.csproj   # Test project configuration
├── azure-pipelines.yml           # CI/CD pipeline configuration
├── .editorconfig                 # Code style consistency rules
└── README.md                     # This file

Requirements

  • .NET SDK: 6.0 or later
  • Visual Studio: 2022 or later (optional, but recommended)
  • Git: For version control

Getting Started

Installation

  1. Clone the repository:

    git clone https://github.com/MaisamRazai/UnitTestingDemo.git
    cd UnitTestingDemo
  2. Verify .NET SDK installation:

    dotnet --version

Building the Solution

# Restore NuGet packages
dotnet restore

# Build the solution
dotnet build --configuration Release

# or for debug
dotnet build --configuration Debug

Running the Application

# Run the console application
dotnet run --project TDDDemoApp/TDDDemoApp.csproj

The application will:

  1. Display a sample text analysis
  2. Prompt you to enter custom text for analysis
  3. Show the word count for each input
  4. Exit on "exit" command

Running Tests

# Run all tests
dotnet test

# Run tests with verbose output
dotnet test --verbosity detailed

# Run tests with code coverage
dotnet test /p:CollectCoverage=true

Usage

TextAnalyzer Class

The TextAnalyzer class provides a simple interface for analyzing text:

var analyzer = new TextAnalyzer();

// Get word count for a text string
int count = analyzer.GetTotalWordCount("Hello world! This is a test.");
// Returns: 6

GetTotalWordCount Method

/// <summary>
/// Gets the total word count in the provided text.
/// </summary>
/// <param name="text">The text to analyze. If null or empty, returns 0.</param>
/// <returns>The count of whitespace-separated words in the text.</returns>
/// <exception cref="ArgumentNullException">Thrown when text is null.</exception>
public int GetTotalWordCount(string text)

Parameters:

  • text - The input text to analyze (non-null)

Returns:

  • Integer count of words separated by whitespace

Exceptions:

  • ArgumentNullException - If text is null
  • Returns 0 for empty or whitespace-only strings

Code Quality

Principles Implemented

  1. Test-Driven Development: Tests written first, implementation follows
  2. Single Responsibility: Classes have one reason to change
  3. Clean Code: Following C# naming conventions and best practices
  4. Documentation: XML docs for public APIs
  5. Null Safety: Nullable reference types enabled
  6. Code Style: EditorConfig ensures consistency across the team

Testing Approach

The project includes comprehensive unit tests covering:

  • Happy path: Regular text analysis
  • Edge cases: Empty strings, whitespace-only input
  • Single word: Boundary condition
  • Multiple words: Standard usage
  • Null input: Exception handling

Testing Framework

  • xUnit: Modern, extensible testing framework for .NET
  • Microsoft.NET.Test.Sdk: Test execution engine
  • xunit.runner.visualstudio: Visual Studio integration

CI/CD Pipeline

The project uses Azure Pipelines for continuous integration and deployment.

Pipeline Features

  • Automatic builds on push to master and develop branches
  • Pull request validation
  • Automated test execution
  • Code coverage reporting
  • .NET 6.0 SDK installation

Pipeline Stages

  1. Restore: Download NuGet dependencies
  2. Build: Compile the solution
  3. Test: Execute unit tests with coverage
  4. Report: Publish test results and coverage metrics

Development Guidelines

Code Style

This project follows the C# Coding Conventions:

  • Namespaces: PascalCase (e.g., TDDDemoApp)
  • Classes: PascalCase (e.g., TextAnalyzer)
  • Methods: PascalCase (e.g., GetTotalWordCount)
  • Properties: PascalCase
  • Private Fields: camelCase with _ prefix (e.g., _analyzer)
  • Local Variables: camelCase

EditorConfig

The .editorconfig file enforces consistent formatting:

  • Indentation: 4 spaces for C#
  • Line endings: CRLF
  • Character encoding: UTF-8

Nullable Reference Types

Nullable reference types are enabled. All types must explicitly declare nullability:

string? nullableText = null;     // Can be null
string nonNullableText = "text"; // Cannot be null

Common Tasks

Running Tests in Visual Studio

  1. Open the solution in Visual Studio
  2. Go to TestTest Explorer
  3. Click Run All Tests

Debugging Tests

  1. In Test Explorer, right-click a test
  2. Select Debug Selected Tests

Building for Release

dotnet build --configuration Release --no-restore

Publishing

dotnet publish --configuration Release --output ./publish

Troubleshooting

Build Errors

Error: ".NET SDK not found"

Error: "Project file missing or incorrect"

  • Solution: Ensure project files (.csproj) exist and are properly formatted

Test Failures

Error: "Test project reference error"

  • Solution: Run dotnet restore to restore dependencies

Error: Namespace not found

  • Solution: Check that namespace declarations match folder/class names

Contributing

We welcome contributions! Please follow these guidelines:

  1. Create a feature branch: git checkout -b feature/my-feature
  2. Write tests first: Follow TDD principles
  3. Commit changes: Use clear, descriptive commit messages
  4. Push to branch: git push origin feature/my-feature
  5. Submit pull request: Describe your changes in detail

Code Review Checklist

  • Tests are included and passing
  • Code follows style guidelines
  • XML documentation is complete
  • No breaking changes (unless documented)
  • CI/CD pipeline passes

Performance

Text Analysis Performance

The TextAnalyzer uses efficient string splitting for word counting:

  • Time Complexity: O(n) where n is the length of the input string
  • Space Complexity: O(w) where w is the number of words
  • Optimized for: Medium to large text documents

Benchmarking

To run performance benchmarks (if implemented):

dotnet run --project TDDDemoApp --configuration Release -- --benchmark

Resources

License

This project is licensed under the MIT License. See the LICENSE file for details.

Authors

  • Maisam Razai - Initial implementation and TDD demonstration

Support

For issues, questions, or suggestions, please:

  1. Check existing GitHub issues
  2. Create a new issue with a clear description
  3. Include steps to reproduce for bugs
  4. Mention your .NET SDK version

Changelog

Version 1.0.0 (2024)

  • Initial project setup with TDD principles
  • TextAnalyzer implementation with comprehensive tests
  • Azure Pipelines CI/CD integration
  • Professional code structure and documentation
  • Upgraded to .NET 6.0
  • Added EditorConfig for code consistency

Last Updated: June 2024 Status: Active Development

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages