單元測試---MbUnit一些常用屬性介紹

來源:互聯網
上載者:User
(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()
{
    ...
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.