O'Reilly logo

The Art of Unit Testing, 2nd Edition by Roy Osherove

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

[Test]
 public void IsValidFileName_BadExtension_ReturnsFalse()
 {
     LogAnalyzer analyzer = new LogAnalyzer();

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

     Assert.False(result);
 }

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required