Table of Contents
Attributes
Note: any testing framework attributes that are not in this list have no corresponding attribute in xUnit.net.| NUnit 2.x | MbUnit 2.4 | MSTest | xUnit.net | Comments |
| [Test] | [Test] | [TestMethod] | [Fact] | Marks a test method. |
| [TestFixture] | [TestFixture] | [TestClass] | n/a | xUnit.net does not require an attribute for a test class; it looks for all test methods in all public (exported) classes in the assembly. |
| [ExpectedException] | [ExpectedException] | [ExpectedException] | Assert.Throws or Record.Exception | xUnit.net has done away with the ExpectedException attribute in favor of Assert.Throws. See Note 1. |
| [SetUp] | [SetUp] | [TestInitialize] | Constructor | We believe that use of [SetUp] is generally bad. However, you can implement a parameterless constructor as a direct replacement. See Note 2. |
| [TearDown] | [TearDown] | [TestCleanup] | IDisposable.Dispose | We believe that use of [TearDown] is generally bad. However, you can implement IDisposable.Dispose as a direct replacement. See Note 2. |
| [TestFixtureSetUp] | [TestFixtureSetUp] | [ClassInitialize] | IUseFixture<T> | To get per-fixture setup, implement IUseFixture<T> on your test class. See Note 3 |
| [TestFixtureTearDown] | [TestFixtureTearDown] | [ClassCleanup] | IUseFixture<T> | To get per-fixture teardown, implement IUseFixture<T> on your test class. See Note 3 |
| [Ignore] | [Ignore] | [Ignore] | [Fact(Skip="reason")] | Set the Skip parameter on the [Fact] attribute to temporarily skip a test. |
| n/a | [Timeout] | [Timeout] | [Fact(Timeout=n)] | Set the Timeout parameter on the [Fact] attribute to cause a test to fail if it takes too long to run. Note that the timeout value for xUnit.net is in milliseconds. |
| [Property] | n/a | [TestProperty] | [Trait] | Set arbitrary metadata on a test |
| n/a | [Row], [RowTest] | [DataSource] | [Theory], [XxxData] | Theory (data-driven test). See Note 4 |
Note 1: Long-term use of
[ExpectedException] has uncovered various problems with it. First, it doesn't specifically say which line of code should throw the exception, which allows subtle and difficult-to-track failures that show up as passing tests. Second, it doesn't offer the opportunity to fully inspect details of the exception itself, since the handling is outside the normal code flow of the test. Assert.Throws allows you to test a specific set of code for throwing an exception, and returns the exception during success so you can write further asserts against the exception instance itself.
Note 2: The xUnit.net team feels that per-test setup and teardown creates difficult-to-follow and debug testing code, often causing unnecessary code to run before every single test is run. For more information, see
http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html and
http://www.agileprogrammer.com/dotnetguy/articles/SetupTeardown.aspx.
Note 3: xUnit.net provides a new way to think about per-fixture data with the use of the
IUseFixture<T> interface. By implementing this interface, you tell the test system that you wish to have certain fixture data provided to you before each test is run, the type of which is the T in the generic part of the interface. The runner will create a single instance of the fixture data and pass it through to your SetFixture() method before running each test. All the tests share the same instance of fixture data. After all the tests have run, the runner will dispose of the fixture data, if it implements
IDisposable.
Note 4: The extensions library (xunit.extensions.dll) ships with support for data-driven tests call Theories. Mark you test with the
[Theory] attribute (instead of
[Fact]), then decorate it with one or more
[XxxData] attributes, including
[InlineData],
[PropertyData],
[ExcelData],
[SqlServerData], and
[OleDbData]. See the unit tests in the test.xunitext project for examples.
Assertions
xUnit.net has removed most instances of the words "Are" and "Is". The NUnit 2.2.10 Assert class is used for comparison purposes; note that NUnit 2.4 offers both NUnit 2.2.x style and new "natural language" assertions. Note: any testing framework assertions which are not in this list have no corresponding assertion in xUnit.net.| NUnit 2.2.x | MbUnit 2.4 | MSTest | xUnit.net | Comments |
| AreEqual | AreEqual | AreEqual | Equal | MSTest and xUnit.net support generic versions of this method |
| AreNotEqual | AreNotEqual | AreNotEqual | NotEqual | MSTest and xUnit.net support generic versions of this method |
| AreNotSame | AreNotSame | AreNotSame | NotSame | |
| AreSame | AreSame | AreSame | Same | |
| Contains | Contains and In | Contains (on CollectionAssert) | Contains | MbUnit's Contains works on strings, and In works on containers |
| DoAssert | n/a | n/a | n/a | |
| n/a | NotIn | DoesNotContain (on CollectionAssert) | DoesNotContain | |
| n/a | n/a | n/a | DoesNotThrow | Ensures that the code does not throw any exceptions |
| Fail | Fail | Fail | n/a | xUnit.net alternative: Assert.True(false, "message") |
| Greater | Greater | n/a | n/a | xUnit.net alternative: Assert.True(x > y) |
| Ignore | Ignore | Inconclusive | n/a | |
| n/a | Between | n/a | InRange | Ensures that a value is in a given inclusive range (note: NUnit and MSTest have limited support for InRange on their AreEqual methods) |
| IsAssignableFrom | IsAssignableFrom | n/a | IsAssignableFrom | |
| IsEmpty | IsEmpty | n/a | Empty | |
| IsFalse | IsFalse | IsFalse | False | |
| IsInstanceOfType | IsInstanceOfType | IsInstanceOfType | IsType | |
| IsNaN | IsNan | n/a | n/a | xUnit.net alternative: Assert.True(double.IsNaN(x)) |
| IsNotAssignableFrom | IsNotAssignableFrom | n/a | n/a | xUnit.net alternative: Assert.False(obj is Type); |
| IsNotEmpty | IsNotEmpty | n/a | NotEmpty | |
| IsNotInstanceOfType | IsNotInstanceOfType | IsNotInstanceOfType | IsNotType | |
| IsNotNull | IsNotNull | IsNotNull | NotNull | |
| IsNull | IsNull | IsNull | Null | |
| IsTrue | IsTrue | IsTrue | True | |
| Less | Less | n/a | n/a | xUnit.net alternative: Assert.True(x < y) |
| n/a | NotBetween | n/a | NotInRange | Ensures that a value is not in a given inclusive range |
| n/a | n/a | n/a | Throws | Ensures that the code throws an exact exception |