New for unit testing

I would like to know how to implement unit testing in an existing (rather large) application using visual studio 2008 (.net 2.0).

I understand that developing unit tests for existing / legacy code is unrealistic, but I would like to have tests to promote the code.

I found many examples of how to write tests for code, but nothing about how to configure it from scratch on an existing project and how to integrate it into the development cycle.

+10
unit-testing
Feb 24 '09 at 8:44
source share
3 answers

Simple approach:

  • Choose one of the unit test frameworks (Nunit, MbUnit, Xunit, VS unit test framework, ...)
  • Add a unit test project in your solution (e.g. UnitTests).
  • Start writing tests.

To simplify the organization, create at least one namespace for each project in the solution, for example UnitTest.Project1, UnitTests.Project2, ... Depending on the size of the projects, add more levels to the namespace.

This test project is part of the solution. If you use some kind of continuous integration tool, then the tests can be automatically executed with every commit in the source code repository.

Edit:
Regarding the comment on the MS unit test. The Framework problem with creating unit tests, there is a problem with the information: β€œCreate unit test” wizard does not work . It seems that there are unsupported project types that prevent the unit test wizard from working correctly. I just tried with one solution that has one F # and several C # projects. I added a unit testing project and tried to add tests. The test wizard had problems until I unloaded the F # project. Then everything worked fine.

+5
Feb 24 '09 at 8:55
source share

One easy way to get tests running with existing code is to have a test writing policy when an error is detected.

i.e

  • Find a bug
  • Record a test in which a bug is fixed
  • Fix
+3
Feb 24 '09 at 9:09
source share

I highly recommend reading this book: Effectively working with legacy code if you want to do a unit test for existing code. It is also a good book about best practices for unit tests in general.

You can do unit testing on existing projects, but you will need to make some adjustments here and there to make the code verifiable. The problem is often related to dependencies that are too large.

EDIT (after your comment) If you really want to integrate unit testing into your development cycle, then you need to switch to TDD ( Test Driven Development ). The goal here is to write your unit tests first so that you have a good understanding of what your classes will do. Of course, these tests will fail, but the goal is to get them to work one by one. Make google on TDD, there is a lot of information.

+1
Feb 24 '09 at 8:55
source share



All Articles