.NET Testing Best Practices - Rob Richardson - NDC Oslo 2024

Learn crucial .NET testing practices with Rob Richardson at NDC Oslo 2024, covering patterns, frameworks, dependency injection, mocking, and CI/CD integration.

Key takeaways
  • Use the Arrange-Act-Assert pattern to structure tests in a clear and consistent way

  • Favor dependency injection to make code more testable by allowing mocks and fakes to be injected for testing

  • Consider using enums instead of strings/magic values to make code more type-safe and reduce errors

  • Take advantage of testing frameworks like xUnit, Fluent Assertions and Moq to write cleaner and more maintainable tests

  • Integrate test results into CI/CD pipelines using standard formats like JUnit XML reports

  • Use test parameters and theories instead of multiple similar test methods to reduce test code duplication

  • Debug tests just like regular code to understand failures and validate test behavior

  • Extract interfaces and use mocks to isolate the system under test from its dependencies

  • Run tests automatically after builds to catch issues early

  • Write tests to both validate functionality and discover features/behaviors of the system

  • Make test names descriptive and follow consistent naming patterns to clearly identify what’s being tested

  • Refactor production code when needed to make it more testable, while using tests to ensure nothing breaks

  • Use tools like Fluent Assertions to write more readable and maintainable assertions