views:

39

answers:

4

I am beginner of unit testing so I want to ask what kind of function/method we can use as unit testing object.

I want unit test sharepoint code which written on C#.

By the way, I don't ask about unit testing framework. I want to know that what kind of function I can use as unit test object.

Ex:

   // function that return a value.
   string getTitle()
   {
       // TODO: code logic here
       return "A Title";
   }

Or
   // function that no return a value
   void doAction()
   {
       // TODO: code logic here
   }

=> which one of them can use as unit testing object.

A: 

If you're a Java developer, you use JUnit or TestNG as your testing framework.

You write unit test classes that exercise your Java classes. You instantiate instances and call their methods in the tests.

duffymo
A: 

If you're using .NET then I'd highly recommend Moq to create test only implementations of interfaces. If you don't have interface declarations then Moles will allow you to test hard coded dependencies. Use this with caution however as in my opinion if you're using moles a lot then you have design issues with regards to lack of abstraction. If you are looking for a unit testing framework then NUnit has a lot going for it in terms of how fluid it is to write. We've had some issues getting moles to play nicely with NUnit so in general we've moved over to MSTest which is built in to Visual Studio; whilst not as nice as NUnit it does testing without any problems.

If you want to do unit testing of your sharepoint web pages then you're really looking at Selenium to help with that. It's very fragile though so i'd leave this until you're sure that your UI is complete

If you are totally new to Unit testing I'd highly recommend reading about it first. If you get tired of looking at web pages then just create a new MVC web application and take a look at the unit tests that are created out of the box to get an idea (just ignore their terrible test method names)

Dylan
A: 

To answer your question the first test is an easy object to test

assertSame(getTitle(), "A Title");

The second is more difficult because it doesn't give you a way to sense what is happening in it. You need to get access:

class SomethingDoer {
    public sense = false;

    public void DoAction()
    {
        // TODO: code logic here
        sense = true;
    }
}

test: public doer = New SomethingDoer();

doer.DoAction();


assertTrue(doer.sense);

I use the public variable sense to test.

[NOTE] this is totally a toy to explain concept of unit testing requiring a way to sense and check behavior of system under test.

Gutzofter
+2  A: 

Your question is really vague.

If you're asking about unit testing techniques, get a book. Perhaps this or this.

If you're wanting to test code that calls SharePoint objects, you have to talk about tools. You have to fake these out using either Typemock Isolator or Moles. The SharePoint object model is full of concrete, non-inheritable objects.

Mike