Freigeben über


My first unit test in Visual Studio Team System

As usual when you sit down and write your first bit of code in a new language or in this case a new unit testing framework what else would you write other then the tested version of Hello World (thanks to Scott Densmore). In this case the unit testing tool is part of the Visual Studio Team System that was announced at TechEd last week in San Diego. The example shown here uses the tech preview that was distributed to attendees.

using Microsoft.VisualStudio.QualityTools.UnitTesting.Framework;

public class HelloWorld
{
    public static string HelloWorldString()
    {
        return "Hello World";
    }
}  

[TestClass]
public class HelloWorldTest
{
    [TestMethod]
    public void HelloWorldStringTest()
    {
        string expected = "Hello World";
        Assert.AreEqual(expected, HelloWorld.HelloWorldString());
    }
}   

For those of us familiar with xUnit frameworks (JUnit, NUnit, etc) you will find a lot of similarities and much to like.

When I run the test I get the following result:

HelloWorldStringTest: Passed, Duration: 00:00:00.0924109

If I was to change the test to the following:

[TestClass]
public class HelloWorldTest
{
    [TestMethod]
    public void HelloWorldStringTest()
    {
        string expected = "Hello World Again";
        Assert.AreEqual(expected, HelloWorld.HelloWorldString());
    }
}

When I run this test I get the following result:

HelloWorldStringTest: Failed, Duration: 00:00:00.3460624

Assert.AreEqual failed. Expected:<Hello World Again>, Actual:<Hello World>.
at HelloWorldTest.HelloWorldStringTest()

Comments

  • Anonymous
    June 02, 2004
    Gosh darn it.
    I try to run the test I made through the test explorer but I keep getting the dreaded "Test Run configuration needed" dialog.
    Where am I supposed to get one of those?
  • Anonymous
    June 02, 2004
    Roy - The only way that I know of how to get the .rcg file is to create a Test Project. I will follow up with the dev team and see if there is another way.

    Jim
  • Anonymous
    June 03, 2004
    You could simplify that code by doing:

    using NUnit = Microsoft.VisualStudio.QualityTools.UnitTesting;
    using NUnit.Framework;
    using TestCase = TestClass;
    using TestMethod = Test;

    at the top.

    ;)

    But seriously, why reinvent the wheel on this? I have no awesome thoughts of porting all of my NUnit-based tests over to this new framework. Why buck the community on this one?
  • Anonymous
    June 03, 2004
    or rather..
    using TestFixture = TestClass;

    :)
  • Anonymous
    June 03, 2004
    Both csUnit and NUnit use the same attributes for declaring test cases [TestFixture], test methods [Test] and setup/teardown operations [SetUp], [TearDown].

    IMHO I think it would be better and easier to use these well-known attributes in VS2005?!?
  • Anonymous
    June 08, 2004
    I really agree with Dietmar on his point about using the standard Attributes. Unless there is a really good reason this is a disservice to the community.

  • Anonymous
    June 14, 2004
    Please stop the fragmentation of the testing tools market!
  • Anonymous
    June 16, 2004
    Isn't this what they call "diversity"?
  • Anonymous
    June 29, 2004
    We all want unit testing feature in base vs 2005 versions
    see http://hugobatista.dotnetx.org/archive/2004/06/30/159.aspx