Showing posts with label discussions. Show all posts
Showing posts with label discussions. Show all posts

Friday, June 22, 2012

How will another unit test framework cause me to write better tests than using MSTest ?

 I started out with an idea of writing down some of the things I discovered while writing unit tests on both MSTest and the popular NUnit unit testing frameworks and for the most part I wasn't impressed with MSTest. The article developed on how MSTest sucks (the one fore Visual Studio 2010) and how NUnit rules (ok its a bit biased and exaggerating but get this...) but I got hit with one fundamental question...

How will another unit test framework cause me to write better tests than using MSTest?

You see you can have all the "nice features" in the world but if you still write unit tests against a system that isn't well designed or broken up in testable pieces, you will have a hard time writing maintainable unit tests. The other thing is that even if you have the most wonderful designed system and unit-test friendly classes to test against, you still sit with the problem of writing maintainable unit tests.
So I thought of rewriting the post and taking on this discussion from a different perspective. Instead of blabbering on about how this framework rocks and the other one doesn't let's ask the question above. Why against MSTest? Because it seems to be the underdog at the moment. Look at what some of the other bloggers have to say:
I also have a couple of things to say about it which I just quickly want to add before I ask the big quesiton.
  • Firstly it seems to be another monolithic solution from Microsoft. Why not have a .NET testing solution which can be broken down in different testing modules so that you can use the best tool for the job? So then you can use the other testing tools from Microsoft but for Developer unit testing you use something like NUnit or something. But I know why this is the case because again Microsoft wants you to use their products. Now it wouldn't have been a problem if they have taken certain things into account like some of the bloggers up there have stated like extensibility, etc.
  • Startup time is SLOW! And then it's only basic logic tests!
  • There are plugins in Visual Studio to run NUnit tests that are free! (Not a MSTest gripe but many people stick to MSTest because its integrated. Speaking of which there are ways apparently, which I haven't tested myself, to run NUnit on a TFS build server. Check this link: "NUnit for Team Build").
So now I want to start this question objectively and look at how using the best features of unit tests allow me to write more maintainable unit tests. But in instead of taking every unit testing framework I will settle with NUnit and MSTest because I am familiar with NUnit and it seems to be the most popular one out there, and MSTest because everyone is slashing this one to bits so it should provide a nice view if they really make a difference.
Now you might say "I've done this and that with MSTest and it took me hours while I could have done the same in NUnit in minutes". Let's find out shall we?

I'm not going into UI tests and Web tests. Firstly I don't think something like NUnit should be brought anywhere near UI testing! MSTest might have a nice UI tester but you might need to purchase Visual Studio Ultimate or something to get hold of it. Neither am I going to do web tests which involve HTML and JavaScript! That's just a maintenance nightmare waiting to happen!
No I will solely stick to logic tests and maybe even integration tests. Let Developer unit testing BE developer unit testing, anything else, use some of the other tools that are more suited for the job!

Writing tests for small methods are fine because your tests are inherently small to test the small amount of work it needs to do. Stuff like math functions, you give it an input and it spits out an output, very simple and quick. You do the MATH! LOL!
Some of you may say "Well NUnit will simplify your life there, you can have 1 test method with a few value attributes". That's true so NUnit can give you some simplified and good test coverage there. MSTest will require the code duplicate approach or you have to write the plumbing (again it might not be what developers would like as MSTest is not as extensible as NUnit) to get to the same kind of ease-of-effort that NUnit guys are enjoying.

But let's make it more interresting!

How about writing tests against a WCF service where you need to stub out some calls to other services and have to validate that the correct logic is being followed?

So let's say I have a WCF service class:

public class TimeSheetService : ITimeSheetServiceContract
{
       public PersonDetails GetPersonDetails(string id)
       {
            ... some code here ...
       }
}

Ok seems easy to test:

NUnit

[Test]
public void Get_user_details_from_timesheetservice_that_is_existing()
{
     TimeSheetService service = new TimeSheetService();
     var person = service.GetPersonDetails("12345");
     Assert.That(person, Is.Not.Null);
     Assert.That(person.Name, Is.EqualTo("James"));
}

MSTest

[TestMethod]
public void Get_user_details_from_timesheetservice_that_is_existing()
{
     TimeSheetService service = new TimeSheetService();
     var person = service.GetPersonDetails("12345");
     Assert.IsNotNull(person);
     Assert.AreEqual("James", person.Name);  
}

Not much of a difference, right? But hang on a moment! Where do I stub the person with the name "James"?
Big deal, I'll just use my favorite mocking framework to do that.
I currently use Rhino Mocks as my mocking framework and I am open for discussion, so if you see me doing something wrong or you want to notify me on something else that Rhino Mocks users will like to move across to please let me know. Assume I have a preconfigured 'mock' object (will get to that later) and I have noticed that the service needs an IPersonRepository object.

NUnit

[Test]
public void Get_user_details_from_timesheetservice_that_is_existing()
{
     IPersonRepository personRepository = mock.StrictMock<IPersonRepository>();
     personRepository.Expect(x => x.GetPersonDetailsFromRepository("12345")).Return("James");
     mock.ReplayAll();

     TimeSheetService service = new TimeSheetService(personRepository);
     var person = service.GetPersonDetails("12345");
     Assert.That(person, Is.Not.Null);
     Assert.That(person.Name, Is.EqualTo("James"));

}

MSTest


[TestMethod]
public void Get_user_details_from_timesheetservice_that_is_existing()
{
     IPersonRepository personRepository = mock.StrictMock<IPersonRepository>();
     personRepository.Expect(x => x.GetPersonDetailsFromRepository("12345")).Return("James");
     mock.ReplayAll();

     TimeSheetService service = new TimeSheetService(personRepository);
     var person = service.GetPersonDetails("12345");
     Assert.IsNotNull(person);
     Assert.AreEqual("James", person.Name);  
}

See how much it grew? Both are still the same length. The problem is that this is how we generally run into these things. If we don't take care, we usually end up with bloated unit test methods!
Now's the question is how can the unit testing framework make my life simpler?
Now let's forget using other libraries (AutoFixture as example) and let's forget writing helper methods (unless it's part of getting the framework to help deal with this). Let's get the unit testing framework to do most of the work for us. See what I mean with this? I know that there are stuff out there that can help deal with this but that's not the point. Can my unit testing framework help take care of this without me having to write helper methods/classes or rely on external solutions? If it cannot, then why? Those are the things I want to point out.

And so I present this as a question to you the reader. I know MSTest and I know NUnit but I don't know all the integral details that each offer to leverage it to my advantage. So if you look at the above and see some area where MSTest or NUnit can make my life easier please comment below. If you are experienced in another unit testing framework, please give your input as well! You don't necessarily have to give big code replies, just mentioning a feature and how it can be applied should help get a picture.

Just to finalize this let me give a broader picture of the problem and then hopefully people can start sharing their input so that we can really see how much of a difference a unit testing framework makes.

First there is the Rhino Mocks problem. How can I create a new MockRepository for each test and provide it in such a way that when I call the field (or property) "mock" that I have a ready to be used mock. Also don't forget that Rhino Mocks expect the "ReplayAll" to be called before you call your SUT (System Under Test) code and after the test is completed it need to "VerifyAll" against the mock repository object.

Secondly I want to test something else. I want to test the SubmitTime method.
Let me write out the code (only the relevant stuff) so that you can have a clearer picture of what needs to be tested:

public class TimeSheetService : ITimeSheetServiceContract
{
      private readonly IPersonRepository _person;
      private readonly ITimeCaptureRepository _timeCapture;

      public  TimeSheetService(IPersonRepository person, ITimeCaptureRepository timeCapture)
      {
             _person = person;
             _timeCapture = timeCapture;
      }

      public PersonDetails GetPersonDetails(string id)
      {
              var result = _person.GetPersonDetailsFromRepository(id);
              return new PersonDetails { Name = result.Name, Categories = result.Categories };
      }

      public bool SubmitTime(TimeSubmitRequest request)
      {
             PersonDetails personDetails = GetPersonDetails(request.PersonId);
             bool added = _timeCapture.SubmitTimeCapture(request.PersonId, personDetails.Categories, request.TimeSlots);
             return added;

      }
}

Now the Id or PersonId is a string, Categories are IList<string> and TimeSlots are IList<TimeSlot> and TimeSlot is of class of properties { DateTime StartTime, int Minutes, string Category }

If there are somethings that are not clear let me know.

    Wednesday, August 17, 2011

    My Half-Life 2 Episode 3 speculations

    I have been watching the web for a while for the anticipated sequel to Half-Life 2 (Episode 3) which has been in the works for quite some time now. Being a Half-Life fan myself, I too can't wait for its release, however many people have been speculating what the story may contain and how it will play out.
    If you haven't played Half-Life 2's episodes and you would like to in the near future, then I do would like to give note that there might be some spoilers in this post. Looking through the web and listening to the developer commentaries, I thought it would actually be pointless to try and speculate what Valve is going to do next because they actually just plan and improvise as they go (which is probably why they take so long on each of their games) but I thought to just throw the stone and see where it hits.

    Some people think that the helicopter ride from White-forest is going to be a cut scene followed by the "unforeseen consequences" that's going to happen when Gordon finally uncovers the secret of what the Borealis might hold and then we find out what happens from there on in the rest of the game.
    Now personally, judging on how Valve developed the HL games in the past, I would like to state a different point of view. HL doesn't have cut scenes, you play from the morning you arrived at Black Mesa to the point where you defend the White-forest base, though there is a time lapse of approx. 10 years between HL and HL2 and not to mention the week-slow teleport.

    I would like to present some of the stuff that's already out there, in terms of clues, previous occurrences in HL, interviews, etc. It helps to paint the picture of what to expect.

    I think the helicopter ride (from White-forest) is going to be far from pleasant and quick. They would maybe make a few pit stops along the way, because the arctic is a far way off. They might need to refuel or they might even crash the chopper along the way.
    Also there is the head-crab that jumped into the satellite at the end of episode 2 and judging of what happened with Gordon in the beginning of HL 2, you can just imagine what might happen after the launch. Yes the portal between the Combine and earth is closed, but what else might happen? I think that at some point Alex and Gordon is going to be separated again and Gordon is going to be faced with a difficult decision to make (which the story is going to do for you anyway but it gives you the illusion of the choice).

    Would Adrian Shepherd make a comeback or is he totally lost forever? The reason I ask is because at the end of Oposing Force, the case file states:
    Status: Detained
    Further evaluation pending.
    There is also a YouTube video depciting the keyboard found on the background of Portal 1 (once you've finished the game) has all the necessary keys highlighted which (when put together) forms "Adrian Shepherd". Could that be an indication of something? Also, Barney never spoke in BlueShift but they gave him a voice in HL2. Couldn't the same happen with Adrian. Also, if he were to make a comeback what role would he play?

    I also stumbled on some half-life wiki which features some of the earlier Half-Life 2 concept story and art which actually has the Borealis boat and base depicted. Just something interesting I thought I wanted to share. The original story has been DRAMATICALLY changed but maybe it might give us some idea of what to expect.

    In a relatively old post at Computer And VideoGames, they featured Gabe Newell who explained: "I feel like we've gotten away from genuinely scaring the player more than I'd like, and it's something we need to think about, in addition to broadening the emotional palette we can draw on."
    The article ended with: When Edge asked what scares them the most, he had a particularly dark answer: "The death of their children. The fading of their own abilities."
    Could this mean that someone close to Gordon is going to die? We also know that the Advisors are powerful, being able to pull Gordon and plunge him to the nearest wall and he is pretty worthless against them. Would they be able to weaken Gordon to some extent? Could Gordon be teleported to the Combine world somehow? I think that could also be scary. Maybe that's where Adrian would come to help Gordon providing he makes a comeback.

    With the death of Eli, the resistance might suffer a bit since Eli was the leader of the human resistance. Would the resistance loose its power? When I listened to the commentary of Episode 2's ending, the game creators mentioned that Eli has reached his purpose in the game. So what other side effects could we expect from his death other than Alex's wanting for revenge? Alex might put herself in unnecessary danger due to her thirst for revenge and that might cause the unnecessary separation between Gordon and Alex.

    Lastly, what new revelations would we see of the G-Man? We have learned that he is not the main chief but is actually "working" for some "employers" who somehow orchestrate the whole chain of events but to a certain extent. He mentioned "the biggest embarrassment has been Black Mesa" which implies that they are not in so much control as they make out to be, also we see in the beginning of Episode 1 how the Vaurtigans holds the G-Man back. Yet the G-Man keeps on "predicting" the future. He spoke to Eli twice saying "Prepare for unforeseen consequences", before the Black Mesa incident and at the end of Episode 2. Is it future vision or just the logical statement to make based on his orchestration of events that leads to the catastrophic happenings? There is still too much unknown about this character to make any good speculation. For all we know he could be a supernatural being or even an alien. Only Valve knows at this point? Or do they? ;-)


    Thursday, July 21, 2011

    {Insert teacher name here}

    "Oh by the way, did you hear about Rob Bell's controversial book?", I heard someone say to me a couple of weeks back. At first I wasn't sure what it was about, and I didn't think that it would have sparked the flame within the Christian community because I've been a listener to Rob's DVDs for a couple of years and he seemed ok to me.

    After finally going on YouTube to look for some interviews over his new book "Love wins", I was shocked over the way Rob responded to the interviewers and each time he would either give a rehearsed response to the problem in Japan or he would back-pedal over certain issues such as with the questions over God's wrath answering something a little different than written in the book or when asked about whether the crucifixion is the chief and main relevance to Christianity, he would side-step the question.

    I would agree that this is a hard thing for me to take in. Like I said, I knew Rob from his Nooma series and it was very thought provoking and helpful, especially within small groups, etc. I read his book "Sex God" and watched his hour lecture on "Everything is spiritual" and based on what I heard, everything seemed clear and fine (based on what I understood ofcourse). To even think that Rob is a heretic was unthinkable. I thought it was just a misunderstanding of some sort (and in a way I think it can be) but I think the gap is too big to look past it.

    The more I listened to his interviews the more he sounds different. It's as if he cannot find his own feet with those questions. Now, just to make something very clear, I haven't read his book.
    I admit and I will try not to comment on his book but rather on the media around his book and the interviews that are freely available on the web.

    Now I may sound like another blog writer giving my two cents worth on the web about a man who seems to have "lost his way" but what I want to get out of this post is not to discredit, tear down or bad mouth him, but rather create the awareness of how easy it can be for any preacher to preach a message that is just alongside the truth. No matter who the person is, no matter how wonderful his/her message seems like, we need to validate their message. Also, just because they've helped a lot of people and stand for worthy causes, doesn't make their wrong preaching right. I know that not everyone is perfect, I mean we might get something wrong during a preaching/teaching and we need to take the responsibility of admitting that we've been wrong when we discover that it is true and to correct it but I'm not talking about slight misunderstandings in scripture I'm talking about something that's a fundamental belief that could alter the way we live as Christians in our lives.
    Take for instance the idea of the crucifixion. If I believe that Jesus died on the cross but didn't rise from death, then I won't be able to believe that my sins are forgiven and I would be having a lot of other false beliefs entering my idea of Jesus. That's what I'm talking about.

    I can go on and on about Rob's statements but that's not the point of this article, I could have written about someone else entirely who also is close to my heart. For all I know Chip Ingram can also loose his way (and I sincerely hope he doesn't and that God protects him). The point is, this is how crafty the devil is and how vulnerable we are. Any one of us can fall victim to these shifts into wrong fundamental beliefs. Its just that the case with Rob (whether he is guilty or not of heresy) has opened my eyes to the reality that even the best or wonderful of teachers (except Jesus ofcourse) can fall victim to these headlines which we see in the case with Rob. I'm not saying that everyone of them will be victim of it, its just that its a warning to us all to be on the lookout in case it does happen.

    So what I would propose is that we pray for people like Rob Bell, also our very own pastors and preachers that they wouldn't fall victim of Satan's attacks and that they (and we) would gain discernment from the Holy Spirit to interpret the scriptures correctly.

    God Bless.