Wednesday, 1 April 2009

Code coverage using PartCover and NUnit

Question:
I looked for a free open-source tool to calculate code coverage for my NUnit tests.

Answer:
NCover has been a good choice until they went commercial. (very bad)

NCover is dead, long live PartCover.

PartCover is an open-source project for code coverage under GNU GPL. That also means, that you're not allowed to commercialize non-free derivatives.
PartCover does most of the things most of us would demand of such a tool. And don't forget - if you really want a new feature, you can add it yourself and help the community.


Sample setup.

I have a business logic assembly "VokAdapt.BLL" that I test using NUnit with a fixture which sits in the assembly "VokAdapt.BLL.Test".
I want PartCover to start NUnit, execute the tests and get the coverage of the business assembly.

1. Open PartCover GUI.
2. Fill in Target Settings.
2.a The "Executable File" here is NUnit.exe. You might want to choose the console version.
2.b The "Working Directory" is the directory that contains your test assembly.
2.c The "Working Arguments" is the test assembly simple name.
2.d Add a rule that tells PartCover which assemblies/classes to consider.



3. Start PartCover. (That will run the target application, here NUnit)
3.a Execute your tests.
4. Close NUnit and view results



You see very nice stats of your coverage and you can even inspect the code.
I get 36% coverage here. That isn't brilliant, so I better stop blogging and write some tests :)

If you like to integrate PartCover into VS2008 read Vlad's blog entry
PartCover has been added to SharpDevelop 3.
Monday, 8 December 2008

Encrypting config sections on the fly in .Net

Question:
In a asp.net application I want to keep some config sections encrypted. How do I do this?

Put config sections into a separate file (.Net)

Question:
How do I put DotNet config sections into separate file?

Thursday, 20 November 2008

Useful queries for optimizing Sql Server 2005

Question:
How can I figure out the queries that take most resources on a server.

Answer:
We use this query.
It lists the worst queries (I/Os) on your database since the last restart.

select top 5 (total_logical_reads/execution_count) as avg_logical_reads,
(total_logical_writes/execution_count) as avg_logical_writes,
(total_physical_reads/execution_count) as avg_physical_reads,
Execution_count, statement_start_offset, p.query_plan, q.text
from sys.dm_exec_query_stats
cross apply sys.dm_exec_query_plan(plan_handle) p
cross apply sys.dm_exec_sql_text(plan_handle) as q
order by (total_logical_reads + total_logical_writes)/execution_count Desc

I grabbed it somewhere form TechNet: Monitoring SQL Server health
Wednesday, 15 October 2008

How to use the HttpContext cache in a non-web (test) environment

Question:
How to test a class that relies on HttpContext.Current.Cache (or so) in order to work.
If you test this in NUnit, for example, there will be no current HttpContext around.
So abstract the access to the Context away and in the property you'll provide another cache if need is.

Attach Debugger macro in VS 2005/ 2008

Question:
How to use a macro in Visual Studio 2005/2008 to attach a debugger to a process?

Tuesday, 14 October 2008

Call private methods of an object from a test assembly. (NUnit)

I have to test a class of Foo from my test assembly Foo.Test.
Unfortunately, many methods I should test are not public.

This class uses reflection to call private methods on any type or object for testing.

using System;
using System.Reflection;

namespace TestUtilities
{
public class TestHelper
{
public static object InvokeStaticMethod(System.Type type, string methodName, object[] parameters)
{
BindingFlags bindFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
return InvokeMethod(type, methodName, null, parameters, bindFlags);
}
public static object InvokeInstanceMethod(System.Type type, object instance, string methodName, object[] parameters)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
return InvokeMethod(type, methodName, instance, parameters, bindFlags);
}
private static object InvokeMethod(System.Type type, string methodName, object instance, object[] parameters, BindingFlags bindFlags)
{
MethodInfo methInfo;
methInfo = type.GetMethod(methodName, bindFlags);
if (methInfo == null)
{
throw new ArgumentException(String.Format("There is no method with name {0} on type {1}", methodName, type.Name));
}

object result = methInfo.Invoke(instance, parameters);
return result;
}

}
}