6 Unit-testing fundamentals

 

This chapter covers

  • Using SOLID design principles
  • Understanding the difference between facts and theories
  • Executing unit tests with the .NET command-line interface

Like many modern programming frameworks, .NET has built-in support for testing. In this chapter, we’ll cover the basics of writing and running unit tests. The dotnet command-line interface (CLI) tool has a command to initiate tests but doesn’t perform the tests on its own. Instead of forcing one test framework for all of .NET, the dotnet CLI tool looks for a test harness. The test harness usually comes from a package. Many test harnesses and frameworks are available, but in this chapter and beyond, we’ll use the xUnit framework for testing. Your company may use a different framework, but many of the strategies and concepts should translate.

6.1 Writing code that’s easier to test

6.2 SOLID principles

6.2.1 S: Single responsibility principle

6.2.2 O: Open/closed principle

6.2.3 L: Liskov substitution principle

6.2.4 I: Interface segregation principle

6.2.5 D: Dependency inversion principle

6.3 An example test application: Sodoku

6.4 Building your first xUnit test project

6.5 Fact tests

6.6 Theory tests

6.6.1 Applying SOLID principles to SudokuSolver

6.6.2 Testing for exceptions

6.6.3 Theory testing with MemberData

Summary