Call Us: +32 2 466 00 16
Email: info@u2u.be
Follow Us:

Effective Unit Testing

2 days
UTEST
2 days

Upcoming Sessions

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Book now

Interested in a private company training? Request it here.

Unit testing in .NET

Every developer aspires to write code that is not only clean but also functional. However, achieving code that is easily maintainable and consistently operational poses a significant challenge. How does one navigate the complexities of software development to ensure that the code remains robust and reliable in the long run? In this context, we will explore the role of unit testing in verifying the correctness of our code and ensuring its continued functionality..

  • What every developer wants: Quality code that works; and keeps on working
  • Finding bugs: not just in code. - Or how NASA lost a 125.000.000$ Mars orbiter
  • What is unit testing? And what is a good unit test?
  • Understanding the difference between a unit test and an integration test
  • Test Driven Development - Should you be doing it?
  • Role-playing: Marge, Homer and Bart
  • The Triple-A of unit testing - and what has cooking to do with this?
  • Given-When-Then

Unit testing with Visual Studio with MSTest and xUnit

When it comes to Unit Testing, you will need to choose frameworks. There are several options available, so which one do you need? Visual Studio comes with a built-in framework called MSTest, but you also have others, such as the popular xUnit. In this module, we will discuss the pros and cons of these frameworks and delve into some best practices. How can you determine if you have tested enough? Code coverage allows you to see which paths in your code you have tested and easily identify untested conditional logic.

  • MSUnit - Built-in into Visual Studio
  • Building and running Unit Tests with MSUnit
  • Using the Test Explorer Window
  • Using Test Settings
  • Live unit testing with Visual Studio
  • Unit Testing best practices with xUnit
  • Facts and Theories
  • Running Unit tests in parallel, or not
  • Fluent Assertions
  • Measuring and improving Code Coverage
  • LAB: Implementing a library with testing

More loose coupling using the art of Dependency Injection

When writing code, it's crucial to minimize unnecessary dependencies on other objects. Dependencies can increase maintenance overhead, reduce testability, and limit flexibility. If you must rely on another object, consider depending on an interface. Interfaces describe the dependency without enforcing specific implementations. However, you should avoid hardcoding these dependencies. This is where dependency injection (DI) comes in handy. Dependency injection allows you to dynamically determine which implementations to inject into dependencies at runtime, providing greater flexibility and decoupling within your codebase.

  • What is tight coupling and how to prevent it?
  • Using Inversion of Control (IoC) containers.
  • Constructor and Property injection.
  • Microsoft.Extensions.DependencyInjection as an example.

Writing testable code

If you aim to make your code testable, you must design it with testing in mind. In this course, you'll discover the concepts of stubs and mocks, and you'll master techniques for substituting dependencies that could disrupt your testing process.

  • Testing dependencies - and the art of writing testable code
  • The difference between a Fake, a Stub and a Mock
  • How to replace dependencies with stub and mock objects
  • The Extract and Override pattern
  • How the MVVM pattern facilitates UI testing
  • Replacing configuration in tests
  • LAB: Use Extract and Override to test legacy code

Isolation Frameworks: NSubstitute and Microsoft Fakes.

Building stubs and mocks can be laborious without an Isolation Framework. NSubstitute is one such framework that simplifies the creation of fake and stub objects required for robust testing. But what about testing "untestable" code? Code where dependencies are tightly coupled to the class under test? For such scenarios, you need a bit of magic: Microsoft Fakes. This tool enables you to substitute any class's implementation, easing the testing of legacy and otherwise challenging-to-test code.

  • Understanding Isolation Frameworks
  • Building Stubs and Mocks with NSubstitute
  • Checking arguments and return values
  • What makes Microsoft Fakes so special
  • Faking time with TimeProvider and ITimer
  • Testing legacy/untestable code
  • Building Stubs and Mocks with Fakes
  • LAB: Using NSubstitute

Unit Testing ASP.NET Core Web Applications

When creating modern web applications with ASP.NET Core MVC, unit testing the Controllers and Views isn't straightforward. How does one validate whether the ActionResult returned by the Controller action is the expected one, and whether the View is correct? How do we replace dependencies during testing with MVC? How do we perform end-to-end testing?

  • Challenges when unit testing MVC applications
  • Writing unit test for your MVC controllers
  • Unit testing your views
  • Unit testing ApiControllers
  • Faking data access
  • Using the ASP.NET Core testing framework
  • End-to-end testing with Microsoft Playwright
  • LAB: Implementing integration and end-to-end tests

Automatic Regression Testing

So your software works! Great! But how do you know if it will work next week, or next month? Did you break something while adding a new cool feature? Test everything again? Of course, but now we will automate it, making a machine do it automatically every time a team member makes a change.

  • What is a regression?
  • Using an automated build system
  • Continuous integration
  • Running integration tests
  • Automatic regression tests
  • Automatically running your regression tests using GitHub Actions

Writing solid, maintainable code that works is the goal of every developer. This course guides you into the art of unit testing, where you learn to build testable code and various techniques to ensure its quality. By the end of this course, you will gain a solid and practical understanding of unit testing and its real-life applications. U2U is renowned for its hands-on approach to training, and each chapter is accompanied by a practical lab.

This course is intended for experienced programmers who are very familiar with C# and have a working experience with .NET 6/8. All examples and labs utilize the latest LTS version of .NET and Visual Studio.

Contact Us
  • Address:
    U2U nv/sa
    Z.1. Researchpark 110
    1731 Zellik (Brussels)
    BELGIUM
  • Phone: +32 2 466 00 16
  • Email: info@u2u.be
  • Monday - Friday: 9:00 - 17:00
    Saturday - Sunday: Closed
Say Hi
© 2024 U2U All rights reserved.