A professional C# demonstration of Test-Driven Development (TDD) principles using .NET 6.0 and xUnit testing framework.
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.
- 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
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
- .NET SDK: 6.0 or later
- Visual Studio: 2022 or later (optional, but recommended)
- Git: For version control
-
Clone the repository:
git clone https://github.com/MaisamRazai/UnitTestingDemo.git cd UnitTestingDemo -
Verify .NET SDK installation:
dotnet --version
# Restore NuGet packages
dotnet restore
# Build the solution
dotnet build --configuration Release
# or for debug
dotnet build --configuration Debug# Run the console application
dotnet run --project TDDDemoApp/TDDDemoApp.csprojThe application will:
- Display a sample text analysis
- Prompt you to enter custom text for analysis
- Show the word count for each input
- Exit on "exit" command
# Run all tests
dotnet test
# Run tests with verbose output
dotnet test --verbosity detailed
# Run tests with code coverage
dotnet test /p:CollectCoverage=trueThe 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/// <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
- Test-Driven Development: Tests written first, implementation follows
- Single Responsibility: Classes have one reason to change
- Clean Code: Following C# naming conventions and best practices
- Documentation: XML docs for public APIs
- Null Safety: Nullable reference types enabled
- Code Style: EditorConfig ensures consistency across the team
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
- xUnit: Modern, extensible testing framework for .NET
- Microsoft.NET.Test.Sdk: Test execution engine
- xunit.runner.visualstudio: Visual Studio integration
The project uses Azure Pipelines for continuous integration and deployment.
- Automatic builds on push to master and develop branches
- Pull request validation
- Automated test execution
- Code coverage reporting
- .NET 6.0 SDK installation
- Restore: Download NuGet dependencies
- Build: Compile the solution
- Test: Execute unit tests with coverage
- Report: Publish test results and coverage metrics
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
The .editorconfig file enforces consistent formatting:
- Indentation: 4 spaces for C#
- Line endings: CRLF
- Character encoding: UTF-8
Nullable reference types are enabled. All types must explicitly declare nullability:
string? nullableText = null; // Can be null
string nonNullableText = "text"; // Cannot be null- Open the solution in Visual Studio
- Go to Test → Test Explorer
- Click Run All Tests
- In Test Explorer, right-click a test
- Select Debug Selected Tests
dotnet build --configuration Release --no-restoredotnet publish --configuration Release --output ./publishError: ".NET SDK not found"
- Solution: Install .NET 6.0 SDK from dotnet.microsoft.com
Error: "Project file missing or incorrect"
- Solution: Ensure project files (.csproj) exist and are properly formatted
Error: "Test project reference error"
- Solution: Run
dotnet restoreto restore dependencies
Error: Namespace not found
- Solution: Check that namespace declarations match folder/class names
We welcome contributions! Please follow these guidelines:
- Create a feature branch:
git checkout -b feature/my-feature - Write tests first: Follow TDD principles
- Commit changes: Use clear, descriptive commit messages
- Push to branch:
git push origin feature/my-feature - Submit pull request: Describe your changes in detail
- Tests are included and passing
- Code follows style guidelines
- XML documentation is complete
- No breaking changes (unless documented)
- CI/CD pipeline passes
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
To run performance benchmarks (if implemented):
dotnet run --project TDDDemoApp --configuration Release -- --benchmarkThis project is licensed under the MIT License. See the LICENSE file for details.
- Maisam Razai - Initial implementation and TDD demonstration
For issues, questions, or suggestions, please:
- Check existing GitHub issues
- Create a new issue with a clear description
- Include steps to reproduce for bugs
- Mention your .NET SDK version
- 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