Chapter 2: Testing with NUnit


Some tips and techniques for using NUnit, the testing framework used in this book.

Testing: A Core Practice

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.




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net