2.4. Writing your first test

How do you test your code? A unit test usually comprises three main actions:

1.  Arrange objects, creating and setting them up as necessary.

2.  Act on an object.

3.  Assert that something is as expected.

Here’s a simple piece of code that does all three, with the assert part performed by the NUnit framework’s Assert class:

 public void IsValidFileName_BadExtension_ReturnsFalse()
     LogAnalyzer analyzer = new LogAnalyzer();

     bool result = analyzer.IsValidLogFileName("filewithbadextension.foo");


Before we go on, you’ll need to know a little more about the Assert class, because it’s an important part of writing unit tests.

2.4.1. The Assert class

The Assert class has static methods ...

Get The Art of Unit Testing, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.