Profiling a Windows Service through NUnit

Profiling a Windows Service through NUnit

Hi,


I have a problem while trying to profile a Windows Service through NCover.


Here are the sequence of steps I am following:

1. Run NCover.Console.exe with NUnit.exe as the argument.
2. When the NUnit window launches, open the NUnit test dll in which I have test cases for the Windows Service called "MyService".
3. Run these Test Cases.
4. Close the NUnit application, and then check the Coverage logs.


Now, in step 2, the dll which contains the NUnit test cases has a reference to the said MyService.exe.
Most of the test cases call the functions of MyService directly, like
MyService.UtilClass utilClass = new MyService.UtilClass();

The NCoverage log shows the coverage for this MyService.UtilClass constructor correctly.

But, I have one testcase which tests the Starting and Stopping of 'MyService', for which I am using a ServiceController object, as follows:

ServiceController service = new ServiceController("MyService"); service.Start();

When this executes, the MyService starts as expected, but the NCover log reports that the OnStart() And OnStop() and other methods of MyService have not been Covered (or excuted)

I suspect that this is because we are using ServiceController to manage the Service.

Could you please suggest an alternative for this, so that I can get the proper coverage for all the TestCases?

Let me know if you need more information.

Regards, Santosh


RE: Profiling a Windows Service through NUnit

Your best bet here is going to be to just unit test the OnStart and OnStop methods rather than using the ServiceController.

One other option would be to move those particular tests out into a set of functional tests rather than than your unit tests, and use NCover's service profiling to test those particular features. One problem there, though, is that we have to start the service to run coverage, and we have to stop the service to end coverage, and if you actually stop coverage during your tests you'll cause coverage to stop being recorded. That said, you could trust NCover to test your starting and stopping and then write any other tests that require the service to be running to fully test.