(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()
{
...
}