EditRethinking Unit Testing: xUnit.Net
- Brad Wilson, Jim Newkirk
- Get it at:
xUnit Core,
Extensions- The T in TDD is wrong: Replace [Test] with [Fact] and [Theory]
- Be Flexible: Removed [TestFixture]; Added static test methods, Added private test methods. TestFixture was an optomization and not essential.
- Be Safe: Create a new instance for each test method. Tests are run in a random order - on purpose!
- Be Sure: Removed [ExpectedException]. Exactly the right code is tested, and expected exception can hide code later in a test method.
- Be Extensible: Override how built-in exceptions work, Add pre and post operations for a test method.
- Extension methods are being explored. I.e. x.ShouldEqual(4); x.ShouldBeNull();
- Once you understand that a new object is created for every test method, the constructor becomes the setup, and the IDisposible Dispose method becomes the teardown. Method specific cleanup within the test method.
- Clock class shipped as part of the extensions to wrap the DateTime.Now. You can set it and also "freeze" time for comparing using the [Fact, FreezeClock]. - need to play to determine what this is use for!
- Assert.InRange(5, 1, 10); - would allow the actual to be between 1 and 10.
- Exception ex = Record.Exception( () => code ); returns null of no exception thrown, otherwise the exception that is thrown.
- Before and after methods taking MethodInfo allow you to do stuff before and after a fact.
- Fact is supposibly a statement that is always true; Theory is a statement that is true under certain conditions. Theory allow data driven tests.
[Theory]
[InLineData(42)]
[InLineData(-21)]
public void OurTheory(int x)
{
Assert.True(x > 0);
}Data can be inserted by InLineData, SQL, OLEDB, ExcelData (XLS file with named values not XLSX), and PropertyData (you define a Get property returning IEnumerable