Facts & Fixtures

The xUnit.js console runner uses the xUnit.js.Attributes test engine. In this way, fact and fixture registration can be kept with the target method; and is accomplished with C# style attribute notation: Function.RegisterNamespace("Test"); [Fixture] Test.SampleFixture=function(){ [Fixture] function NestedFixture(){ [Fact] function NestedFact(){ // Arrange var expected="expected"; // Act var actual=expected; // Assert Assert.Equal(expected,actual); } } }

The above example will produce two fixtures: Test.SampleFixture, and Test.SampleFixture.NestedFixture; and one fact: Test.SampleFixture.NestedFixture.NestedFact.

To avoid collisions, it is recommended to use namespaces for top level fixtures, as indicated above: Function.RegisterNamespace("Test"); [Fixture] Test.SampleFixture=function(){ [...] }

Methods decorated with Fixture attibutes are executed when the file or script it is contained in is loaded. When the method executes, nested fixtures and facts will be identifed and registered, and nested fixtures will also be executed, continuing the registration chain. This is important, as it can cause unintended side effects when dealing with variables and execution patterns local to the scope or closure of the fixture. In other words, local code inside a fixture is not equivalent to Setup and Teardown.

Methods decorated with Fact attributes are only executed when a test run begins. This will occur after all initial script files are loaded, and any targets or traits have been parsed, and matching tests collected.