Chapter 4. Unit testing with xUnit
This chapter covers
- Executing unit tests with the .NET CLI
- Writing unit tests with xUnit
- The difference between facts and theories
- Logging test output
Testing is an essential part of writing great libraries and applications, and the first line of defense for testing is the unit test. In this chapter you’ll learn how to write unit tests in .NET Core, execute the tests, and add and collect logging data.
Unit tests are written against the smallest units in a software library, such as classes. A class should have responsibility over a single piece of functionality, and that responsibility should be entirely encapsulated by the class. This is the single responsibility principle, and it’s one of the SOLID software design principles.
SOLID software design
When developing a software application, it’s common to want to build all the pieces and test the software’s functionality as a whole. The problem with this approach is that it’s too easy to miss corner cases or unanticipated scenarios. Unit testing gets the developer to think about each unit individually and to verify those units independently.