Saturday, September 22, 2007
Sorry. We moved to different location.
Sunday, April 29, 2007
Testing Non-Public members with MbUnit (Part II).
Following methods are marked as obsolete. My guess is that these methods are going to be gone completely in version 3.
Old Method | New Method |
GetNonPublicField | GetField |
GetNonPublicVariable | GetField |
GetNonPublicProperty | GetProperty |
RunPrivateMethod | InvokeMethod |
RunNonPublicMethod | InvokeMethod |
There're also new methods:
SetField - Sets field value.
SetProperty - Sets property value.
Thanks to Jeff who pointed out that Reflector didn’t have these methods. He also mentioned that method names between static and instance implementation are inconsistent. This also was corrected.
Let's create SampleClass:
public class SampleClass
{
public string publicString = "MbUnit Rocks!!!";
private DateTime privateDateTime = DateTime.Today;
internal DateTime InternalProperty
{
get { return privateDateTime; }
set { privateDateTime = value; }
}
private static int Add(int x, int y)
{
return x + y;
}
}
Here an example how we can create tests for SampleClass:
TestSample _sampleObject;
Reflector _reflect;
[TestFixtureSetUp]
public void Init()
{
_sampleObject = new SampleClass();
_reflect = new Reflector(_sampleObject);
}
[Test]
public void GetPublicField()
{
Assert.AreEqual("MbUnit Rocks!!!", _reflect.GetField("publicString"));
}
[Test]
public void SetNonPublicProperty()
{
string propertyName = "InternalProperty";
DateTime dt = new DateTime(2008, 1, 1);
_reflect.SetProperty(propertyName, dt);
Assert.AreEqual(dt, _reflect.GetProperty(propertyName));
}
[Test]
public void StaticPrivateMethod()
{
Assert.AreEqual(7, _reflect.InvokeMethod("Add", 1, 6));
}
I mentioned before that static and instance methods now have consistent names.
You can implement StaticPrivateMethod test like this using static implementation of InvokeMethod(..).
[Test]
public void StaticPrivateMethod()
{
Assert.AreEqual(7, Reflector.InvokeMethod(new SampleClass(), "Add", 1, 6));
}
Here's another of Jeff's suggestions:
Indicating public vs. non-public is useful for documentation purposes.
Once again, because of Jeff's suggestion we have AccessModifier enum type and using it we can specify if we want to access public, static, or non-public member. In examples above we didn't use AccessModifier. It means that Reflector will invoke method or access field or property as long as an object has implementation for it.
In the example below we are trying to test Add method that has private static modifier. However, if Add method wasn't static or was public, an assirtion would happen in our test.
[Test]
public void StaticPrivateMethod()
{
Assert.AreEqual(7, _reflect.InvokeMethod(AccessModifier.Static | AccessModifier.NonPublic, , "Add", 1, 6));
}
It's a new code that was checked in; however, it has not been built yet. These and some other new features should be available in a next MbUnit build.
Thursday, April 12, 2007
Duration in MbUnit
The test below will fail because in Duration attribute we set duration for one second but inside the test we sleep for 2 seconds.
[Test]
[Duration(1)]
public void DurationFailTest()
{
Thread.Sleep(2000);
}
Next test will succeed because we don’t sleep at all.
[Test]
[Duration(1)]
public void DurationSuccessTest()
{
Thread.Sleep(0);
}
You can use it with RowTest attribute. Row(0) will succeed and Row(2000) will obviously fail.
[RowTest, Duration(1)]
[Row(0)]
[Row(2000)]
public void DurationRowTest(int sleepTime)
{
Thread.Sleep(sleepTime);
}
Tuesday, April 10, 2007
TypeMock too powerful to use
It was then. Today I believe that TypeMock is bad for the same reason I loved it before. It’s too powerful. It doesn’t force you to write a testable code.
The reason I found Rhino.Mocks hard to use is because I haven't learn yet about Inversion of Control Containers and the Dependency Injection pattern. Today I write my own mock objects without using any framework. However, if you want to use a Mock Framework, I strongly recommend Rhino.Mocks.
If you're not familiar with this pattern I recommend to read Jeremy Palermo's article Simple dependency injection to get you started with unit testing.
Sunday, April 8, 2007
Timer class
Here’s there Timer class:
public class Timer
{
long _start;
long _stop;
public void Start()
{
_start = GetTimeInMilliseconds();
}
public void Stop()
{
_stop = GetTimeInMilliseconds();
}
public long TimeElapsed
{
get { return _stop - _start; }
}
private long GetTimeInMilliseconds()
{
return DateTime.Now.Hour * 60 * 60 * 1000
+ DateTime.Now.Minute * 60 * 1000
+ DateTime.Now.Second * 1000
+ DateTime.Now.Millisecond;
}
}
In case you’re interested in performance difference between SqlDataReader and DataTableReader. Here’re my findings:
On single CPU machine SqlDataReader was faster and it of course was expected. However, on double CPU machine DataTableReader was slightly faster and this was a pleasant surprise for me.
Tuesday, April 3, 2007
Fixture order with MbUnit
B_Fixture
…
N_Fixture
Bad_Fixture
B_Fixture
B_Child_Fixture
[TestFixture]
public class A_Fixture
{
[Test]
public void Success()
{ }
}
public class Bad_Fixture
{
[Test]
public void Failure()
{
Assert.IsTrue(false);
}
public void Success()
{
}
}
[DependsOn(typeof(A_Fixture))]
[DependsOn(typeof(Bad_Fixture))]
public class B_Fixture
{
[Test]
public void Success()
{
}
}
[DependsOn(typeof(B_Fixture))]
public class B_Child_Fixture
{
[Test]
public void Success()
{
}
}
You can see that B_Child_Fixture depends on B_Fixture. B_Fixture at the same time depends on A_Fixture and Bad_Fixture. As you can see one of the tests in Bad_Fixture will fail. As a result B_Fixture and B_Child will not run.
Sunday, April 1, 2007
AddressAccessDeniedException, Vista & Orcas
http://localhost:8000/
namespace and how to resolve this problem. Using his suggestion to use netsh utility, I was able to get rid of the exception. It also became very clear why it worked for VS 2005 and not VS “Orcas”. To be able to run VS 2005 on Vista I had to run VS 2005 as Administrator and when press Ctrl-F5 or F5 the service also ran as Administrator. However, you don’t need to run VS “Orcas” as an Administrator and the service of course runs as VS user that doesn’t have rights to listen to http port 8000.I wonder how many security issues like this we’re going to have. Unfortunately security and convinience don’t go together and we have to learn how to live with this.