views:

12896

answers:

10

I've now found out what Unit Testing is, does anyone have an example of Unit Testing in C#?

The example must be simple :)

A: 
[Test]
public void A_MapsTo_B()
{
var a = new A();
a.Name = "name";

var b = new B();
Mapper.MapNames(a, b);

Assert.AreEqual(a.name, b.name);
}

Checks to see if the MapNames method correctly maps the name property, by asserting the values are the same after the method is called.

Dan
A: 

Depends on the Unit Testing Framework, here is an Example of NUnit from the top of my head (I know that Is requires another Using, but i'm not sure which one)

using NUnit.Framework;

namespace whatever
{
/// <summary>
/// TestFixture indicates that this class is a class that contains Tests
/// </summary>
[TestFixture]
public class MyTestClass
{
/// <summary>
/// Tests whether MyFunction returns 5 when given SomeParam
/// The Test Attribute indicates that this is a test.
/// Tests need to be Public Void and no parameters
/// </summary>
[Test]
public void Test1()
{
ClassYouWantToTest ctw = new ClassYouWantToTest();
Assert.That(ctw.MyFunction("SomeParam"),Is.EqualTo(5));
}
}
}
Michael Stum
A: 

Hi Dan,

Thanks for the example, but could you explain how it works please. I don't understand it.

GateKiller
+4  A: 

Ayt, here's the simplest I could come up with:

using System;
using NUnit.Framework;

namespace SampleUnitTest
{
  [TestFixture]
  public Class SampleTest
  {
    [Test]
    public void AddingOneAndOneResultsInTwo()
    {
      int two = 1 + 1;

      Assert.AreEqual(2, two);
    }
  }
}

You need to install and reference NUnit in your project, and add a reference to NUnit. Easiest way to do this is install TestDriven.NET, which adds NUnit to your .NET references and installs a nifty Visual Studio add-in to run tests from inside the IDE. :)

Can't install TestDriven.NET if you're just using Express though :(

The TestFixture attribute before the class name declaration simply indicates that this is a test and all the methods inside it with the Test attribute will be run.

Asserts determine if your tests pass or fail. If the condition within the Assert is met (e.g., in the sample variable named two is really equal to the number 2) then the test passes. If it's not, then the test fails.

Jon Limjap
A: 

Have you looked at the QuickStart page for NUnit? I think it's a good place to start. I also think that when you download NUnit you get a sample project as well (however not 100% sure on that one).

Riri
A: 

Lattex,

Thanks for that explanation. It was very clear to understand :)

GateKiller
+3  A: 

This is a Windows control I created some time ago.

You can download it and see a real project with actual working unit tests.

Both the control and the unit tests are pretty simple so they should be a good starting point - for example, open the Tests\TestTextBoxRegex.cs file and analyze the TestIsTextValidSmallLetters or TestIsTextValidNonAlphaNumericCharacters function.

Marek Grzenkowicz
A: 

hey, i wonder how does this program run? should there be any main function to start??

hilly
It is not run by your program. NUnit.exe looks through your executable and finds any methods marked with [Test] and runs them. If it meets an assert which fails, it shows as red. If it doesn't run for some reason, it shows yellow, and if it passes it shows as green.
Jonathan
+1  A: 

No need to use this lib. MS also support Unit Test inside Visual Studio http://msdn.microsoft.com/en-us/library/ms379625%28VS.80%29.aspx

binhvu
+2  A: 

Definitely read the Osherove book, The Art of Unit Testing

Aidan