unit testing with nUnit

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
Ezeckiel
Posts: 3
Joined: Wed Mar 08, 2006 7:43 pm

unit testing with nUnit

Post by Ezeckiel » Wed Mar 08, 2006 8:10 pm

hi, i was looking at the API/Documentation and found many functions very usefull for unit testing (example MemAssertion.NoNewInstances). When launching nunit-gui as described in documentation, everything was fine.

Now the problem is that launching the test suite by any other mean does not start the profiler, ie MemProfiler.IsProfiling = false. Basically, what i was trying to do is write a test for every memory leak i found in my application to prevent regression. These tests are not run interactively, but rather by my build process under nAnt using a nUnit task (this build my code and run all my tests for me, reporting errors).

Another way of running the test is with TestDriven.Net, a free add-in for visual studio that let you run your nunit test directly in the ide. Again, i haven't find a way to start the profiler prior the beginning of the test.

At first, i was thinking that i should instanciate the profiler class and call Start but there is no such methods. So, i just want to use the profiler by code without any UI to test that my object are no more in memory. Is it possible ?

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Thu Mar 09, 2006 8:01 pm

In order to use the memory assertion methods for unit testing, the unit testing framework must run under the profiler. There is currently no possibility to start or attach the profiler after the application has started.

If you want to perform automated testing, it is possible to start NUnit under the profiler using command line arguments. The profiler can be run in a non-interactive mode by using the command line argument /noui.

Note that it is also recommended that you disable root referee identification if your run NUnit under .NET Framework 1.x. The command line below shows an example on how you can start NUnit under the profiler.

NetMemProfiler.exe /ri- nunit-gui.exe [optional arguments to NUnit]

For more information about the command line arguments, see the "Starting .NET Memory Profiler" section in the documentation.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Ezeckiel
Posts: 3
Joined: Wed Mar 08, 2006 7:43 pm

Post by Ezeckiel » Fri Mar 10, 2006 1:03 pm

Thanks for the reply. The method you descride works great when the unit test is run by the programmer, but that is not really "automated". Anyway, all our tests are done by a test server and the profiler cannot be start that way.

Is there a plan to add Attaching to process fonctionnality to a next release?
Thx

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Sun Mar 12, 2006 7:22 pm

We don't have any previous experience with NAnt, but I downloaded it and took a look at it.

One thing that makes it harder to perform memory testing using .NET Memory Profiler and NAnt, is that the NUnit task is integrated with NAnt. As mentioned previously, in order to perform memory assertions, it is necessary to start the profiled application using .NET Memory Profiler. In this case I see two options:
  1. Start the whole build process using .NET Memory Profiler. E.g.
    <path to MemProfiler>\NetMemProfilerConsole.exe /ri- NAnt.exe
  2. Avoiding using the <nunit2> task and start an external NUNit session. This can be performed using the exec task. E.g.

    Code: Select all

    <exec program="NetMemProfilerConsole.exe">
      <arg value="/ri- NUnit.exe" />
      <arg value="TestAssembly.dll" />
    </exec>
    
The disadvantage of the first option is that the complete build task is run under the profiler, which may or may not be a problem. The disadvantage of the second option is that the output from NUnit is not as well integrated with the build process as when running the <nunit2> task.

In order to make the build process fully automated, it is recommended that you provide command line arguments that defines what to do when a memory assertion fails (e.g /memleakprompt, /memleakcollect), and what to do when the session ends (e.g. /sessionprompt).

The next version of the profiler will allow you to attach to a process, but the snapshot information obtained will be less detailed (e.g. no stack trace will be presented).
Best regards,

Andreas Suurkuusk
SciTech Software AB

Ezeckiel
Posts: 3
Joined: Wed Mar 08, 2006 7:43 pm

Post by Ezeckiel » Mon Mar 13, 2006 1:35 pm

Thx alot. I will wait for the next version of the profiler for further testing.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests