|
MbUnit : MyFirstTestFixtureTutorial
This page last changed on Sep 26, 2006 by hmobius.
MbUnit uses custom attributes (as in NUnit) to mark types that contain tests and mark methods as tests. A type tagged with the TestFixtureAttribute can contain an arbitrary number of test methods. Each method is detected by MbUnit and transformed into a test. Creating your first test.Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml 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()); } } Negative testsYou can also verify that a method throws an exception using the ExpectedExceptionAttribute attribute as in the example below: Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml [Test] [ExpectedException(typeof(FormatException))] public void WriteLineWithFormatOutOfRange() { this.writer = new StringWriter(); this.writer.WriteLine("{1}", writer); } The ExpectedExceptionAttribute is a TestDecorator. MbUnit provides a long list of such attributes but you can also define your own. Set up and tear down of each testOur 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. Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml [SetUp] public void SetUp() { this.writer = new StringWriter(); } [TearDown] public void CleanUp() { Console.WriteLine("Cleaning up"); } Set up and tear down of the fixtureAt last, one can add a set up and tear down method at the fixture level using the TestFixtureSetUpAttribute and TestFixtureTearDownAttribute. Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml [TestFixtureSetUp] public void FixtureSetUp() { ... } [TestFixtureTearDown] public void FixtureTearDown() { ... } |
| Document generated by Confluence on Jun 11, 2007 11:56 |