架构深渊

慢慢走进程序的深渊……关注领域驱动设计、测试驱动开发、设计模式、企业应用架构模式……积累技术细节,以设计架构为宗。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

单元测试---MbUnit一些常用属性介绍

Posted on 2008-09-15 12:48  chen eric  阅读(427)  评论(0编辑  收藏  举报
(1)手工测试(显示在一个测试界面上----MUnit不推荐)
 using System;
using MbUnit.Core.Framework;
using MbUnit.Framework;

[TestFixture]
public class ManualFixture
{
    [Test]
    public void DoSomething()
    {
        ManualTester.DisplayForm(
            "Do this",
            "Do that",
            "You should see this",
            "..."
            );
    }
}
 
(2)更换测试线程的名称
A MbUnit fixture runs in it's own thread. By default, it uses the same AppartmentState as the main thread. If you want to force using another value, you can use the AppartmentState field in the constructor:
 
// A fixture running in STA
[TestFixture(ApartmentState = ApartmentState.STA)]
public class MyFixture
{
   [Test]
   public void DoingCOM()
   {
      ....
   }
}
 
(3)设定一个类方法的执行时间防止死锁
A MbUnit fixture runs in it's own thread. By default, it uses the same AppartmentState as the main thread. If you want to force using another value, you can use the AppartmentState field in the constructor:
// A fixture with 10 minute timeout
 
[TestFixture(TimeOut = 10)]
public class MyFixture
{
[Test]
public void AMessageBoxShouldNotBlockTheTestProcess()
{
 MessageBox.Show("I'm blocking the test automation");
}
}
 
(4)Test Suit作用好像是把多个funtion一起执行To use suites, tag your class with the [TestSuiteFixtureAttribute] attribute. Each method which creates suite must be tagged with [TestSuiteAttribute] and return a TestSuite. Of course, there can be multiple methods returning suites.

A TestSuite can be filled with ITestCase implementation, whether you implement your own version or you use the built-in implementations.

using System;
using MbUnit.Core.Framework;
using MbUnit.Framework;
namespace MyNamespace
{
[TestSuiteFixture]
 public class MyClass
{
  public delegate void TestDelegate(Object context);
 
 [TestSuite]
  public TestSuite GetSuite()
    {
        TestSuite suite = new TestSuite("Suite1");
        suite.Add( "Test1", new TestDelegate( this.Test ), "hello" );
        suite.Add( "Test2", new TestDelegate( this.AnotherTest), "another test" );
        return suite;
   }
 public void Test( object testContext )
  {
       Console.WriteLine("Test");
       Assert.AreEqual("hello", testContext);
  }
public void AnotherTest( object testContext )
   {
      Console.WriteLine("AnotherTest"); Assert.AreEqual("another test", testContext);
    }
 }
}
 
(5)规定方法执行顺序
ProcessTestFixture allows you to write tests in MbUnit that are executed in a defined sequence. Tests are then decorated with the TestSequenceAttribute to indicate the order in which they will be executed.
 
[ProcessTestFixture]
 public class CheckTestSequences
{
      int i = 0;
     [Test] [TestSequence( 2 )]
     public void Test2()
     {
       Assert.AreEqual( 2, i++ );
    }
 
    [Test] [TestSequence( 1 )]
    public void Test1()
    {
      Assert.AreEqual( 1, i++ );
    }
 
    [Test] [TestSequence( 0 )]
    public void Test0()
   {
      Assert.AreEqual( 0, i++ );
   }
}
 
(6)最基本的定义“测试”,“测试安装”属性
using System;
using System.IO;
using MbUnit.Framework;
[TestFixture]
public class MyTestFixture
{
    private TextWriter writer = null;
    [Test]
    public void WriteLine()
    {
        this.writer = new StringWriter();
        this.writer.WriteLine("hello world");
        Console.WriteLine(this.writer.ToString());
    }
}
Our current test has a lot of duplicated code, specialy initializing the writer field. You can define a setup method (tagged with [SetUpAttribute]) that will be run before each test. Similarly, a tear down method (tagged with [TearDownAttribute]) can be defined. The tear down method will be run after each test even if it fails.
[SetUp]
public void SetUp()
{
    this.writer = new StringWriter();
}
[TearDown]
public void CleanUp()
{
    Console.WriteLine("Cleaning up");
}
 
At last, one can add a set up and tear down method at the fixture level using the [TestFixtureSetUpAttribute] and [TestFixtureTearDownAttribute].
[TestFixtureSetUp]
public void FixtureSetUp()
{
    ...
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
    ...
}