In test-driven development, it’s important to make clear what a test checks and what result it expects. Microsoft Learn uses unit tests to show how code should behave given a particular input and to verify that behavior. A well-named test can convey the expected result without requiring you to read the method’s implementation.
The scope of a unit test
According to Microsoft Learn, unit tests help you determine whether a change to an application has broken existing functionality. Bugs introduced by a change are called regressions. You can rerun the test suite after every build or after changing a line of code.
A functional test involves opening the application and following a series of steps to verify the expected behavior. You can repeat those steps after every change. In a unit test, the test runner determines whether the test passes automatically.
Microsoft Learn describes a good unit test as fast, isolated, repeatable, and self-checking. A repeatable test gives the same result when nothing changes between runs. An isolated test runs independently of external dependencies such as the file system or a database.
Writing a test should take a reasonable amount of time relative to writing the code it covers. Microsoft Learn suggests considering a design that makes the code easier to test if writing the tests takes considerably longer than writing the code itself. This criterion takes into account both the test result and the time it takes to write the test.
Naming tests for the behavior they verify
Microsoft Learn recommends including three parts in a test name. These are the name of the method under test, the scenario being tested, and the expected behavior in that scenario. The name tells you what the test verifies and under what conditions. If the test fails, it also helps you identify which scenario fell short of expectations.
In Microsoft Learn’s StringCalculator example, Add_SingleNumber_ReturnsSameNumber replaces the name Test_Single. The longer name identifies the Add method, the single-number scenario, and the expectation that the same number will be returned. The test compares the result of Add("0") with zero.
The same approach applies to empty input. The name Add_EmptyString_ReturnsZero tells you that an empty string should produce a result of zero. The test name captures both the input and the result to verify.
The Arrange, Act, Assert pattern
Microsoft Learn describes the Arrange, Act, Assert pattern for writing unit tests. In Arrange, you create and configure the objects you need. In Act, you perform an operation on the object. In Assert, you verify that the result matches your expectation.
These separate steps make it clear which dependencies the code uses, how the call is made, and what result the test checks. Microsoft Learn notes that combining the steps can make the test shorter and emphasizes readability as the goal. Keeping the steps separate also helps prevent assertions from getting mixed in with the Act code.
In the empty string example, Arrange creates a StringCalculator. Act stores the result of Add("") in the actual variable. Assert checks that actual is zero. The pattern shows object creation, the method call, and result verification as separate operations.
Microsoft Learn recommends one Act step per test. You can write separate tests for separate actions or use a parameterized test. A single Act step makes it easier to see which operation failed and which scenario the test covers.
When a test includes multiple Act steps, each one needs its own verification. Microsoft Learn notes that once an Assert step fails, subsequent checks may never run. The test result can therefore leave it unclear whether other functionality is working.
Keeping inputs and test logic simple
According to Microsoft Learn, a unit test should use the simplest input that can verify the behavior under test. Unnecessary model properties or nonzero values where zero would suffice can distract from the behavior the test is meant to verify.
In the example that checks what Add returns for a single number, Microsoft Learn uses "0" as the input in place of a nonzero number. The test still verifies the value returned for a single number. The simple input keeps the focus on the behavior under test and gives the reader fewer implementation details to think about.
Hard-coded string values with no explanation can also make tests hard to read. Microsoft Learn calls these magic strings. To understand why an unusual value appears in a test, the reader may have to dig into implementation details. Microsoft Learn recommends giving these values meaningful constant names.
Microsoft Learn also recommends avoiding manual string concatenation and control-flow constructs such as if, while, for, and switch in unit tests. Adding logic to a test increases the chance of a bug in the test itself. If the logic seems unavoidable, the recommendation is to split the test into multiple tests.