C# and .NET Core Test Driven Development

Book description

Learn how to apply a test-driven development process by building ready C# 7 and .NET Core applications.

About This Book
  • Create tests to quickly detect and resolve issues when writing portable code
  • Uncover code integration issues that improve code quality using continuous integration
  • Set up and use data-driven unit testing to verify your code
Who This Book Is For

This book is for .NET developers who would like to build efficient applications by implementing principles of test-driven development. C# programming and working knowledge of VS is assumed.

What You Will Learn
  • Write flexible, maintainable, and verifiable code for .NET Core
  • Write testable code using SOLID principles and dependency injections
  • Recognize the characteristics of a good unit test
  • Structure and group your unit test
  • Use mock objects to handle dependencies
  • Set up an end-to-end continuous integration process
In Detail

This book guides developers to create robust, production-ready C# 7 and .NET Core applications through the practice of test-driven development process.

In C# and .NET Core Test-Driven Development, you will learn the different stages of the TDD life cycle, basics of TDD, best practices, and anti-patterns. It will teach you how to create an ASP.NET Core MVC sample application, write testable code with SOLID principles and set up a dependency injection for your sample application. Next, you will learn the xUnit testing framework and learn how to use its attributes and assertions. You'll see how to create data-driven unit tests and mock dependencies in your code. You will understand the difference between running and debugging your tests on .NET Core on LINUX versus Windows and Visual Studio. As you move forward, you will be able to create a healthy continuous integration process for your sample application using GitHub, TeamCity, Cake, and Microsoft VSTS.

By the end of this book, you will have learned how to write clean and robust code through the effective practice of TDD, set up CI build steps to test and build applications as well as how to package application for deployment on NuGet.

Style and approach

The book explores the concepts of test driven development in depth so readers can apply these proven techniques to build sophisticated software with C# and .NET.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. C# and .NET Core Test-Driven Development
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Exploring Test-Driven Development
    1. Difficulty in maintaining code
    2. How does bad code appear? 
      1. Tight coupling
      2. Code smell
      3. Bad or broken designs
      4. Naming the program elements
      5. Source code readability
      6. Poor source code documentation
      7. Non-tested code
    3. What we can do to prevent bad code
      1. Loose coupling
      2. Sound architecture and design
      3. Preventing and detecting code smell
      4. C# coding conventions
      5. Succinct and proper documentation
      6. Why test-driven development?
      7. Building for longevity
    4. The principles of test-driven development
      1. Origin of TDD
      2. TDD misconceptions
      3. Benefits of TDD
      4. Types of tests
        1. Unit tests
        2. Integration tests
        3. System testing
        4. User acceptance testing
      5. Principles of TDD
        1. Writing the tests
        2. Writing the code
        3. Running the tests
        4. Refactoring
      6. Doing TDD the wrong way
    5. The TDD cycle
    6. Summary
  8. Getting Started with .NET Core
    1. .NET Core framework
      1. .NET Standard
      2. .NET Core components
      3. Supported languages
      4. When to choose .NET Core over .NET Framework
        1. Cross-platform requirements
        2. Ease of deployment
        3. Scalability and performance
      5. Limitations of .NET Core
    2. Structure of a .NET Core application
      1. ASP.NET Core MVC project structure
        1. wwwroot folder
        2. Models, Views, and Controllers folders
        3. JSON files – bower.json, appsettings.json, bundleconfig.json
        4. Program.cs
        5. Startup.cs
    3. Tour of Microsoft's Visual Studio Code editor
      1. Installing .NET Core on Linux
      2. Installing and setting up Visual Studio Code on Linux
      3. Exploring Visual Studio Code
    4. A look at the new features of C# 7
      1. Tuples enhancement
      2. Out keyword
      3. Ref locals and returns
        1. Ref locals
        2. Ref returns
      4. Local function
      5. Patterns matching
      6. Digit separator and binary literal
    5. Creating an ASP.NET MVC Core application
    6. Summary
  9. Writing Testable Code
    1. Warning signs when writing untestable code
      1. Tight coupling
      2. Monster Constructor
      3. Classes with more than one responsibility
      4. Static objects
    2. Law of Demeter
      1. Train Wreck
    3. The SOLID architecture principles
      1. Single Responsibility Principle
      2. Open-Closed Principle 
      3. Liskov Substitution Principle
      4. Interface Segregation Principle
      5. Dependency Inversion Principle
    4. Setting up a DI container for ASP.NET Core MVC
    5. Summary
  10. .NET Core Unit Testing
    1. The attributes of a good unit test
      1. Readable
      2. Unit independence
      3. Repeatable
      4. Maintainable and runs fast
      5. Easy to set up, non-trivial, and with good coverage
    2. Unit testing framework ecosystem for .NET Core and C#
      1. .NET Core testing with MSTest
      2. .NET Core testing with NUnit
      3. xUnit.net
        1. How to configure xUnit.net
        2. xUnit.net test runners
        3. Test parallelism
    3. Unit testing consideration for ASP.NET MVC Core
      1. Unit testing controllers
      2. Unit testing razor pages
    4. Structuring unit tests with xUnit
    5. xUnit.net shared test context
    6. Live unit testing with Visual Studio 2017 Enterprise
    7. Proving unit test results with xUnit.net assertions
    8. The test runners available on both .NET Core and Windows
      1. ReSharper
    9. Summary
  11. Data-Driven Unit Tests
    1. The benefits of data-driven unit testing
      1. Tests brevity
      2. Inclusive testing
    2. xUnit.net theory attribute for creating data-driven tests
    3. Inline data-driven unit tests
    4. Property data-driven unit tests
      1. MemberData attribute
      2. ClassData attribute
    5. Integrating data from other sources
      1. SqlServerData attribute
      2. Custom attribute
    6. Summary
  12. Mocking Dependencies
    1. The benefits of mocking objects
      1. Fast running tests
      2. Dependencies isolation
      3. Refactoring legacy code
      4. Wider test coverage
    2. The shortcomings of mocking frameworks
      1. Interface explosion
      2. Extra complexity
      3. Mock explosion
    3. Hand-rolling mocks versus using a mocking framework
      1. Mocking concept
      2. Benefits of hand-rolling mocks
      3. Mocks and stubs
      4. Hand-rolled mock
    4. Mocking objects using Moq framework
      1. Mocking methods, properties, and callback
        1. Properties
        2. Matching parameters
        3. Events
        4. Callbacks
        5. Mock customization
          1. CallBase
          2. Mock repository
          3. Implementing multiple interfaces in a mock
      2. Verification method and property invocations with Moq
      3. LINQ to mocks
      4. Advanced Moq features
        1. Mocking internal types
    5. Summary
  13. Continuous Integration and Project Hosting
    1. Continuous integration
      1. CI workflow
        1. Single source code repository
        2. Build automation
        3. Automated tests
        4. Identical test and production environments
        5. Daily commit
      2. Benefits of CI
        1. Quick bugs detection
        2. Improved productivity
        3. Reduced risks
        4. Facilitating continuous delivery
      3. CI tools
        1. Microsoft Team Foundation Server
        2. TeamCity
        3. Jenkins
    2. Continuous delivery
      1. Benefits of continuous delivery
        1. Lower risks
        2. Quality software products
        3. Reduced costs
    3. GitHub online project hosting
      1. Project hosting
        1. Branching with GitHub Flow
          1. Pull request
          2. Reviewing changes and merging
    4. Basic Git commands
      1. Configuration commands
      2. Initializing repository commands
      3. Change commands
      4. Branching and merging commands
    5. Configuring GitHub WebHooks
      1. Consuming WebHooks
      2. GitHub WebHook
        1. Events and payloads
        2. Setting up your first WebHook
    6. TeamCity CI platform
      1. TeamCity concepts
      2. Installing TeamCity Server
      3. TeamCity CI workflow
      4. Configuring and running build
    7. Summary
  14. Creating Continuous Integration Build Processes
    1. Installing the Cake Bootstrapper
      1. Installation
        1. PowerShell security
        2. Cake Bootstrapper installation steps
          1. Step 1
          2. Step 2
          3. Step 3
    2. Writing build scripts in C#
      1. Task
      2. TaskSetup and TaskTeardown
      3. Configuration and preprocessor directives
      4. Dependencies
      5. Criteria
      6. Cake's error handling and finally block
      7. LoanApplication build script
    3. Cake Extension for Visual Studio
      1. Cake templates
      2. Task Runner Explorer
      3. Syntax highlighting
    4. Using Cake tasks to build steps
    5. CI with Visual Studio Team Services
      1. Setting up a project in VSTS
      2. Installing Cake into VSTS
      3. Adding a build task
    6. Summary
  15. Testing and Packaging the Application
    1. Executing xUnit.net tests with Cake
      1. Executing xUnit.net tests in .NET projects
      2. Executing xUnit.net tests in .NET Core projects
    2. .NET Core versioning
      1. Versioning principle
      2. Installer
      3. Package manager
      4. Docker
      5. Semantic Versioning
    3. .NET Core packages and metapackages
      1. Metapackage
      2. Microsoft.AspNetCore.All metapackage
    4. Packaging for NuGet distribution
      1. dotnet publish command
      2. Creating a NuGet package
    5. Summary
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: C# and .NET Core Test Driven Development
  • Author(s): Ayobami Adewole
  • Release date: May 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788292481