Automated Testing with Unit Tests
Being able to automate the testing process of your projects is an important feature of a CI-environment. Having source code that works as tests for your application logic will help you when you’re later going back to modify, extend and fix existing code. With good tests you can ensure that your code changes won’t break the whole application. There are different frameworks available for unit testing, the most widely used with .NET is NUnit (http://www.nunit.org). To learn more about test-driven development, visit Wikipedia: http://en.wikipedia.org/wiki/
Test-driven_development.
When the installation of NUnit is complete, you can explore the included samples to learn more about writing unit tests. You can use the NUnit GUI to run your unit tests outside of the CI-environment.
To start writing unit tests, you need to add a reference to the following assembly in your Integration.Test project: “C:\Program Files (x86)\NUnit 2.4.8\bin\nunit. framework.dll”. Additionally you should create a reference between the Test and the Common project, so we can access our business domain model in the tests project.
When you’re done adding the reference, let’s create a class named PersonTest.cs which will contain all the unit tests for our specific domain object.
Tests are done through assertions. There are many types of assert methods included in the NUnit framework and you should use them to validate your business logic. Writing good unit tests is hard and it’s a practice in which you will continually improve as you get more experienced with it.
You can now begin to run and validate the unit tests on your local machine, open the NUnit GUI application and open the Integration.Test.dll file.
As you can see in my screenshot, both of the tests for the Person object validates.
When we later go back to change the business logic, we can use the unit tests as a means to validate that our changes doesn’t break anything.