liveBook · Manning

Test

As explained in .NET in Action, Second Edition

Testing is the systematic evaluation of software behavior against expected functionality and correctness.

Testing checks software behavior against requirements and builds confidence that code works as intended. Developers share responsibility for testing and maintaining the code they ship, especially when applications are delivered rapidly. section 6.1

Unit-testing practice in .NET centers on:

  • Testing individual methods with quick-running tests that avoid external dependencies where possible. section 6.1
  • Designing maintainable, testable code with SOLID principles; replacing complex dependencies with mocks or fakes can simplify tests. section 6.2 section 7.0
  • Using xUnit facts for tests without inputs and theories for sets of inputs; assertions can check equality or verify that exceptions are thrown. section 6.7
Listing 6.17 Theory test for board sizes
[Theory]
[InlineData(8, false)]                     #1
[InlineData(9, true)]
public void EmptyBoardSizes(int size, bool isValid)
{
  if (!isValid)
  {
    Assert.Throws<ArgumentException>(      #2
      "size",                              #3
      () => new ArrayBoard(size));         #4
  }
  else
  {
    _ = new ArrayBoard(size);              #5
  }
}

A theory test checks board sizes using multiple inputs and asserts that an invalid size throws an ArgumentException. section 6.6.2

How .NET in Action, Second Edition covers it

How deep it goes
Works from individual methods through xUnit facts, theories, input data, equality assertions, and exception checks.
Examples
Tests a Sudoku solver and checks that invalid board sizes throw an ArgumentException.
What you can do after
Apply via xUnit to write tests for inputs, expected results, and exceptions.
How it connects
Connects testing to SOLID design, dependency injection, mocks, fakes, and repository abstractions.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.