Some tips and techniques for using NUnit, the testing framework used in this book.
As you read this book, you ll see that we do a lot of testing. We write most of our tests before we write the code, one test at a time. A little test, a little code to make it work, and so on. We work that way for some good reasons, which you ll see as we go along:
Most important, well- tested code works better. Customers like it better.
Testing as we write means we spend less time debugging. We get our programs done faster.
Testing as we write means that we don t have those long testing cycles at the end of our projects. We like working without that death march thing.
Our tests are the first users of our code. We experience what it is like to use our code very quickly. The design turns out better.
Tests support refactoring. Since we want to ship useful function early and often, we know that we ll be evolving the design with refactoring.
Tests give us confidence. We re able to work with less stress, and we re not afraid to experiment as we go.
Testing before coding is more interesting than testing after we code. Because it s interesting, we find it easier to maintain what we know is a good practice.
But I m not here to sell testing; I m here to tell you how we do it. Let s move along. We re going to start here with a moderately advanced example using regular expressions and the .NET Regex class. In the next chapter, which relates our first real efforts on the XML Notepad, you ll see that we return to an approach that is even simpler than Regex. Here, we re just experimenting with NUnit and learning a bit about Regex.
As we work with the XML Notepad, I m expecting that we ll have to look at text strings a lot and figure out whether they have any XML tags in them. XML tags, as you probably know, come in matched pairs of strings embedded in squiggles: <SomeTag>like this</SomeTag>. Now .NET includes some powerful features for working with XML, and later on in the book we ll look at some of them. But we re here to learn C#, and part of that will mean digging down a little deeper into how things work. Since C# has some powerful support for regular expressions, I decided to learn a little about them.
Often when I experiment with something new, I just write some throwaway code that prints results to the console. Some good angel inspired me to do it this time by writing tests using NUnit, the .NET testing framework. The result, as you ll see, is some permanent information instead of just something that goes into my head, perhaps later to be forgotten. And something that others can learn from, making their job just a bit easier. Read on.