Lecture 11
Introduction to Unit Testing
1. What is Unit Testing?
o Unit Testing: Testing individual units of code in isolation to ensure
they work as expected.
o Key Point: If your code under test interacts with other classes or
external systems (e.g., databases), you're performing integration
testing, not unit testing.
o NUnit: A popular unit testing framework for C# that supports
automating unit tests, ensuring they can be run consistently and
repeatedly.
2. Test-Friendly Programming
o Writing code with testing in mind is essential. Use techniques that
make your code easier to test, such as dependency injection and
interfaces.
What is a 'Unit'?
1. Definition
o A Unit is typically considered a "unit of work," which could be a
method, class, or the smallest testable piece of code.
o Example: Testing a method that performs a specific function within
a class.
2. Roy Osherove's Definition
o "A unit of work is the sum of actions that take place between the
invocation of a public method and a single noticeable end result."